package jdk.vm.ci.hotspot;
import static jdk.vm.ci.hotspot.HotSpotModifiers.jvmFieldModifiers;
import static jdk.vm.ci.hotspot.HotSpotVMConfig.config;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import jdk.internal.vm.annotation.Stable;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaType;
class HotSpotResolvedJavaFieldImpl implements HotSpotResolvedJavaField {
private final HotSpotResolvedObjectTypeImpl holder;
private JavaType type;
private final int offset;
private final short index;
private final int modifiers;
HotSpotResolvedJavaFieldImpl(HotSpotResolvedObjectTypeImpl holder, JavaType type, long offset, int modifiers, int index) {
this.holder = holder;
this.type = type;
this.index = (short) index;
assert this.index == index;
assert offset != -1;
assert offset == (int) offset : "offset larger than int";
this.offset = (int) offset;
this.modifiers = modifiers;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof HotSpotResolvedJavaField) {
HotSpotResolvedJavaFieldImpl that = (HotSpotResolvedJavaFieldImpl) obj;
if (that.offset != this.offset || that.isStatic() != this.isStatic()) {
return false;
} else if (this.holder.equals(that.holder)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return offset ^ modifiers;
}
@Override
public int getModifiers() {
return modifiers & jvmFieldModifiers();
}
@Override
public boolean isInternal() {
return (modifiers & config().jvmAccFieldInternal) != 0;
}
public boolean isInObject(Object object) {
if (isStatic()) {
return false;
}
return getDeclaringClass().isAssignableFrom(HotSpotResolvedObjectTypeImpl.fromObjectClass(object.getClass()));
}
@Override
public HotSpotResolvedObjectTypeImpl getDeclaringClass() {
return holder;
}
@Override
public String getName() {
return holder.createFieldInfo(index).getName();
}
@Override
public JavaType getType() {
JavaType currentType = type;
if (currentType instanceof HotSpotUnresolvedJavaType) {
HotSpotUnresolvedJavaType unresolvedType = (HotSpotUnresolvedJavaType) currentType;
ResolvedJavaType resolved = unresolvedType.reresolve(holder);
if (resolved != null) {
type = resolved;
}
}
return type;
}
public int offset() {
return offset;
}
@Override
public String toString() {
return format("HotSpotField<%H.%n %t:") + offset + ">";
}
@Override
public boolean isSynthetic() {
return (config().jvmAccSynthetic & modifiers) != 0;
}
public boolean isStable() {
return (config().jvmAccFieldStable & modifiers) != 0;
}
@Override
public Annotation[] getAnnotations() {
Field javaField = toJava();
if (javaField != null) {
return javaField.getAnnotations();
}
return new Annotation[0];
}
@Override
public Annotation[] getDeclaredAnnotations() {
Field javaField = toJava();
if (javaField != null) {
return javaField.getDeclaredAnnotations();
}
return new Annotation[0];
}
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
Field javaField = toJava();
if (javaField != null) {
return javaField.getAnnotation(annotationClass);
}
return null;
}
private Field toJava() {
if (isInternal()) {
return null;
}
try {
return holder.mirror().getDeclaredField(getName());
} catch (NoSuchFieldException | NoClassDefFoundError e) {
return null;
}
}
}