use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.
the class JniEnv method GetStaticMethodID.
/**
* <h3>jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char
* *sig);</h3>
* <p>
* Returns the method ID for a static method of a class. The method is specified by its name and
* signature.
* <p>
* GetStaticMethodID() causes an uninitialized class to be initialized.
*
* @param clazz a Java class object.
* @param namePtr the static method name in a 0-terminated modified UTF-8 string.
* @param signaturePtr the method signature in a 0-terminated modified UTF-8 string.
* @return a method ID, or NULL if the operation fails.
* @throws NoSuchMethodError if the specified static method cannot be found. *
* @throws ExceptionInInitializerError if the class initializer fails due to an exception.
* @throws OutOfMemoryError if the system runs out of memory.
*/
@JniImpl
@Handle(Method.class)
public long GetStaticMethodID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject signaturePtr) {
String name = NativeUtils.interopPointerToString(namePtr);
String signature = NativeUtils.interopPointerToString(signaturePtr);
assert name != null && signature != null;
Method method = null;
Symbol<Name> methodName = getNames().lookup(name);
if (methodName != null) {
Symbol<Signature> methodSignature = getSignatures().lookupValidSignature(signature);
if (methodSignature != null) {
// Throw a NoSuchMethodError exception if we have an instance of a
// primitive java.lang.Class
Klass klass = clazz.getMirrorKlass();
if (klass.isPrimitive()) {
Meta meta = getMeta();
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
}
klass.safeInitialize();
// Lookup only if name and type are known symbols.
if (Name._clinit_.equals(methodName)) {
// Never search superclasses for static initializers.
method = klass.lookupDeclaredMethod(methodName, methodSignature);
} else {
method = klass.lookupMethod(methodName, methodSignature);
}
}
}
if (method == null || !method.isStatic()) {
Meta meta = getMeta();
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
}
return methodIds.handlify(method);
}
use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.
the class JniEnv method CallStaticBooleanMethodVarargs.
@JniImpl
public boolean CallStaticBooleanMethodVarargs(@JavaType(Class.class) StaticObject clazz, @Handle(Method.class) long methodId, @Pointer TruffleObject varargsPtr) {
Method method = methodIds.getObject(methodId);
assert method.isStatic();
assert (clazz.getMirrorKlass()) == method.getDeclaringKlass();
Object result = method.invokeDirect(null, popVarArgs(varargsPtr, method.getParsedSignature()));
return getMeta().asBoolean(result, true);
}
use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.
the class Management method GetPeakMemoryPoolUsage.
@ManagementImpl
@JavaType(Object.class)
public StaticObject GetPeakMemoryPoolUsage(@JavaType(Object.class) StaticObject pool) {
if (StaticObject.isNull(pool)) {
return StaticObject.NULL;
}
Method init = getMeta().java_lang_management_MemoryUsage.lookupDeclaredMethod(Symbol.Name._init_, getSignatures().makeRaw(Symbol.Type._void, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long));
StaticObject instance = getMeta().java_lang_management_MemoryUsage.allocateInstance();
init.invokeDirect(instance, 0L, 0L, 0L, 0L);
return instance;
}
use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.
the class Management method GetMemoryPoolUsage.
@ManagementImpl
@JavaType(Object.class)
public StaticObject GetMemoryPoolUsage(@JavaType(Object.class) StaticObject pool) {
if (StaticObject.isNull(pool)) {
return StaticObject.NULL;
}
Method init = getMeta().java_lang_management_MemoryUsage.lookupDeclaredMethod(Symbol.Name._init_, getSignatures().makeRaw(Symbol.Type._void, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long));
StaticObject instance = getMeta().java_lang_management_MemoryUsage.allocateInstance();
init.invokeDirect(instance, 0L, 0L, 0L, 0L);
return instance;
}
use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.
the class Target_sun_reflect_NativeMethodAccessorImpl method callMethodReflectively.
@JavaType(Object.class)
public static StaticObject callMethodReflectively(Meta meta, @JavaType(Object.class) StaticObject receiver, @JavaType(Object[].class) StaticObject args, Method m, Klass klass, @JavaType(Class[].class) StaticObject parameterTypes) {
// Klass should be initialized if method is static, and could be delayed until method
// invocation, according to specs. However, JCK tests that it is indeed always initialized
// before doing anything, even if the method to be invoked is from another class.
klass.safeInitialize();
Method reflectedMethod = m;
if (reflectedMethod.isRemovedByRedefition()) {
reflectedMethod = m.getContext().getClassRedefinition().handleRemovedMethod(reflectedMethod, reflectedMethod.isStatic() ? reflectedMethod.getDeclaringKlass() : receiver.getKlass());
}
// actual method to invoke
Method method;
// target klass, receiver's klass for non-static
Klass targetKlass;
if (reflectedMethod.isStatic()) {
// Ignore receiver argument;.
method = reflectedMethod;
targetKlass = klass;
} else {
if (StaticObject.isNull(receiver)) {
throw meta.throwNullPointerException();
}
// Check class of receiver against class declaring method.
if (!klass.isAssignableFrom(receiver.getKlass())) {
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "object is not an instance of declaring class");
}
// target klass is receiver's klass
targetKlass = receiver.getKlass();
// no need to resolve if method is private or <init>
if (reflectedMethod.isPrivate() || Name._init_.equals(reflectedMethod.getName())) {
method = reflectedMethod;
} else {
// resolve based on the receiver
if (reflectedMethod.getDeclaringKlass().isInterface()) {
// resolve interface call
// Match resolution errors with those thrown due to reflection inlining
// Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
method = reflectedMethod;
assert targetKlass instanceof ObjectKlass;
method = ((ObjectKlass) targetKlass).itableLookup(method.getDeclaringKlass(), method.getITableIndex());
if (method != null) {
// Check for abstract methods as well
if (!method.hasCode()) {
// new default: 65315
throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
}
}
} else {
// if the method can be overridden, we resolve using the vtable index.
method = reflectedMethod;
// VTable is live, use it
method = targetKlass.vtableLookup(method.getVTableIndex());
if (method != null) {
// Check for abstract methods as well
if (method.isAbstract()) {
// new default: 65315
throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, Meta.initException(meta.java_lang_AbstractMethodError));
}
}
}
}
}
// an internal vtable bug. If you ever get this please let Karen know.
if (method == null) {
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, "please let Karen know");
}
int argsLen = StaticObject.isNull(args) ? 0 : args.length();
final Symbol<Type>[] signature = method.getParsedSignature();
// Check number of arguments.
if (Signatures.parameterCount(signature, false) != argsLen) {
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "wrong number of arguments!");
}
Object[] adjustedArgs = new Object[argsLen];
for (int i = 0; i < argsLen; ++i) {
StaticObject arg = args.get(i);
StaticObject paramTypeMirror = parameterTypes.get(i);
Klass paramKlass = paramTypeMirror.getMirrorKlass();
// Throws guest IllegallArgumentException if the parameter cannot be casted or widened.
adjustedArgs[i] = checkAndWiden(meta, arg, paramKlass);
}
Object result;
try {
result = method.invokeDirect(receiver, adjustedArgs);
} catch (EspressoException e) {
throw meta.throwExceptionWithCause(meta.java_lang_reflect_InvocationTargetException, e.getGuestException());
}
if (reflectedMethod.getReturnKind() == JavaKind.Void) {
return StaticObject.NULL;
}
if (reflectedMethod.getReturnKind().isPrimitive()) {
return Meta.box(meta, result);
}
// Result is not void nor primitive, pass through.
return (StaticObject) result;
}
Aggregations