use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.
the class JDKProxyRedefinitionPlugin method collectProxyArguments.
public synchronized void collectProxyArguments(@JavaType(String.class) StaticObject proxyName, @JavaType(Class[].class) StaticObject interfaces, int classModifier, DirectCallNode generatorMethodCallNode) {
if (proxyGeneratorMethodCallNode == null) {
proxyGeneratorMethodCallNode = generatorMethodCallNode;
}
// register onLoad action that will give us
// the klass object for the generated proxy
registerClassLoadAction(getContext().getMeta().toHostString(proxyName), klass -> {
// store guest-world arguments that we can use when
// invoking the call node later on re-generation
ProxyCache proxyCache = new ProxyCache(klass, proxyName, interfaces, classModifier);
Klass[] proxyInterfaces = new Klass[interfaces.length()];
for (int i = 0; i < proxyInterfaces.length; i++) {
proxyInterfaces[i] = (Klass) getContext().getMeta().HIDDEN_MIRROR_KLASS.getHiddenObject(interfaces.get(i));
}
// when they change we can re-generate the proxy bytes
for (KlassRef proxyInterface : proxyInterfaces) {
addCacheEntry(proxyCache, proxyInterface);
}
});
}
use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.
the class VM method JVM_FindLoadedClass.
@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_FindLoadedClass(@JavaType(ClassLoader.class) StaticObject loader, @JavaType(String.class) StaticObject name) {
Symbol<Type> type = getTypes().fromClassGetName(getMeta().toHostString(name));
// HotSpot skips reflection (DelegatingClassLoader) class loaders.
Klass klass = getRegistries().findLoadedClass(type, nonReflectionClassLoader(loader));
if (klass == null) {
return StaticObject.NULL;
}
return klass.mirror();
}
use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.
the class VM method JVM_GetClassDeclaredFields.
@VmImpl(isJni = true)
@JavaType(java.lang.reflect.Field[].class)
public StaticObject JVM_GetClassDeclaredFields(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
// TODO(peterssen): From Hostpot: 4496456 We need to filter out
// java.lang.Throwable.backtrace.
Meta meta = getMeta();
ArrayList<Field> collectedMethods = new ArrayList<>();
Klass klass = self.getMirrorKlass();
klass.ensureLinked();
for (Field f : klass.getDeclaredFields()) {
if (!publicOnly || f.isPublic()) {
collectedMethods.add(f);
}
}
final Field[] fields = collectedMethods.toArray(Field.EMPTY_ARRAY);
EspressoContext context = meta.getContext();
// TODO(peterssen): Cache guest j.l.reflect.Field constructor.
// Calling the constructor is just for validation, manually setting the fields would be
// faster.
Method fieldInit;
if (meta.getJavaVersion().java15OrLater()) {
fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
Type.java_lang_Class, /* name */
Type.java_lang_String, /* type */
Type.java_lang_Class, /* modifiers */
Type._int, /* trustedFinal */
Type._boolean, /* slot */
Type._int, /* signature */
Type.java_lang_String, /* annotations */
Type._byte_array));
} else {
fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
Type.java_lang_Class, /* name */
Type.java_lang_String, /* type */
Type.java_lang_Class, /* modifiers */
Type._int, /* slot */
Type._int, /* signature */
Type.java_lang_String, /* annotations */
Type._byte_array));
}
StaticObject fieldsArray = meta.java_lang_reflect_Field.allocateReferenceArray(fields.length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int i) {
final Field f = fields[i];
StaticObject instance = meta.java_lang_reflect_Field.allocateInstance();
Attribute rawRuntimeVisibleAnnotations = f.getAttribute(Name.RuntimeVisibleAnnotations);
StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
Attribute rawRuntimeVisibleTypeAnnotations = f.getAttribute(Name.RuntimeVisibleTypeAnnotations);
StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
if (meta.getJavaVersion().java15OrLater()) {
fieldInit.invokeDirect(/* this */
instance, /* declaringKlass */
f.getDeclaringKlass().mirror(), /* name */
context.getStrings().intern(f.getName()), /* type */
f.resolveTypeKlass().mirror(), /* modifiers */
f.getModifiers(), /* trustedFinal */
f.isTrustedFinal(), /* slot */
f.getSlot(), /* signature */
meta.toGuestString(f.getGenericSignature()), /* annotations */
runtimeVisibleAnnotations);
} else {
fieldInit.invokeDirect(/* this */
instance, /* declaringKlass */
f.getDeclaringKlass().mirror(), /* name */
context.getStrings().intern(f.getName()), /* type */
f.resolveTypeKlass().mirror(), /* modifiers */
f.getModifiers(), /* slot */
f.getSlot(), /* signature */
meta.toGuestString(f.getGenericSignature()), /* annotations */
runtimeVisibleAnnotations);
}
meta.HIDDEN_FIELD_KEY.setHiddenObject(instance, f);
meta.HIDDEN_FIELD_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
return instance;
}
});
return fieldsArray;
}
use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.
the class VM method cloneForeignArray.
private static StaticObject cloneForeignArray(StaticObject array, Meta meta, InteropLibrary interop, ToEspressoNode toEspressoNode, SubstitutionProfiler profiler, char exceptionBranch) {
assert array.isForeignObject();
assert array.isArray();
int length;
try {
long longLength = interop.getArraySize(array.rawForeignObject());
if (longLength > Integer.MAX_VALUE) {
profiler.profile(exceptionBranch);
throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, "Cannot clone a foreign array whose length does not fit in int");
}
if (longLength < 0) {
profiler.profile(exceptionBranch);
throw meta.throwExceptionWithMessage(meta.java_lang_NegativeArraySizeException, "Cannot clone a foreign array with negative length");
}
length = (int) longLength;
} catch (UnsupportedMessageException e) {
profiler.profile(exceptionBranch);
throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, "Cannot clone a non-array foreign object as an array");
}
ArrayKlass arrayKlass = (ArrayKlass) array.getKlass();
Klass componentType = arrayKlass.getComponentType();
if (componentType.isPrimitive()) {
try {
switch(componentType.getJavaKind()) {
case Boolean:
boolean[] booleanArray = new boolean[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
booleanArray[i] = (boolean) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, booleanArray);
case Byte:
byte[] byteArray = new byte[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
byteArray[i] = (byte) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, byteArray);
case Short:
short[] shortArray = new short[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
shortArray[i] = (short) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, shortArray);
case Char:
char[] charArray = new char[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
charArray[i] = (char) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, charArray);
case Int:
int[] intArray = new int[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
intArray[i] = (int) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, intArray);
case Float:
float[] floatArray = new float[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
floatArray[i] = (float) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, floatArray);
case Long:
long[] longArray = new long[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
longArray[i] = (long) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, longArray);
case Double:
double[] doubleArray = new double[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
doubleArray[i] = (double) toEspressoNode.execute(foreignElement, componentType);
}
return StaticObject.createArray(arrayKlass, doubleArray);
case Object:
case Void:
case ReturnAddress:
case Illegal:
CompilerDirectives.transferToInterpreter();
throw EspressoError.shouldNotReachHere("Unexpected primitive kind: " + componentType.getJavaKind());
}
} catch (UnsupportedTypeException e) {
profiler.profile(exceptionBranch);
throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "Cannot cast an element of a foreign array to the declared component type");
}
}
StaticObject[] newArray = new StaticObject[length];
for (int i = 0; i < length; ++i) {
Object foreignElement = readForeignArrayElement(array, i, interop, meta, profiler, exceptionBranch);
try {
newArray[i] = (StaticObject) toEspressoNode.execute(foreignElement, componentType);
} catch (UnsupportedTypeException e) {
profiler.profile(exceptionBranch);
throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "Cannot cast an element of a foreign array to the declared component type");
}
}
return StaticObject.createArray(arrayKlass, newArray);
}
use of com.oracle.truffle.espresso.impl.Klass in project graal by oracle.
the class VM method JVM_GetEnclosingMethodInfo.
@VmImpl(isJni = true)
@JavaType(Object[].class)
public StaticObject JVM_GetEnclosingMethodInfo(@JavaType(Class.class) StaticObject self) {
Meta meta = getMeta();
InterpreterToVM vm = meta.getInterpreterToVM();
if (self.getMirrorKlass() instanceof ObjectKlass) {
ObjectKlass klass = (ObjectKlass) self.getMirrorKlass();
EnclosingMethodAttribute enclosingMethodAttr = klass.getEnclosingMethod();
if (enclosingMethodAttr == null) {
return StaticObject.NULL;
}
int classIndex = enclosingMethodAttr.getClassIndex();
if (classIndex == 0) {
return StaticObject.NULL;
}
StaticObject arr = meta.java_lang_Object.allocateReferenceArray(3);
RuntimeConstantPool pool = klass.getConstantPool();
Klass enclosingKlass = pool.resolvedKlassAt(klass, classIndex);
vm.setArrayObject(enclosingKlass.mirror(), 0, arr);
int methodIndex = enclosingMethodAttr.getMethodIndex();
if (methodIndex != 0) {
NameAndTypeConstant nmt = pool.nameAndTypeAt(methodIndex);
StaticObject name = meta.toGuestString(nmt.getName(pool));
StaticObject desc = meta.toGuestString(nmt.getDescriptor(pool));
vm.setArrayObject(name, 1, arr);
vm.setArrayObject(desc, 2, arr);
}
return arr;
}
return StaticObject.NULL;
}
Aggregations