简体   繁体   中英

How do I define the JNI method signature of a generic class?

I have a base class defined in java. I would like to call a native method like so:

public class Base<T>
{
    public void doSomething()
    {
        nativeDoSomething();
    }

    private native void nativeDoSomething();
}

My question is, how do I specify the jni method signature of a generic class?

I'm late here, but I'll add it for future references.

Generics in Java are implemented using Type Erasure , which basically is: generics exists only in compile-time : they are gone after that, not existing in run-time .

This means that, even though you can have a method like public native void blah(E genericOne, F genericTwo) , when compiled, it is actually converted to public native void blah(Object genericOne, Object genericTwo) .

This way, you don't need to even care about generics when using the Java Native Interface: everything is converted down to Object , so you can simply reference them as a jobject .

The same goes for the Java classes: you can simply reference them as a jclass .

javah seems to ignore generics:

JNIEXPORT void JNICALL Java_Base_nativeDoSomething
   (JNIEnv *, jobject);

With javah . Don't try to figure it out yourself when there's a tool that does it for youl.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM