use of com.oracle.truffle.espresso.substitutions.JavaType 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.substitutions.JavaType in project graal by oracle.
the class VM method JVM_GetMethodParameters.
@VmImpl(isJni = true)
@JavaType(Parameter[].class)
public StaticObject JVM_GetMethodParameters(@JavaType(Object.class) StaticObject executable, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
assert meta.java_lang_reflect_Executable.isAssignableFrom(executable.getKlass());
StaticObject parameterTypes = (StaticObject) executable.getKlass().lookupMethod(Name.getParameterTypes, Signature.Class_array).invokeDirect(executable);
int numParams = parameterTypes.length();
if (numParams == 0) {
return StaticObject.NULL;
}
Method method;
if (meta.java_lang_reflect_Method.isAssignableFrom(executable.getKlass())) {
method = Method.getHostReflectiveMethodRoot(executable, meta);
} else if (meta.java_lang_reflect_Constructor.isAssignableFrom(executable.getKlass())) {
method = Method.getHostReflectiveConstructorRoot(executable, meta);
} else {
throw EspressoError.shouldNotReachHere();
}
MethodParametersAttribute methodParameters = (MethodParametersAttribute) method.getAttribute(Name.MethodParameters);
if (methodParameters == null) {
return StaticObject.NULL;
}
// Verify first.
/*
* If number of entries in ParametersAttribute is inconsistent with actual parameters from
* the signature, it will be caught in guest java code.
*/
int cpLength = method.getConstantPool().length();
for (MethodParametersAttribute.Entry entry : methodParameters.getEntries()) {
int nameIndex = entry.getNameIndex();
if (nameIndex < 0 || nameIndex >= cpLength) {
profiler.profile(0);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Constant pool index out of bounds");
}
if (nameIndex != 0 && method.getConstantPool().tagAt(nameIndex) != ConstantPool.Tag.UTF8) {
profiler.profile(1);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Wrong type at constant pool index");
}
}
// TODO(peterssen): Cache guest j.l.reflect.Parameter constructor.
// Calling the constructor is just for validation, manually setting the fields would
// be faster.
Method parameterInit = meta.java_lang_reflect_Parameter.lookupDeclaredMethod(Name._init_, getSignatures().makeRaw(Type._void, /* name */
Type.java_lang_String, /* modifiers */
Type._int, /* executable */
Type.java_lang_reflect_Executable, /* index */
Type._int));
// Use attribute's number of parameters.
return meta.java_lang_reflect_Parameter.allocateReferenceArray(methodParameters.getEntries().length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int index) {
MethodParametersAttribute.Entry entry = methodParameters.getEntries()[index];
StaticObject instance = meta.java_lang_reflect_Parameter.allocateInstance();
// For a 0 index, give an empty name.
StaticObject guestName;
if (entry.getNameIndex() != 0) {
guestName = meta.toGuestString(method.getConstantPool().symbolAt(entry.getNameIndex(), "parameter name").toString());
} else {
guestName = getJavaVersion().java9OrLater() ? StaticObject.NULL : meta.toGuestString("");
}
parameterInit.invokeDirect(/* this */
instance, /* name */
guestName, /* modifiers */
entry.getAccessFlags(), /* executable */
executable, /* index */
index);
return instance;
}
});
}
use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.
the class VM method JVM_DefineClass.
@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class.class)
public StaticObject JVM_DefineClass(@Pointer TruffleObject namePtr, @JavaType(ClassLoader.class) StaticObject loader, @Pointer TruffleObject bufPtr, int len, @JavaType(ProtectionDomain.class) StaticObject pd) {
ByteBuffer buf = NativeUtils.directByteBuffer(bufPtr, len, JavaKind.Byte);
final byte[] bytes = new byte[len];
buf.get(bytes);
// can be null
Symbol<Type> type = namePtrToInternal(namePtr);
StaticObject clazz = getContext().getRegistries().defineKlass(type, bytes, loader, new ClassRegistry.ClassDefinitionInfo(pd)).mirror();
assert clazz != null;
return clazz;
}
use of com.oracle.truffle.espresso.substitutions.JavaType 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.substitutions.JavaType 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