Search in sources :

Example 76 with Method

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);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) Method(com.oracle.truffle.espresso.impl.Method) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 77 with 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);
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Method(com.oracle.truffle.espresso.impl.Method)

Example 78 with Method

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;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 79 with Method

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;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 80 with Method

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;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Symbol(com.oracle.truffle.espresso.descriptors.Symbol) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method)

Aggregations

Method (com.oracle.truffle.espresso.impl.Method)91 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)57 Meta (com.oracle.truffle.espresso.meta.Meta)27 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)22 Klass (com.oracle.truffle.espresso.impl.Klass)19 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)19 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ExportMessage (com.oracle.truffle.api.library.ExportMessage)12 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)9 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)8 ArityException (com.oracle.truffle.api.interop.ArityException)8 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 ArrayList (java.util.ArrayList)8 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)7 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)5 Field (com.oracle.truffle.espresso.impl.Field)4 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)3 Signature (com.oracle.truffle.espresso.descriptors.Symbol.Signature)3