Panayiotis Karabassis
Panayiotis Karabassis

Reputation: 2288

JNI: getting method id for private method in superclass

I am trying to call a private method from within the native implementation of another method like this:

jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID output_mid = (*env)->GetMethodID(env, cls, "methodName", "(Ljava/lang/String;)V");
if (output_mid == 0)
{
    // Exit
}

This is giving me a "method not found" exception. From what I gather, the class object must correspond to the class where the target method was defined, because it is private.

Indeed if I change the access modifier to protected, the fault disappears. Also from the logs, I see GetObjectClass returned the runtime class, not the base class. So, my question is: how to get the correct base class, in JNI?

Upvotes: 3

Views: 3815

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

There's a JNI function getSuperClass() that you can use to obtain the correct class in this case. In the general case of knowing that a private method is defined in some superclass, you might need to walk the inheritance tree checking each superclass up to the root.

Upvotes: 5

Related Questions