package jdk.vm.ci.hotspot;
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
import static jdk.vm.ci.hotspot.HotSpotVMConfig.config;
import static jdk.vm.ci.hotspot.UnsafeAccess.UNSAFE;
import static jdk.internal.misc.Unsafe.ADDRESS_SIZE;
import java.lang.annotation.Annotation;
import jdk.internal.vm.annotation.Stable;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.UnresolvedJavaType;
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 HotSpotResolvedJavaFieldImpl) {
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 holder.hashCode() ^ offset;
}
@Override
public int getModifiers() {
return modifiers & HotSpotModifiers.jvmFieldModifiers();
}
@Override
public boolean isInternal() {
return (modifiers & config().jvmAccFieldInternal) != 0;
}
@Override
public boolean isInObject(JavaConstant object) {
if (isStatic()) {
return false;
}
HotSpotObjectConstant constant = (HotSpotObjectConstant) object;
return getDeclaringClass().isAssignableFrom(constant.getType());
}
@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 UnresolvedJavaType) {
UnresolvedJavaType unresolvedType = (UnresolvedJavaType) currentType;
JavaType resolved = HotSpotJVMCIRuntime.runtime().lookupType(unresolvedType.getName(), holder, false);
if (resolved instanceof ResolvedJavaType) {
type = resolved;
}
}
return type;
}
@Override
public int getOffset() {
return offset;
}
int getIndex() {
return index;
}
@Override
public String toString() {
return format("HotSpotResolvedJavaFieldImpl<%H.%n %t:") + offset + ">";
}
@Override
public boolean isSynthetic() {
return (config().jvmAccSynthetic & modifiers) != 0;
}
@Override
public boolean isStable() {
return (config().jvmAccFieldStable & modifiers) != 0;
}
private boolean hasAnnotations() {
if (!isInternal()) {
HotSpotVMConfig config = config();
final long metaspaceAnnotations = UNSAFE.getAddress(holder.getMetaspaceKlass() + config.instanceKlassAnnotationsOffset);
if (metaspaceAnnotations != 0) {
long fieldsAnnotations = UNSAFE.getAddress(metaspaceAnnotations + config.annotationsFieldAnnotationsOffset);
if (fieldsAnnotations != 0) {
long fieldAnnotations = UNSAFE.getAddress(fieldsAnnotations + config.fieldsAnnotationsBaseOffset + (ADDRESS_SIZE * index));
return fieldAnnotations != 0;
}
}
}
return false;
}
@Override
public Annotation[] getAnnotations() {
if (!hasAnnotations()) {
return new Annotation[0];
}
return runtime().reflection.getFieldAnnotations(this);
}
@Override
public Annotation[] getDeclaredAnnotations() {
if (!hasAnnotations()) {
return new Annotation[0];
}
return runtime().reflection.getFieldDeclaredAnnotations(this);
}
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
if (!hasAnnotations()) {
return null;
}
return runtime().reflection.getFieldAnnotation(this, annotationClass);
}
}