简体   繁体   中英

jni native function overload signature

I am trying to use function overload when declaring JNI native functions.

The Java method is :

public native static void methodaaa(String type, int errorCode);
public native static void methodaaa(String type, byte[] byts);

Without overload, the code is shown as below:

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

And this works just fine.

Then I tried to add overload :

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}

And this give me the error :

conflicting types for Java_com_xxx_yyy_JavaCallCpp_methodaaa

Then I did some research and it seems like I need to add a "__" to the end of the functions that I want to overload and also append the arguments Name mangling.

So I tried:

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_I(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_B(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}

But it still not work, the error is :

No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V

Is anybody know that how to write the JNICALL function name with a jstring as parameter or what I am doing wrong here?

Any advice will be appreciated, thanks :)

Update:

I found the link here :

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html

and then tried to modify my code :

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_2I(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_2B(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}

But, still I am getting the same error :

No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V

Don't attempt to figure out JNI method signatures yourself. Use the output of javah . It is never wrong.

https://edux.pjwstk.edu.pl/mat/268/lec/lect10/lecture10.html

maybe this will help you

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class getter_number_GetNumber */

    #ifndef _Included_getter_number_GetNumber
    #define _Included_getter_number_GetNumber
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     getter_number_GetNumber
     * Method:    getNumber
     * Signature: ()I
     */
    JNIEXPORT jint JNICALL Java_getter_number_GetNumber_getNumber__
      (JNIEnv *, jobject);

    /*
     * Class:     getter_number_GetNumber
     * Method:    getNumber
     * Signature: (J)J
     */
    JNIEXPORT jlong JNICALL Java_getter_number_GetNumber_getNumber__J
      (JNIEnv *, jobject, jlong);

    /*
     * Class:     getter_number_GetNumber
     * Method:    getNumber
     * Signature: (FF)F
     */
   JNIEXPORT jfloat JNICALL Java_getter_number_GetNumber_getNumber__FF
      (JNIEnv *, jobject, jfloat, jfloat);

    #ifdef __cplusplus
    }
    #endif
    #endif

Field descriptors of the primitive types are presented in the table.

Java type   Field descriptor
boolean Z
byte    B
char    C
short   S
int     I
long    J
float   F
double  D

Additionally, JNI function names are C, not C++. They can not be overloaded.

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