Detailed description of JNI header files (3) object operations, and detailed description of jni
Java object operations in JNI:
Functions related to object operations: (AllocObject, NewObject, NewObjectA, NewObjectV, GetObjectRefType, IsInstanceOf, IsSameObject)
1. Comparison of function features. 1.1 differences between AllocObject and NewObject, NewObjectA, and NewObjectV
Difference: AllocObject does not need to call the constructor to generate a Java object. NewObject, NewObjectA, NewObjectV,
The object is generated by calling the Java class constructor.
Similarities: AllocObject, NewObject, NewObjectA, NewObjectV, and their jclass objects cannot be array-type references.
1.2 What are the differences and commonalities between NewObject, NewObjectA, and NewObjectV.
Differences: The parameters they pass to the constructor are transmitted in different forms. One is in the form of parameters (...), and the other is in the form of arrays (jvalue * args.
The third is a variable parameter of the va_list struct type to pass the parameters of the constructor.
Similarities: to call these three functions, you must first obtain the MethodID of the constructor, and then use the constructor for initialization.
1.3 GetObjectRefType function, used to obtain an object reference type. (JNI1.6 new features)
GetObjectRefType can obtain a total of four different types of reference values. It is the enumerated value defined in The JNI standard for the above four types of reference.
JNIInvalidRefType = 0, JNILocalRefType = 1, JNIGlobalRefType = 2, JNIWeakGlobalRefType = 3
JNIInvalidRefType: Invalid reference type. The GetObjectRefType (env, NULL) method returns JNIInvalidRefType.
JNILocalRefType: If the second parameter object of this method is a reference to a Local object, the return value is. JNILocalRefType. Generally, the second object can be generated through NewLocalRef.
JNIGlobalRefType: If the second parameter of this method is a Global object, the return value is. JNIGlobalRefType. Generally, the second parameter can be generated through NewGlobalRef.
JNIWeakGlobalRefType: If the second parameter is a WeakGlobal object, only JNIWeakGlobalRefType is returned,
Normally, the second parameter can be generated through NewGlobalRef.
1.4IsInstanceOf method.
The instanceof operator in java is used to indicate at runtime whether an object is an instance of a specific class.
Instanceof indicates whether the object is an instance of this particular class or its subclass by returning a Boolean value.
The isInstanceOf method is the same as the instanceof method in java.
In java, instanceof is used as follows: The instanceof method is used to determine whether to forcibly convert the type before forcing the type.
Public class {
Private A a = new ();
Public static void main (String [] args ){
If (a instanceof ){
System. out. println ("Object a is an instance of type ");
}
}
}
Next we will talk about his usage in C.
1.5IsSameObject method.
This method tests whether two references are referenced in the same java object.
1. AllocObject1.1 function prototype
JobjectRefType GetObjectRefType (JNIEnv * env, jobject obj );
1.2 function parameter env: the JNI interface pointer. (It is a JNI interface pointer .)
Clazz: a Java class object. (a java class object. 1.3 Return Value
RETURNS:
Returns a Java object, or NULL if the object cannot be constructed.
The Java object is returned successfully. If the result is unsuccessful, NULL is returned.
1.4 exceptions
THROWS: two exceptions may occur.
InstantiationException: if the class is an interface or an abstract class. This class is an abstract class or an interface.
OutOfMemoryError: if the system runs out of memory. the system memory is insufficient.
1.5 demo:
JNIEXPORT jobject JNICALL java_com_zuoshao1__mainactivity_allocobjectdemo (JNIEnv * pEnv, jobject pObj ){
Jclass testCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Test"); // obtain the Class Object of Test by class name.
Jobject allocObj = (* pEnv)-> AllocObject (pEnv, testCls); // obtain the Java object through the class object.
Return allocObj; // and return to the Java layer.
}
2. NewObject2.1 function prototype
Jobject NewObject (JNIEnv * env, jclass clazz, jmethodID methodID ,...)
2.2 function parameters
Env: the JNI interface pointer. (the JNI interface pointer .)
Clazz: a Java class object. a java class object.
MethodID: the method ID of the constructor. It is the ID of the java class constructor, which can be obtained through GetMethodID
...: Is a constructor parameter.
2.3 function return value
Returns the java object of this class.
2.4 exception 2.5 demo
JNIEXPORT jobject JNICALL java_com_zuoshao1__mainactivity_newobjectdemo
(JNIEnv * pEnv, jobject pObj, jstring pName, jint pAge ){
Jclass personCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Person"); // gets the Class Object of Person.
JmethodID method = (* pEnv)-> GetMethodID (pEnv, personCls, "<init>", "(Ljava/lang/String; I) V "); // obtain the Id of the constructor.
Jobject newObj = (* pEnv)-> NewObject (pEnv, personCls, method, pName, pAge); // use the NewObject method to call the constructor to generate a Java object.
Return newObj; // return the object to the Java layer.
}
3. NewObjectA3.1 function prototype
Jobject NewObjectA (JNIEnv * env, jclass clazz, jmethodID methodID, jvalue * args)
3.2 Function Parameters
Env: the JNI interface pointer. (the JNI interface pointer .)
Clazz: a Java class object. a java class object.
MethodID: the method ID of the constructor. It is the ID of the java class constructor, which can be obtained through GetMethodID
Args: A constructor parameter.
3.3 function return value
Returns the java object of this class.
3.4 exception 3.5 demo
JNIEXPORT jobject JNICALL java_com_zuoshao1__mainactivity_newobjectademo
(JNIEnv * pEnv, jobject pObj, jstring pName, jint pAge ){
Jclass personCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Person"); // gets the Class Object of Person.
JmethodID method = (* pEnv)-> GetMethodID (pEnv, personCls, "<init>", "(Ljava/lang/String; I) V "); // obtain the Id of the constructor.
Jvalue args [2]; // construct a parameter array of the jvalue struct type.
Args [0]. l = pName;
Args [1]. I = pAge;
Jobject newObjA = (* pEnv)-> NewObjectA (pEnv, personCls, method, args); // use the NewObject method to call the constructor to generate a Java object.
Return newObjA;
}
4. NewObjectV4.1 function prototype
Jobject NewObjectV (JNIEnv * env, jclass clazz, jmethodID methodID, va_list args)
4.2 function parameters
Env: the JNI interface pointer. (the JNI interface pointer .)
Clazz: a Java class object. a java class object.
MethodID: the method ID of the constructor. It is the ID of the java class constructor, which can be obtained through GetMethodID
Va_list: A constructor parameter.
4.3 function return value
Returns the java object of this class.
4.4 exception 4.5 demo
Static jobject newObjectVUtil (JNIEnv * pEnv ,...){
Jclass personCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Person ");
JmethodID method = (* pEnv)-> GetMethodID (pEnv, personCls, "<init>", "(Ljava/lang/String; I) V ");
Va_list arList;
Va_start (arList, pEnv );
Jobject newObjV = (* pEnv)-> NewObjectV (pEnv, persontCls, method, arList );
Va_end (arList );
Return newObjV;
}
JNIEXPORT jobject JNICALL java_com_zuoshao1__mainactivity_newobjectvdemo
(JNIEnv * pEnv, jobject pObj, jstring pName, jint pAge ){
Return newObjectVUtil (pEnv, pName, pAge );
}
5. New Features of The GetObjectRefType5.1 function prototype 1.6
JobjectRefType (JNICALL * GetObjectRefType) (JNIEnv * env, jobject obj );
5.2 function parameters
Env: the JNI interface pointer. (the JNI interface pointer .)
Obj: a local, global or weak global reference. a local, or global, or (global weak) object reference.
5.3 function return value
The Return Value of the function is an enumeration type. It indicates that only four of the enumerated values are allowed.
JNIInvalidRefType = 0, JNILocalRefType = 1, JNIGlobalRefType = 2, JNIWeakGlobalRefType = 3
GetObjectRefType (env, NULL) is obtained only. The value of JNIInvalidRefType. Others are the values of the relevant Enumeration type.
5.4 exception 5.5 demo
Jobjcet obj;
JNIEXPORT jobject JNICALL java_com_zuoshao1__mainactivity_getobjectreftypedemo {
Jclass personCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Person ");
Jclass personSubCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/PersonSub ");
JmethodID personConstrucat = (* pEnv)-> GetMethodID (pEnv, personCls, "<init>", "() V ");
JmethodID personSubConstructor = (* pEnv)-> GetMethodID (pEnv, personSubCls, "<init>", "() V ");
Jobject personObj = (* pEnv)-> NewObject (pEnv, personCls, personConstructor );
Jobject personSubObj = (* pEnv)-> NewObject (pEnv, personSubCls, personSubConstructor );
Obj = (* pEvn)-> NewGlobalRef (pEnv, personSubObj );
_ Android_log_print (ANDROID_LOG_INFO, "GetObjectRefTypeDemo", "personSubObj is: % d ",
(* PEnv)-> GetObjectRefType (pEnv, personSubObj ));
_ Android_log_print (ANDROID_LOG_INFO, "GetObjectRefTypeDemo", "obj is: % d ",
(* PEnv)-> GetObjectRefType (pEnv, obj ));
.....
}
6. IsInstanceOf6.1 function prototype
Jboolean IsInstanceOf (JNIEnv * env, jobject obj, jclass clazz );
6.2 function parameters
Env: the JNI interface pointer. JNI interface pointer.
Obj: a Java object. java object, and instanceof in java is a function.
Clazz: a Java class object. a class object.
6.3 function return value
If Java obj is a clazz object. JNI_TRUE is returned; otherwise, JNI_FALSE is returned.
JNI_TRUE
JNI_FALSE
6.4 exception 6.5 demo
In Java, a Person class is used as follows.
Public class Person {}
PersonSub inherits the following PersonSub class.
Public class PersonSub extends Person {}
The following uses IsInstanceOf to determine whether an object is a class instance. It is the same as the instanceof in the java layer.
Jclass personCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Person ");
Jclass personSubCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/PersonSub ");
JmethodID personConstrucat = (* pEnv)-> GetMethodID (pEnv, personCls, "<init>", "() V ");
JmethodID personSubConstructor = (* pEnv)-> GetMethodID (pEnv, personSubCls, "<init>", "() V ");
Jobject personObj = (* pEnv)-> NewObject (pEnv, personCls, personConstructor );
Jobject personSubObj = (* pEnv)-> NewObject (pEnv, personSubCls, personSubConstructor );
_ Android_log_print (ANDROID_LOG_INFO, "IsInstanceOfDemo", "personObj, perosnCls: % d", (* pEnv)-> IsInstanceOf (pEnv, personObj, personCls ));
_ Android_log_print (ANDROID_LOG_INFO, "IsInstanceOfDemo", "personObj, personSubCls: % d", (* pEnv)-> IsInstanceOf (pEnv, personObj, personSubCls ));
_ Android_log_print (ANDROID_LOG_INFO, "IsInstanceOfDemo", "personSubObj, personCls: % d", (* pEnv)-> IsInstanceOf (pEnv, personSubObj, personCls ));
_ Android_log_print (ANDROID_LOG_INFO, "IsInstanceOfDemo", "personSubObj, personSubCls: % d", (* pEnv)-> IsInstanceOf (pEnv, personSubObj, personSubCls ));
7. IsSameObject 7.1 function prototype
Jboolean IsSameObject (JNIEnv * env, jobject ref1, jobject ref2 );
7.2 function parameters
Env: the JNI interface pointer.
Ref1: a Java object.
Ref2: a Java object.
7.3 function return value
If ref1, and ref2 reference the same object. The return value is true. Otherwise, the returned value is false.
JNI_TRUE
JNI_FALSE
7.4 exception 7.5 demo
Jclass personCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/Person ");
Jclass personSubCls = (* pEnv)-> FindClass (pEnv, "com/zuoshaohua/PersonSub ");
JmethodID personConstrucat = (* pEnv)-> GetMethodID (pEnv, personCls, "<init>", "() V ");
JmethodID personSubConstructor = (* pEnv)-> GetMethodID (pEnv, personSubCls, "<init>", "() V ");
Jobject personObj = (* pEnv)-> NewObject (pEnv, personCls, personConstructor );
Jobject personSubObj = (* pEnv)-> NewObject (pEnv, personSubCls, personSubConstructor );
_ Android_log_print (ANDROID_LOG_INFO, "IsInstanceOfDemo", "personObj, personSubObj: % d ",
(* PEnv)-> IsSameObject (pEnv, personObj, personSubObj ));