JNI learning 3: detailed explanation of how JNI uses Java objects

Source: Internet
Author: User
1. First, write the Java file:
public class HeaderFile {      private native void  doVoid();      native int doShort();      native void doArray(Object[] o );      native int doInt(int i);      //byte ,short ,int,long,float,double ,boolean,char            native int doInt(double d);    //byte ,short ,int,long,float,double ,boolean,char      native int doInt(Object o);          native int doInt(double d1,double d2);      static native int doInt(double d1 ,double d2,double d3);      static native int doInt(double d1 ,float f,boolean b ,char[] c );             native int doInt(int[] i);      native int doInt(int[] i1,double[] i2 );          static native int doInt(int[] i1,double[] i2 ,Object[] o );           public native String doString(String s);      public native Object doObject(Object o );      public native Enumeration doInterface(Iterator it);      public native Student doStudent(Student s);       //  native int[] doInt(int[] i);  //byte ,short ,int,long,float,double ,boolean,char      public native String[] doString(String[] s);      public native Object[] doObjects(Object[] o );      public native Enumeration[] doInterface(Iterator[] it);      public native Student[] doStudent(Student[] s);                  public native static Object doAll(int[] i , String[] s , Student[] student );               }

Java files contain private, public, protect, and other types of methods, static methods and non-static methods. The return types include basic types and objects.

Ii. Let's take a look at the generated header file:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h>  /* Header for class com_nedu_jni_helloword_HeaderFile */  #ifndef _Included_com_nedu_jni_helloword_HeaderFile  #define _Included_com_nedu_jni_helloword_HeaderFile  #ifdef __cplusplus  extern "C" {  #endif  /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doVoid   * Signature: ()V   */ JNIEXPORT void JNICALL Java_com_nedu_jni_helloword_HeaderFile_doVoid    (JNIEnv *, jobject);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doShort   * Signature: ()I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doShort    (JNIEnv *, jobject);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doArray   * Signature: ([Ljava/lang/Object;)V   */ JNIEXPORT void JNICALL Java_com_nedu_jni_helloword_HeaderFile_doArray    (JNIEnv *, jobject, jobjectArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: (I)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__I    (JNIEnv *, jobject, jint);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: (D)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__D    (JNIEnv *, jobject, jdouble);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: (Ljava/lang/Object;)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__Ljava_lang_Object_2    (JNIEnv *, jobject, jobject);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: (DD)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__DD    (JNIEnv *, jobject, jdouble, jdouble);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: (DDD)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__DDD    (JNIEnv *, jclass, jdouble, jdouble, jdouble);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: (DFZ[C)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__DFZ_3C    (JNIEnv *, jclass, jdouble, jfloat, jboolean, jcharArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: ([I)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt___3I    (JNIEnv *, jobject, jintArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: ([I[D)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt___3I_3D    (JNIEnv *, jobject, jintArray, jdoubleArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInt   * Signature: ([I[D[Ljava/lang/Object;)I   */ JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt___3I_3D_3Ljava_lang_Object_2    (JNIEnv *, jclass, jintArray, jdoubleArray, jobjectArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doString   * Signature: (Ljava/lang/String;)Ljava/lang/String;   */ JNIEXPORT jstring JNICALL Java_com_nedu_jni_helloword_HeaderFile_doString__Ljava_lang_String_2    (JNIEnv *, jobject, jstring);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doObject   * Signature: (Ljava/lang/Object;)Ljava/lang/Object;   */ JNIEXPORT jobject JNICALL Java_com_nedu_jni_helloword_HeaderFile_doObject    (JNIEnv *, jobject, jobject);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInterface   * Signature: (Ljava/util/Iterator;)Ljava/util/Enumeration;   */ JNIEXPORT jobject JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInterface__Ljava_util_Iterator_2    (JNIEnv *, jobject, jobject);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doStudent   * Signature: (Lcom/nedu/jni/helloword/Student;)Lcom/nedu/jni/helloword/Student;   */ JNIEXPORT jobject JNICALL Java_com_nedu_jni_helloword_HeaderFile_doStudent__Lcom_nedu_jni_helloword_Student_2    (JNIEnv *, jobject, jobject);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doString   * Signature: ([Ljava/lang/String;)[Ljava/lang/String;   */ JNIEXPORT jobjectArray JNICALL Java_com_nedu_jni_helloword_HeaderFile_doString___3Ljava_lang_String_2    (JNIEnv *, jobject, jobjectArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doObjects   * Signature: ([Ljava/lang/Object;)[Ljava/lang/Object;   */ JNIEXPORT jobjectArray JNICALL Java_com_nedu_jni_helloword_HeaderFile_doObjects    (JNIEnv *, jobject, jobjectArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doInterface   * Signature: ([Ljava/util/Iterator;)[Ljava/util/Enumeration;   */ JNIEXPORT jobjectArray JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInterface___3Ljava_util_Iterator_2    (JNIEnv *, jobject, jobjectArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doStudent   * Signature: ([Lcom/nedu/jni/helloword/Student;)[Lcom/nedu/jni/helloword/Student;   */ JNIEXPORT jobjectArray JNICALL Java_com_nedu_jni_helloword_HeaderFile_doStudent___3Lcom_nedu_jni_helloword_Student_2    (JNIEnv *, jobject, jobjectArray);   /*   * Class:     com_nedu_jni_helloword_HeaderFile   * Method:    doAll   * Signature: ([I[Ljava/lang/String;[Lcom/nedu/jni/helloword/Student;)Ljava/lang/Object;   */ JNIEXPORT jobject JNICALL Java_com_nedu_jni_helloword_HeaderFile_doAll    (JNIEnv *, jclass, jintArray, jobjectArray, jobjectArray);   #ifdef __cplusplus  }  #endif  #endif
Iii. Analysis of header files:

1. The first nine lines of the file are needless to say. They are the headers of C and C ++ and should be well understood.

2. Comments of a method. Each method has its comments. These are similar. For an analysis:

 
  1. /*  
  2.  * Class:     com_nedu_jni_helloword_HeaderFile  
  3.  * Method:    doVoid  
  4.  * Signature: ()V  
  5.  */ 

The comments are divided into three parts: Class, method, and signature.

Class: The Class Name of the native method.

Method: indicates the method name.

Signature: A method identifier. It is mainly used by JNI to operate Java objects.

Signature is generally composed of two parts: a method parameter and a return type. The method parameters are in brackets and the return type is behind them,

For example

 
  1. () V returns void with no parameter.
  2. (DFZ [c) I returns int, and the parameters are double, float, and char []
  3. (Ljava/lang/string;) ljava/lang/string; return string, the parameter is string

If you do not know the meaning of the characters, you cannot understand the meaning. The character pairs should be of the basic type, object type, and array type. The analysis is as follows:

1) the correspondence between basic types is as follows:

2) When the method parameter or return value is an object in Java, the path must be added with "L". However, the path must be separated with "/", and custom objects also use this rule, when not in the package, add the class name "L. For example, java. Lang. String is "Java/lang/string", Com. nedu. JNI. helloword. Student is "com/nedu/JNI/helloword/student"

