package com.oracle.svm.configure.config;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import com.oracle.svm.configure.json.JsonPrintable;
import com.oracle.svm.configure.json.JsonWriter;
import jdk.vm.ci.meta.MetaUtil;
public class ConfigurationMethod implements JsonPrintable {
public static final String CONSTRUCTOR_NAME = "<init>";
public static boolean isConstructorName(String name) {
return CONSTRUCTOR_NAME.equals(name);
}
public static String toInternalParamsSignature(List<ConfigurationType> types) {
StringBuilder sb = new StringBuilder("(");
for (ConfigurationType type : types) {
sb.append(MetaUtil.toInternalName(type.getQualifiedJavaName()));
}
sb.append(')');
return sb.toString();
}
private final String name;
private final String internalSignature;
private int hash;
public ConfigurationMethod(String name, String internalSignature) {
this.name = name;
String paramsOnlySignature = internalSignature;
if (paramsOnlySignature != null) {
int paramsEnd = internalSignature.lastIndexOf(')');
paramsOnlySignature = paramsOnlySignature.substring(0, paramsEnd + 1);
}
this.internalSignature = paramsOnlySignature;
}
public String getName() {
return name;
}
public String getInternalSignature() {
return internalSignature;
}
public boolean matches(String methodName, String methodInternalSignature) {
return getName().equals(methodName) && (internalSignature == null || methodInternalSignature.startsWith(internalSignature));
}
public boolean isConstructor() {
return isConstructorName(name);
}
@Override
public void printJson(JsonWriter writer) throws IOException {
writer.append('{');
writer.quote("name").append(':').quote(name).append(',');
writer.quote("parameterTypes").append(":[");
String prefix = "";
for (String type : SignatureUtil.toParameterTypes(internalSignature)) {
writer.append(prefix).quote(type);
prefix = ",";
}
writer.append("] }");
}
@Override
public int hashCode() {
if (hash == 0) {
hash = name.hashCode() * 31 + (internalSignature == null ? 0 : internalSignature.hashCode());
}
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj != this && obj instanceof ConfigurationMethod) {
ConfigurationMethod other = (ConfigurationMethod) obj;
return name.equals(other.name) && Objects.equals(internalSignature, other.internalSignature);
}
return (obj == this);
}
}