Java Generics and JNI

Is it possible to call a native CPP function using a JNI that takes general arguments? Something like the following:

public static native <T, U, V> T foo(U u, V v);

And then call it like this:

//class Foo, class Bar, class Baz are already defined;
Foo f = foo(new Bar(), new Baz());

Can anyone provide me with a sample that actually does this or some kind of online tutorial that does this? I ask, because in my JPI CPP function (called by the JVM), I get an unsatisfied communication error.

The CPP code follows:

JNIEXPORT jobject JNICALL Java_Processor_process (JNIEnv *env, jclass processor_class, jobject obj1, jobject obj2)
{
    jclass bar_class = env->FindClass("Bar");
    jmethodID getFooMethod = env->GetMethodID(bar_class, "getFoo", "()Ljava/lang/Object;");
//getFoo() is defined as `public Foo getFoo();` in Bar.java
    return env->CallObjectMethod(obj1, getFooMethod);
}

EDIT:

I tried changing the code, but now I get a NoSuchMethodError:

Java Code:

public static native <U, V> String foo(U u, V v);
//...
String str = foo(new Bar(), new Baz());

CPP Code:

JNIEXPORT jstring JNICALL Java_Processor_process (JNIEnv *env, jclass processor_class, jobject obj1, jobject obj2)
{
    jclass bar_class = env->FindClass("Bar");
    jmethodID getFooMethod = env->GetMethodID(bar_class, "getFoo", "()Ljava/lang/String;");
    //getFoo() is now defined as `public String getFoo();` in Bar.java
    return env->CallObjectMethod(obj1, getFooMethod);
}

Does this mean that JNI does not support generics or am I missing something?

+5
source share
2 answers

, (, java.util.List), , , JNI Java. foo ( , , , ) Object foo(Object u, Object v), , , , .

(, ), , T.

:
, getFoo 'Foo',

jmethodID getFooMethod = env->GetMethodID(bar_class, "getFoo", "()LFoo;");

, ... foo, , . foo getFoo Bar, "Foo" , foo (Foo getFoo() ), .

+7

, javap -s , JNI. .

+8

All Articles