jni.h header file analysis 3. Other data structures

Keywords: Java Attribute jvm

Article directory

Property ID, method ID structure

//java attribute ID structure
struct _jfieldID;                       /* opaque structure Opaque, i.e. not specifically implemented in this header file */
typedef struct _jfieldID* jfieldID;     /* field IDs */

//java method ID structure
struct _jmethodID;                      /* opaque structure Opaque, i.e. not specifically implemented in this header file  */
typedef struct _jmethodID* jmethodID;   /* method IDs */

Field descriptor in local interface

/*
 * JavaNative Interface FieldDescriptors  
 * 
 * jvalue With union declaration, jvalue can represent all data types in java
 *
 * Used in some functions in JNIEnv under jni.h that end in uppercase · MethodA · to indicate the type of parameter field
 */
typedef union jvalue {
    jboolean    z;
    jbyte       b;
    jchar       c;
    jshort      s;
    jint        i;
    jlong       j;
    jfloat      f;
    jdouble     d;
    jobject     l;
} jvalue;

Reference types in jni memory management

/*
 * jni Reference type, used to manage memory in jni environment
 *  Local Reference It only exists when the native method is executed. When the native method is executed, it will automatically become invalid,
 *    So it can be released without strict compliance
 *  JNIGlobalRefType The global reference, when executed by gc, will not be actively recycled and needs to be released manually, that is, call deleteGlobalRef
 * 			 Improper release of global references may cause memory leaks
 *  WeakGlobalRefType Weak full written reference is the same as the global one in use, but it will be recycled in gc; judgment is needed in use
 * 								weakObj==NULL
 */
typedef enum jobjectRefType {
    JNIInvalidRefType = 0,
    JNILocalRefType = 1,
    JNIGlobalRefType = 2,
    JNIWeakGlobalRefType = 3
} jobjectRefType;

Local method structure

typedef struct {
    const char* name;
    const char* signature; //Method signature, as seen by the javap command 
    void*       fnPtr;
} JNINativeMethod;  //Local method structure

JNIEnv and JavaVM

There are different structure definitions for c and c + +; finally, it is renamed as JNIEnv and JavaVM through typedef

struct JNIInvokeInterface; //The structure of jni calling interface is declared here; various jvm related operation functions are defined internally

struct _JNIEnv; //C + +: jnienv is similar to JNINativeInterface implementation
struct _JavaVM; //C + +: Java VM is similar to JNIInvokeInterface implementation
typedef const struct JNINativeInterface* C_JNIEnv;

#if defined(__cplusplus) //c++
typedef _JNIEnv JNIEnv; 
typedef _JavaVM JavaVM; 
#else //c
typedef const struct JNINativeInterface* JNIEnv;
typedef const struct  JNIInvokeInterface* JavaVM;
#endif

In c + +, JNIEnv and JavaVM refer to a struct;
c + +, the function in the structure uses this pointer, which refers to the pointer of JNIEnv and JavaVM;
To sum up, you can: env - > method (args )
.
In c, refers to the pointer type of (jnininativeinterface * and JNIInvokeInterface *);
In the structure function, JNIEnv or JavaVM variables need to be passed;
This variable is equivalent to the second level pointer of JNINativeInterface or JNIInvokeInterface
To sum up, in the jni function, you can: (* Env) - > method (Env, args )

Posted by jeger003 on Thu, 28 Nov 2019 08:21:46 -0800