Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 702
MM. Avatar asked Mar 22 '23 12:03

MM.


2 Answers

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.

like image 87
JoaaoVerona Avatar answered Apr 24 '23 22:04

JoaaoVerona


javah seems to ignore generics:

JNIEXPORT void JNICALL Java_Base_nativeDoSomething
   (JNIEnv *, jobject);
like image 28
Eugene Avatar answered Apr 24 '23 22:04

Eugene