Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an a overridden super class method in Java through JNI?

I'm attempting to override an activity callback method with a native implementation (to hook Lua into an activity). However I've hit a snag trying to call the superclass method within JNI code, as is required for the callback.

For example, I have a class like this:

class TestActivity extends Activity {

    @Override
    protected native void onCreate(Bundle bundle);

    static {
        System.loadLibrary("test-jni")
    }
}

And I'm trying to implement TestActivity.onCreate() in C it like this:

void Java_test_TestActivity_onCreate(JNIEnv* env, jobject obj, jobject bundle)
{
    // Get class, super class, and super class method ID...
    jclass objcls = (*env)->GetObjectClass(env, obj);
    jclass sprcls = (*env)->GetSuperclass(env, objcls);
    jmethodID methid = (*env)->GetMethodID(env, sprcls, "onCreate", "(Landroid/os/Bundle;)V");

    // Call super class method...
    (*env)->CallVoidMethod(env, obj, methid, bundle);
}

However, when I try this code, TestActivity.onCreate() is called within itself instead of Activity.onCreate(), Producing this error:

JNI ERROR (app bug): local reference table overflow (max=512)

I thought a jmethodID was specific to a class and would identify the super method, however this wasn't the case.

So my question is, how do you implement this...

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
}

In JNI?

like image 665
jmbx3nu Avatar asked Jun 04 '13 22:06

jmbx3nu


People also ask

How do you call a superclass method overridden?

Invoking overridden method from sub-class : We can call parent class method in overriding method using super keyword. Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name(Constructor name must always be same as Class name).

How do you call a super class method?

If you want the superclass constructor called with specific arguments, explicitly call the superclass constructor from the subclass constructor. The call to the superclass constructor must come before any other references to the object.

How do you call the super class method in subclass?

Subclass Constructors Invocation of a superclass constructor must be the first line in the subclass constructor. super(); or: super(parameter list);

Can you override an overridden method Java?

A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.


2 Answers

You can try CallNonVirtualVoidMethod(): see http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp4581

like image 51
Alex Cohn Avatar answered Oct 21 '22 12:10

Alex Cohn


You could do this:

protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    onCreate0(bundle);
}

private native void onCreate0(Bundle bundle);
like image 37
gparyani Avatar answered Oct 21 '22 12:10

gparyani