use of com.oracle.truffle.espresso.impl.Method 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.impl.Method in project graal by oracle.
the class VM method JVM_InitStackTraceElement.
@VmImpl(isJni = true)
@SuppressWarnings("unused")
public void JVM_InitStackTraceElement(@JavaType(StackTraceElement.class) StaticObject element, @JavaType(internalName = "Ljava/lang/StackFrameInfo;") StaticObject info, @Inject Meta meta) {
if (StaticObject.isNull(element) || StaticObject.isNull(info)) {
throw meta.throwNullPointerException();
}
StaticObject mname = meta.java_lang_StackFrameInfo_memberName.getObject(info);
if (StaticObject.isNull(mname)) {
throw meta.throwExceptionWithMessage(meta.java_lang_InternalError, "uninitialized StackFrameInfo !");
}
StaticObject clazz = meta.java_lang_invoke_MemberName_clazz.getObject(mname);
Method m = (Method) meta.HIDDEN_VMTARGET.getHiddenObject(mname);
if (m == null) {
throw meta.throwExceptionWithMessage(meta.java_lang_InternalError, "uninitialized StackFrameInfo !");
}
int bci = meta.java_lang_StackFrameInfo_bci.getInt(info);
fillInElement(element, new VM.StackElement(m, bci), getMeta().java_lang_Class_getName);
}
use of com.oracle.truffle.espresso.impl.Method 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.Method in project graal by oracle.
the class VM method JVM_GetStackTraceElement.
@VmImpl(isJni = true)
@JavaType(StackTraceElement.class)
public StaticObject JVM_GetStackTraceElement(@JavaType(Throwable.class) StaticObject self, int index, @Inject SubstitutionProfiler profiler) {
Meta meta = getMeta();
if (index < 0) {
profiler.profile(0);
throw meta.throwException(meta.java_lang_IndexOutOfBoundsException);
}
StaticObject ste = meta.java_lang_StackTraceElement.allocateInstance();
StackTrace frames = EspressoException.getFrames(self, meta);
if (frames == null || index >= frames.size) {
profiler.profile(1);
throw meta.throwException(meta.java_lang_IndexOutOfBoundsException);
}
StackElement stackElement = frames.trace[index];
Method method = stackElement.getMethod();
if (method == null) {
return StaticObject.NULL;
}
int bci = stackElement.getBCI();
getMeta().java_lang_StackTraceElement_init.invokeDirect(/* this */
ste, /* declaringClass */
meta.toGuestString(MetaUtil.internalNameToJava(method.getDeclaringKlass().getType().toString(), true, true)), /* methodName */
meta.toGuestString(method.getName()), /* fileName */
meta.toGuestString(method.getSourceFile()), /* lineNumber */
method.bciToLineNumber(bci));
return ste;
}
use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.
the class VM method getMethodFromFrame.
@TruffleBoundary
public Method getMethodFromFrame(FrameInstance frameInstance) {
EspressoRootNode root = getRawEspressoRootFromFrame(frameInstance);
if (root == null) {
return null;
}
Method method = root.getMethod();
if (method.getContext() != getContext()) {
return null;
}
return method;
}
Aggregations