Is it possible to call a native CPP function using JNI which takes generic arguments? Something like the following:
public static native <T, U, V> T foo(U u, V v);
And then call it like:
//class Foo, class Bar, class Baz are already defined;
Foo f = foo(new Bar(), new Baz());
Can anyone please provide me with a sample which is actually doing this or some tutorial on the net which does this? I am asking because in my CPP JNI function (called by JVM), I get unsatisfied link error.
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 have tried by modifying the code but now I am getting 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 has no support for generics or am I missing something?
Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.
JNI provides functions for accessing the contents of array objects. While arrays of objects must be accessed one entry at a time, arrays of primitives can be read and written directly as if they were declared in C.
Introduction to Java Native Interface: Establishing a bridge between Java and C/C++ JNI (Java Native Interface) is a foreign function interface that allows code running on JVM to call (or be called by) native applications. Using JNI, one can call methods written in C/C++ or even access assembly language.
typedef jobject jclass; In C++, JNI introduces a set of dummy classes to enforce the subtyping relationship. For example: class _jobject {}; class _jclass : public _jobject {}; ...
In general you should always use javap -s to get the signatures of methods you are going to be looking for in JNI. Don't guess.
There are numerous questions regarding type erasure on stack overflow (e.g. Get generic type of java.util.List), what you're looking to do is neither possible with JNI nor Java itself. The runtime type signature of foo is (in both worlds, or actually, there is only one world) Object foo(Object u, Object v)
, which will do an implicit class cast on the return value to whatever type you call it with.
As you might notice (and as mentioned in the comment to your question), there's no way for you to know what type T
is.
EDIT:
By the way, the getFoo method is supposed to return 'Foo', so shouldn't you be doing
jmethodID getFooMethod = env->GetMethodID(bar_class, "getFoo", "()LFoo;");
Come to think of it, your entire call sequence seems out of place... you've got a foo
which is native, returning string. Now foo
looks for getFoo
in Bar
, which returns 'Foo', and returns the result of that call directly, effectively trying to return a Foo
(Foo getFoo()
according to the comment) where string is expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With