Introduction to JNI Primer

Source: Internet
Author: User

(Conversion of jstring and char*)jniexport void Jnicall Java_command_cmd_command(JNIENV * env, jobject o, jstring command)
{
Char* T;
t = (char*) env->getstringutfchars (command,0);
System (t);
}


-----------------------------------------------------------------------------------

Java Basics--jni Introduction (bottom)
3. Java type and local type correspondence
  
A conversion between types is used when a reference to a Java object needs to be applied to a local method in the following cases:
  
1) Inside the Java method, the parameter is passed to the local method;
  
2) Create a Java object inside the local method;
  
3) Return the result to the Java program in the local method.
  
There are two types of cases:
  
Java Primitive Type
  
The original types, such as booleans, integers, floats, etc., that are uploaded from the Java program to the local method can be used directly, and the following are the corresponding types in the original and local methods in Java:
  
Java type Local type byte (bit)
  
Boolean Jboolean 8, unsigned
BYTE Jbyte 8
Char Jchar, unsigned
Short Jshort 16
int Jint 32
Long Jlong 64
Float Jfloat 32
Double Jdouble 64
void void N/A
  
That is, if I pass a Boolean argument in the method, then I have the Jboolean type corresponding to it in the local method. Similarly, if you return a jint in a local method, then an int type is returned in Java.
  
Java objects
  
Java objects are passed as references to local methods, and all references to these Java objects have a common parent type of jobject (equivalent to the object class in Java being the parent class of all classes). Here are some jobject subclasses of the JNI implementation:
  
4. Accessing the contents of a Java program in a local method
  
1) Access the string object:
  
A string object passed from a Java program corresponds to the jstring type in the local method, and the jstring type differs from the char* in C, so if you use it directly as char*, you will get an error. Therefore, it is necessary to convert the jstring to char* in C/A + +, using the JNIEnv method. Here is an example:
  
Code Listing 3:
  
Jniexport jstring Jnicall Java_prompt_getline
(jnienv *env, jobject obj, jstring prompt)
{
Char buf[128];
const char *STR = (*env)->getstringutfchars (env, prompt, 0);
printf ("%s", str);
(*env)->releasestringutfchars (env, prompt, str);
  
Here, the Getstringutfchars method is used to convert the incoming prompt (jstring type) into the UTF-8 format, which can be used in the local method.
  
Note: After you have used the object you converted, you need to show the call to the Releasestringutfchars method, let the JVM release the space of the object converted to UTF-8 string, and if the call is not displayed, the object will be saved in the JVM. Will not be reclaimed by the garbage collector, so it will cause memory overflow.
  
Here are some ways to access the string:
  
Getstringutfchars convert jstring into UTF-8 format char*
  
Getstringchars convert jstring into Unicode format char*
  
Releasestringutfchars releasing pointers to char* in UTF-8 format
  
Releasestringchars releasing pointers to char* in Unicode format
  
Newstringutf creating a String object in UTF-8 format
  
NewString creating a Unicode-formatted string object
  
Getstringutflengt gets the length of the char* in UTF-8 format
  
Getstringlength gets the length of the char* in Unicode format
  
2) Access the Array object:
  
Like a string object, the Jarray object cannot be accessed directly in the local method, but instead uses some of the methods pointed to by the jnienv pointer.
  
To access an array of Java primitive types:
  
1) Get the length of the array:
  
Code Listing 4:
  
Jniexport Jint Jnicall Java_intarray_sumarray
(jnienv *env, jobject obj, Jintarray arr)
{
int i, sum = 0;
Jsize len = (*env)->getarraylength (env, arr);
  
As shown in code 4, the length of the array obtained here is not the same as the get array length in the normal C language, where a function of JNIEVN is used getarraylength.
  
2) Gets a pointer to an array element:
  
Code Listing 4:
  
Jint *body = (*env)->getintarrayelements (env, arr, 0);
  
Use the Getintarrayelements method to get a pointer to an array element of ARR, note the parameters of the function, the first is jnienv, the second is an array, and the third is the array, the third is the element that starts inside the array.
  
