package com.oracle.svm.hosted.c.info;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public final class AccessorInfo extends ElementInfo {
public enum AccessorKind {
GETTER("get"),
SETTER("set"),
OFFSET("offsetOf"),
ADDRESS("addressOf");
private final String prefix;
AccessorKind(String prefix) {
this.prefix = prefix;
}
}
private final ResolvedJavaMethod annotatedMethod;
private final AccessorKind accessorKind;
private final boolean isIndexed;
private final boolean hasLocationIdentityParameter;
private final boolean hasUniqueLocationIdentity;
public AccessorInfo(ResolvedJavaMethod annotatedMethod, AccessorKind accessorKind, boolean isIndexed, boolean hasLocationIdentityParameter, boolean hasUniqueLocationIdentity) {
super(annotatedMethod.getName());
this.annotatedMethod = annotatedMethod;
this.accessorKind = accessorKind;
this.isIndexed = isIndexed;
this.hasLocationIdentityParameter = hasLocationIdentityParameter;
this.hasUniqueLocationIdentity = hasUniqueLocationIdentity;
}
public AccessorKind getAccessorKind() {
return accessorKind;
}
String getAccessorPrefix() {
return accessorKind.prefix;
}
public boolean isIndexed() {
return isIndexed;
}
public boolean hasLocationIdentityParameter() {
return hasLocationIdentityParameter;
}
public boolean hasUniqueLocationIdentity() {
return hasUniqueLocationIdentity;
}
public static int baseParameterNumber(boolean withReceiver) {
assert withReceiver;
return 0;
}
public int indexParameterNumber(boolean withReceiver) {
assert isIndexed();
return withReceiver ? 1 : 0;
}
public int valueParameterNumber(boolean withReceiver) {
assert accessorKind == AccessorKind.SETTER;
return (withReceiver ? 1 : 0) + (isIndexed() ? 1 : 0);
}
public int locationIdentityParameterNumber(boolean withReceiver) {
assert hasLocationIdentityParameter();
return parameterCount(withReceiver) - 1;
}
public int parameterCount(boolean withReceiver) {
return (withReceiver ? 1 : 0) + (isIndexed() ? 1 : 0) + (getAccessorKind() == AccessorKind.SETTER ? 1 : 0) + (hasLocationIdentityParameter() ? 1 : 0);
}
@Override
public ResolvedJavaMethod getAnnotatedElement() {
return annotatedMethod;
}
@Override
public void accept(InfoTreeVisitor visitor) {
visitor.visitAccessorInfo(this);
}
public ResolvedJavaType getReturnType() {
return getReturnType(annotatedMethod);
}
public ResolvedJavaType getParameterType(int index) {
return getParameterType(annotatedMethod, index);
}
public ResolvedJavaType getValueParameterType() {
return getParameterType(valueParameterNumber(false));
}
public static ResolvedJavaType getReturnType(ResolvedJavaMethod method) {
return (ResolvedJavaType) method.getSignature().getReturnType(method.getDeclaringClass());
}
public static ResolvedJavaType getParameterType(ResolvedJavaMethod method, int index) {
return (ResolvedJavaType) method.getSignature().getParameterType(index, method.getDeclaringClass());
}
}