package jdk.vm.ci.hotspot;
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
class SharedLibraryJVMCIReflection extends HotSpotJVMCIReflection {
@Override
Object resolveObject(HotSpotObjectConstantImpl objectHandle) {
throw new HotSpotJVMCIUnsupportedOperationError("cannot resolve handle in a JVMCI shared library to a raw object: " + objectHandle);
}
@Override
boolean isInstance(HotSpotResolvedObjectTypeImpl holder, HotSpotObjectConstantImpl obj) {
if (obj instanceof DirectHotSpotObjectConstantImpl) {
ResolvedJavaType type = getType(obj);
return holder.isAssignableFrom(type);
}
return runtime().compilerToVm.isInstance(holder, obj);
}
@Override
boolean isAssignableFrom(HotSpotResolvedObjectTypeImpl holder, HotSpotResolvedObjectTypeImpl otherType) {
return runtime().compilerToVm.isAssignableFrom(holder, otherType);
}
@Override
boolean isLocalClass(HotSpotResolvedObjectTypeImpl holder) {
throw new HotSpotJVMCIUnsupportedOperationError("requires a call Class.isLocalClass()");
}
@Override
boolean isMemberClass(HotSpotResolvedObjectTypeImpl holder) {
throw new HotSpotJVMCIUnsupportedOperationError("requires a call Class.isMemberClass()");
}
@Override
HotSpotResolvedObjectType getEnclosingClass(HotSpotResolvedObjectTypeImpl holder) {
throw new HotSpotJVMCIUnsupportedOperationError("requires a call Class.getEnclosingClass()");
}
@Override
JavaConstant readFieldValue(HotSpotResolvedObjectTypeImpl holder, HotSpotResolvedJavaField field, boolean isVolatile) {
JavaConstant javaConstant = runtime().compilerToVm.readFieldValue(holder, field, isVolatile);
if (javaConstant == null) {
return JavaConstant.NULL_POINTER;
}
return javaConstant;
}
@Override
JavaConstant readFieldValue(HotSpotObjectConstantImpl object, HotSpotResolvedJavaField field, boolean isVolatile) {
if (object instanceof DirectHotSpotObjectConstantImpl) {
return null;
}
JavaConstant javaConstant = runtime().compilerToVm.readFieldValue(object, field, isVolatile);
if (javaConstant == null) {
return JavaConstant.NULL_POINTER;
}
return javaConstant;
}
@Override
boolean equals(HotSpotObjectConstantImpl x, HotSpotObjectConstantImpl y) {
if (x == y) {
return true;
}
if (x.compressed != y.compressed) {
return false;
}
if (x instanceof DirectHotSpotObjectConstantImpl && y instanceof DirectHotSpotObjectConstantImpl) {
DirectHotSpotObjectConstantImpl xd = (DirectHotSpotObjectConstantImpl) x;
DirectHotSpotObjectConstantImpl yd = (DirectHotSpotObjectConstantImpl) y;
return (xd.object == yd.object);
}
if (x instanceof DirectHotSpotObjectConstantImpl || y instanceof DirectHotSpotObjectConstantImpl) {
return false;
}
IndirectHotSpotObjectConstantImpl indirectX = (IndirectHotSpotObjectConstantImpl) x;
IndirectHotSpotObjectConstantImpl indirectY = (IndirectHotSpotObjectConstantImpl) y;
return runtime().compilerToVm.equals(x, indirectX.objectHandle, y, indirectY.objectHandle);
}
@Override
JavaConstant getJavaMirror(HotSpotResolvedPrimitiveType hotSpotResolvedPrimitiveType) {
return runtime().compilerToVm.getJavaMirror(hotSpotResolvedPrimitiveType);
}
@Override
ResolvedJavaMethod.Parameter[] getParameters(HotSpotResolvedJavaMethodImpl javaMethod) {
return null;
}
static Annotation[] getClassAnnotations(String className) {
throw new InternalError("missing substitution: " + className);
}
static Annotation[][] getParameterAnnotations(String className, String methodName) {
throw new InternalError("missing substitution: " + className + " " + methodName);
}
@Override
Annotation[] getAnnotations(HotSpotResolvedObjectTypeImpl holder) {
Annotation[] annotations = getClassAnnotations(holder.getName());
return annotations == null ? new Annotation[0] : annotations;
}
@Override
Annotation[] getDeclaredAnnotations(HotSpotResolvedObjectTypeImpl holder) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
<T extends Annotation> T getAnnotation(HotSpotResolvedObjectTypeImpl holder, Class<T> annotationClass) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
Annotation[][] getParameterAnnotations(HotSpotResolvedJavaMethodImpl javaMethod) {
Annotation[][] annotations = getParameterAnnotations(javaMethod.getDeclaringClass().getName(), javaMethod.getName());
if (annotations == null) {
return new Annotation[javaMethod.signature.getParameterCount(false)][0];
}
return annotations;
}
@Override
Type[] getGenericParameterTypes(HotSpotResolvedJavaMethodImpl javaMethod) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
Annotation[] getFieldAnnotations(HotSpotResolvedJavaFieldImpl javaField) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
Annotation[] getMethodAnnotations(HotSpotResolvedJavaMethodImpl javaMethod) {
Annotation[] annotations = getMethodAnnotationsInternal(javaMethod);
return annotations == null ? new Annotation[0] : annotations;
}
@Override
<T extends Annotation> T getMethodAnnotation(HotSpotResolvedJavaMethodImpl javaMethod, Class<T> annotationClass) {
Annotation[] methodAnnotations = getMethodAnnotations(javaMethod);
if (methodAnnotations != null) {
for (Annotation ann : methodAnnotations) {
if (annotationClass.isInstance(ann)) {
return annotationClass.cast(ann);
}
}
}
return null;
}
@SuppressWarnings("unused")
private static Annotation[] getMethodAnnotationsInternal(ResolvedJavaMethod javaMethod) {
throw new InternalError("missing substitution");
}
@Override
Annotation[] getMethodDeclaredAnnotations(HotSpotResolvedJavaMethodImpl javaMethod) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
Annotation[] getFieldDeclaredAnnotations(HotSpotResolvedJavaFieldImpl javaMethod) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
<T extends Annotation> T getFieldAnnotation(HotSpotResolvedJavaFieldImpl javaField, Class<T> annotationClass) {
throw new HotSpotJVMCIUnsupportedOperationError("unimplemented");
}
@Override
HotSpotResolvedObjectTypeImpl getType(HotSpotObjectConstantImpl object) {
if (object instanceof DirectHotSpotObjectConstantImpl) {
Class<?> theClass = ((DirectHotSpotObjectConstantImpl) object).object.getClass();
try {
String name = theClass.getName().replace('.', '/');
HotSpotResolvedObjectTypeImpl type = (HotSpotResolvedObjectTypeImpl) runtime().compilerToVm.lookupType(name, null, true);
if (type == null) {
throw new InternalError(name);
}
return type;
} catch (ClassNotFoundException e) {
throw new InternalError(e);
}
}
return runtime().compilerToVm.getResolvedJavaType(object, runtime().getConfig().hubOffset, false);
}
@Override
String asString(HotSpotObjectConstantImpl object) {
if (object instanceof IndirectHotSpotObjectConstantImpl) {
return runtime().compilerToVm.asString(object);
}
Object value = ((DirectHotSpotObjectConstantImpl) object).object;
if (value instanceof String) {
return (String) value;
}
return null;
}
@Override
ResolvedJavaType asJavaType(HotSpotObjectConstantImpl object) {
if (object instanceof DirectHotSpotObjectConstantImpl) {
DirectHotSpotObjectConstantImpl direct = (DirectHotSpotObjectConstantImpl) object;
if (direct.object instanceof Class) {
Class<?> javaClass = (Class<?>) direct.object;
return runtime().fromClass(javaClass);
}
if (direct.object instanceof ResolvedJavaType) {
return (ResolvedJavaType) convertUnknownValue(direct.object);
}
return null;
}
return runtime().compilerToVm.asJavaType(object);
}
static Object convertUnknownValue(Object object) {
return object;
}
@SuppressWarnings("unchecked")
@Override
<T> T asObject(HotSpotObjectConstantImpl object, Class<T> type) {
if (object instanceof DirectHotSpotObjectConstantImpl) {
Object theObject = ((DirectHotSpotObjectConstantImpl) object).object;
if (type.isInstance(theObject)) {
return (T) convertUnknownValue(type.cast(theObject));
}
}
return null;
}
@Override
Object asObject(HotSpotObjectConstantImpl object, HotSpotResolvedJavaType type) {
throw new HotSpotJVMCIUnsupportedOperationError("cannot resolve a shared library JVMCI object handle to a " +
"raw object as it may be in another runtime");
}
@Override
String formatString(HotSpotObjectConstantImpl object) {
if (object instanceof DirectHotSpotObjectConstantImpl) {
DirectHotSpotObjectConstantImpl direct = (DirectHotSpotObjectConstantImpl) object;
return "CompilerObject<" + direct.object.getClass().getName() + ">";
}
return "Instance<" + object.getType().toJavaName() + ">";
}
@Override
Integer getLength(HotSpotObjectConstantImpl object) {
if (object instanceof DirectHotSpotObjectConstantImpl) {
DirectHotSpotObjectConstantImpl direct = (DirectHotSpotObjectConstantImpl) object;
if (direct.object.getClass().isArray()) {
return Array.getLength(direct.object);
}
return null;
}
int length = runtime().compilerToVm.getArrayLength(object);
if (length >= 0) {
return length;
}
return null;
}
@Override
JavaConstant readArrayElement(HotSpotObjectConstantImpl arrayObject, int index) {
Object result = runtime().compilerToVm.readArrayElement(arrayObject, index);
if (result == null) {
return null;
}
if (result instanceof JavaConstant) {
return (JavaConstant) result;
}
JavaConstant constant = JavaConstant.forBoxedPrimitive(result);
if (constant == null) {
throw new InternalError("Unexpected value " + result);
}
return constant;
}
@Override
JavaConstant forObject(Object value) {
return DirectHotSpotObjectConstantImpl.forObject(value, false);
}
@Override
JavaConstant unboxPrimitive(HotSpotObjectConstantImpl source) {
Object box = runtime().compilerToVm.unboxPrimitive(source);
return JavaConstant.forBoxedPrimitive(box);
}
@Override
JavaConstant boxPrimitive(JavaConstant source) {
return runtime().compilerToVm.boxPrimitive(source.asBoxedPrimitive());
}
@Override
int getInt(HotSpotObjectConstantImpl object, long displacement) {
return runtime().compilerToVm.getInt(object, displacement);
}
@Override
byte getByte(HotSpotObjectConstantImpl object, long displacement) {
return runtime().compilerToVm.getByte(object, displacement);
}
@Override
short getShort(HotSpotObjectConstantImpl object, long displacement) {
return runtime().compilerToVm.getShort(object, displacement);
}
@Override
long getLong(HotSpotObjectConstantImpl object, long displacement) {
return runtime().compilerToVm.getLong(object, displacement);
}
@Override
void checkRead(HotSpotObjectConstantImpl constant, JavaKind kind, long displacement, HotSpotResolvedObjectType type) {
}
}