Search in sources :

Example 1 with JMethodID

use of org.graalvm.jniutils.JNI.JMethodID in project graal by oracle.

the class JNIUtil method findMethod.

static JMethodID findMethod(JNIEnv env, JClass clazz, boolean staticMethod, boolean required, String methodName, String methodSignature) {
    JMethodID result;
    try (CCharPointerHolder name = toCString(methodName);
        CCharPointerHolder sig = toCString(methodSignature)) {
        result = staticMethod ? GetStaticMethodID(env, clazz, name.get(), sig.get()) : GetMethodID(env, clazz, name.get(), sig.get());
        JNIExceptionWrapper.wrapAndThrowPendingJNIException(env, required ? ExceptionHandler.DEFAULT : ExceptionHandler.allowExceptions(NoSuchMethodError.class));
        return result;
    }
}
Also used : JMethodID(org.graalvm.jniutils.JNI.JMethodID) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)

Example 2 with JMethodID

use of org.graalvm.jniutils.JNI.JMethodID in project graal by oracle.

the class JNIUtil method getJVMCIClassLoader.

/**
 * Returns a ClassLoader used to load the compiler classes.
 */
public static JObject getJVMCIClassLoader(JNIEnv env) {
    JClass clazz;
    try (CCharPointerHolder className = CTypeConversion.toCString(JNIUtil.getBinaryName(ClassLoader.class.getName()))) {
        clazz = JNIUtil.FindClass(env, className.get());
    }
    if (clazz.isNull()) {
        throw new InternalError("No such class " + ClassLoader.class.getName());
    }
    JMethodID getClassLoaderId = findMethod(env, clazz, true, true, METHOD_GET_PLATFORM_CLASS_LOADER[0], METHOD_GET_PLATFORM_CLASS_LOADER[1]);
    if (getClassLoaderId.isNull()) {
        throw new InternalError(String.format("Cannot find method %s in class %s.", METHOD_GET_PLATFORM_CLASS_LOADER[0], ClassLoader.class.getName()));
    }
    return env.getFunctions().getCallStaticObjectMethodA().call(env, clazz, getClassLoaderId, nullPointer());
}
Also used : JMethodID(org.graalvm.jniutils.JNI.JMethodID) JClass(org.graalvm.jniutils.JNI.JClass) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)

Example 3 with JMethodID

use of org.graalvm.jniutils.JNI.JMethodID in project graal by oracle.

the class JNIUtil method findClass.

/**
 * Finds a class in HotSpot heap using a given {@code ClassLoader}.
 *
 * @param env the {@code JNIEnv}
 * @param binaryName the class binary name
 */
public static JClass findClass(JNIEnv env, JObject classLoader, String binaryName) {
    if (classLoader.isNull()) {
        throw new IllegalArgumentException("ClassLoader must be non null.");
    }
    trace(1, "%s->HS: findClass %s", getFeatureName(), binaryName);
    JMethodID findClassId = findMethod(env, JNIUtil.GetObjectClass(env, classLoader), false, false, METHOD_LOAD_CLASS[0], METHOD_LOAD_CLASS[1]);
    JValue params = StackValue.get(1, JValue.class);
    params.addressOf(0).setJObject(JNIUtil.createHSString(env, binaryName.replace('/', '.')));
    return (JClass) env.getFunctions().getCallObjectMethodA().call(env, classLoader, findClassId, params);
}
Also used : JMethodID(org.graalvm.jniutils.JNI.JMethodID) JValue(org.graalvm.jniutils.JNI.JValue) JClass(org.graalvm.jniutils.JNI.JClass)

Example 4 with JMethodID

use of org.graalvm.jniutils.JNI.JMethodID in project graal by oracle.

the class JNIUtil method getSystemClassLoader.

/**
 * Returns the {@link ClassLoader#getSystemClassLoader()}.
 */
public static JObject getSystemClassLoader(JNIEnv env) {
    JClass clazz;
    try (CCharPointerHolder className = CTypeConversion.toCString(JNIUtil.getBinaryName(ClassLoader.class.getName()))) {
        clazz = JNIUtil.FindClass(env, className.get());
    }
    if (clazz.isNull()) {
        throw new InternalError("No such class " + ClassLoader.class.getName());
    }
    JMethodID getClassLoaderId = findMethod(env, clazz, true, true, METHOD_GET_SYSTEM_CLASS_LOADER[0], METHOD_GET_SYSTEM_CLASS_LOADER[1]);
    if (getClassLoaderId.isNull()) {
        throw new InternalError(String.format("Cannot find method %s in class %s.", METHOD_GET_SYSTEM_CLASS_LOADER[0], ClassLoader.class.getName()));
    }
    return env.getFunctions().getCallStaticObjectMethodA().call(env, clazz, getClassLoaderId, nullPointer());
}
Also used : JMethodID(org.graalvm.jniutils.JNI.JMethodID) JClass(org.graalvm.jniutils.JNI.JClass) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)

Aggregations

JMethodID (org.graalvm.jniutils.JNI.JMethodID)4 JClass (org.graalvm.jniutils.JNI.JClass)3 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)3 JValue (org.graalvm.jniutils.JNI.JValue)1