The previous article is a simple application that shows how JNI works, and this article is mainly about the description of the parameters of the local method SayHello, and the use of the methods
First, consider the implementation of the SayHello method in C + +:
[CPP]View Plaincopy
- Jniexport void Jnicall Java_com_jni_demo_jnidemo_sayhello (jnienv * env, jobject obj)
- {
- cout<<"Hello World" <<endl;
- }
For the explanation of the jnienv* env parameter in this method parameter:
The jnienv type actually represents the Java environment, and with this jnienv* pointer, the Java-side code can be manipulated. For example, create a JAA class in a Russian object, invoke a method of a Java object, get properties in a Java object, and so on. The jnienv pointer is passed to the implementation function of the local method to manipulate the Java-side code.
There are many functions in the JNIEnv class that can be used:
NewObject: Creating an object in a Java class
NewString: Creating a String object in a Java class
New<type>array: Creating an Array object of type
Get<type>field: Get a field of type
Set<type>field: Setting the value of a field of type
Getstatic<type>field: Gets the static field of type
Setstatic<type>field: Setting the value of a static field of type
Call<type>method: Call a method that returns type
Callstatic<type>method: Call a static method that returns a value of type
For many functions, you can see the function names in the jni.h file.
Parameter: Jobject obj explanation:
If the native method is not static, this obj represents the class instance of the native method
If the native method is static, this obj represents the class object instance of the native method (the static method does not require the class instance, so it represents the class object of the classes)
Here's a look at the basic types of mappings between Java and C + +:
Specific instructions to view the jni.h file.
In order to be able to use Java classes in C + +, the jni.h header file specifically defines the Jclass type to represent class classes in Java
There are several simple functions in the JNIEnv class that can obtain Jclass:
Jclass findclass (const char* clsname): By the name of the class (the full name of the class, this time the package name is not used.), but is used/to differentiate) to get Jclass
such as: Jclass str = env->findclass ("java/lang/string"); Gets the class object of the String object in Java.
Jclass getobjectclass (Jobject obj): Obtains jclass through an object instance, equivalent to the GetClass method in Java
Jclass getsuperclass (Jclass obj): Jclass object whose parent class can be obtained through Jclass
A common application of accessing Java-side code in the C + + native code is to get the properties of the class and methods of invoking the class, in order to represent the properties and methods in C/s + +, JNI defines the jfieldid in the jni.h header file. Jmethodid types to represent the properties and methods of the Java side, respectively
When we are accessing, or setting Java properties, we first get the jfieldid representing the Java property in the local code before we can do Java property operations in the local code, and again, when we need to call the Java side of the method, It is also necessary to obtain a Jmethodid representing the method in order to make a Java method call
Using the jnienv:
Getfieldid/getmethodid
Getstaticfieldid/getstaticmethodid
To obtain the corresponding Jfieldid and Jmethodid
Here are some specific ways to look at these:
Getfieldid (jclass clazz,const char* name,const char* sign)
Parameter description of the method:
Clazz: This is simply the class object that this method relies on.
Name: This is the title of this field
Sign: This is the signature of this field (we know each variable, each method is signed)
How to view the signatures of fields and methods in a class:
Use the JAVAP command:
Getmethodid is also able to get the jmethodid of the constructor, which can invoke the specified constructor when creating a Java object, which is explained later:
such as: Env->getmethodid (Data_class, "<init>", "() V");
Here's a look at the signature format:
Here's a look at the example:
There is no code here: The actual presentation of the time to wait for the next article, this is just to say the use of the method:
Original: http://blog.csdn.net/jiangwei0910410003/article/details/17465457
A detailed description of the use of JNI in Java Article II: Interpretation of jnienv types and Jobject types