3) when the method parameter or return value is an array type, add [, for example, [I represents int [], [[D represents
Double [] [] [], that is, a few [.

Take a look at the example:

3. method declaration

 
  1. JNIEXPORT void JNICALL Java_com_nedu_jni_helloword_HeaderFile_doArray(JNIEnv *,jobject,jobjectArray); 

From the header file, we can see that the method consists of seven parts.

1 and 3 are all JNI keywords, indicating that this function is called by JNI.

2. Return TYPE OF THE METHOD

4. Identify the method in JNI from the Java ID Header

5. Package name + Class Name of the class where the method is located

6. Method Name

7. Parameters. They share a common feature, including jnienv
*-It is an interface pointer used to locate functions in the function table!


The JNI specification is generally called "interface pointer ". It seems like process calling! Yes, in JNI


The operation process is process-oriented! The following jobject is a pointer to this class, similar to this in C. This


The second parameter is changed. When this method is a class instance method, this parameter is jobject. When this method is a class method (that is, a static method)


This parameter is jclass and points to the class of this class.

 

The header files generated based on different methods are compared as follows:

1. Comparison between static and non-static:

 
  1. /*  
  2.  * Class:     com_nedu_jni_helloword_HeaderFile  
  3.  * Method:    doInt  
  4.  * Signature: (DD)I  
  5.  */ 
  6. JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__DD  
  7.   (JNIEnv *, <span style="background-color: rgb(255, 0, 0);">jobject</span>, jdouble, jdouble);  
  8.  
  9. /*  
  10.  * Class:     com_nedu_jni_helloword_HeaderFile  
  11.  * Method:    doInt  
  12.  * Signature: (DDD)I  
  13.  */ 
  14. JNIEXPORT jint JNICALL Java_com_nedu_jni_helloword_HeaderFile_doInt__DDD  
  15.   (JNIEnv *, <span style="color:#000000;background-color: rgb(255, 0, 0);">jclass</span>, jdouble, jdouble, jdouble); 

The first is a non-static method, and the second is a static method. The difference is marked as red. The differences will be mentioned later.

2. methods such as private, friendly, protected, and public are not included in the header file of JNI. These access modifiers are only available in other classes.

Valid when using these methods! JNI does not care about this modifier!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.