4

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?

MM.
  • 4,224
  • 5
  • 37
  • 74

3 Answers3

4

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.

3

javah seems to ignore generics:

JNIEXPORT void JNICALL Java_Base_nativeDoSomething
   (JNIEnv *, jobject);
Eugene
  • 9,242
  • 2
  • 30
  • 29
0

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • As mentioned above, javah appears to ignore generics. – MM. Sep 06 '13 at 17:52
  • @MM So the implication is therefore that generics do not form part of native method signatures, which is what the other answers here also say. – user207421 Jul 18 '16 at 21:06