3) Use the pointer to remove the elements from the array
  
Code Listing 5:
  
for (i=0; i<len; i++) {
Sum + = Body[i];
}
  
There's nothing different about using the array in normal C here.
  
4) Release references to array elements
  
Code Listing 6:
  
(*env)->releaseintarrayelements (env, arr, body, 0);
  
Is the same as the reference to the release string in the action string, reminding the JVM to reclaim the reference to the ARR array element.
  
Here is an example of using an int array, as well as a Boolean, float, and other corresponding arrays.
  
Gets the corresponding relationship of the array element pointer:
  
function array type
  
Getbooleanarrayelements Boolean
Getbytearrayelements byte
Getchararrayelements Char
Getshortarrayelements Short
getintarrayelements int
Getlongarrayelements Long
getfloatarrayelements float
Getdoublearrayelements Double
  
To release the corresponding relationship of the array element pointer:
Function Array Type
Releasebooleanarrayelements Boolean
Releasebytearrayelements byte
Releasechararrayelements Char
Releaseshortarrayelements Short
releaseintarrayelements int
Releaselongarrayelements Long
releasefloatarrayelements float
Releasedoublearrayelements Double
  
Accessing an array of custom Java objects
  
The JNI provides a separate set of functions to access elements of object arrays. You can use the these functions to get and set individual object array elements.
  
Note:you cannot get all the object array elements at once.
  
Getobjectarrayelement returns the object element at a given index.
  
Setobjectarrayelement updates the object element at a given index.
  
3) Ways to access Java objects:
  
To invoke a method of a Java object in a local method:
  
①. Get the class for the Java object you need to access:
  
Jclass cls = (*env)->getobjectclass (env, obj);
  
Use the Getobjectclass method to get the jclass corresponding to obj.
  
②. Get Methodid:
  
Jmethodid mid = (*env)->getmethodid (env, CLS, "Callback", "(I) V");
  
Use the Getmethdoid method to get the methdoid of the method you want to use. The meaning of its parameters:
  
Env?? >jnienv
  
Cls?? > The first step to get the Jclass
  
"Callback"?? > The method name to invoke
  
"(I) V"?? The signature of the > method
  
③. Calling method:
  
(*env)->callvoidmethod (env, obj, Mid, depth);
  
Use the Callvoidmethod method to invoke the method. The meaning of the parameter:
  
Env?? >jnienv
  
Obj?? > Jobject through Local methods
  
Mid?? > Methodid to invoke (that is, Methodid obtained in the second step)
  
Depth?? > method Required parameters (corresponding to the requirements of the method, add the corresponding parameters)
  
Note: The Callvoidmethod method call is used here because there is no return value and if there is a return value the corresponding method is used, which is mentioned later.
  
Signature of the method
  
The signature of a method is composed of the parameters of the method and the type of the return value, following their structure:
  
"(argument-types) Return-type"
  
Where the parameter types and their corresponding values in the Java program are as follows:
  
Signature types in Java
Z Boolean
B byte
C Char
S Short
I int
J Long
F float
D Double
L Fully-qualified-class; Fully-qualified-class
  
[Type type[]
  
(arg-types) Ret-type method type
  
The signature of a method of a Java class can be obtained through the JAVAP command:
  
Javap-s-P Java class name
  
To pass arguments to the called function:
  
Usually we add the arguments that are going to be passed directly behind the Methodid, but there are other ways to pass parameters:
  
CALLVOIDMETHODV can get a variable number of lists as parameters;
  
Callvoidmethoda can obtain a union.
  
Call a static method:
  
is to change the method that calls the second and third steps to correspond:
  
Getstaticmethodid gets the ID of the corresponding static method
  
Callstaticintmethod calling a static method
  
Methods to call the superclass:
  
Use less, see for yourself. ^_^.
  
4) Access the properties of the Java object:
  
Accessing the properties of Java objects and accessing Java objects is essentially the same as simply changing the method inside the function to field (and, of course, not

Introduction to JNI Primer

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.