Access domain for ndk development and ndk Development

Source: Internet
Author: User

Access domain for ndk development and ndk Development

Java has two types of domains: instance domain and static domain. Each instance of a class has its own copy of the Instance domain, and all instances of a class share a static domain (based on Java SE ).

JNI provides functions to access these two types of domains. The general steps are as follows:

1. Get the class through Object Reference
2. Obtain the domain ID through the class
3. Obtain the domain ID

Next, we will proceed with the following steps:
Our Java class has two domains, one static domain and one instance domain:

    private static String staticFiled = "a static Filed465";    private String instanceFiled = "a instance Filed123";

Perform the following three steps to access the domain:

1. Obtain a class through Object Reference

Jclass clazz; // define the domain ID jfieldID instanceFieldId; jfieldID staticFieldId; // define the domain jstring instanceFieldStr; jstring staticFieldStr; // obtain the class clazz = (* env) through Object Reference) -> GetObjectClass (env, thiz );

2. Obtain the domain ID through the class

// Obtain the domain ID instanceFieldId = (* env) through the class-> GetFieldID (env, clazz, "instanceFiled", "Ljava/lang/String ;"); staticFieldId = (* env)-> GetStaticFieldID (env, clazz, "staticFiled", "Ljava/lang/String ;");

The last parameter of the two functions is the domain descriptor that represents the domain type in Java,"Ljava/lang/String;"Indicates that the field type is String.
In general, in order to improve the application performance, we can cache the domain ID. Generally, the most frequently used domain ID is always cached.

3. Obtain the domain ID

// Obtain the domain instanceFieldStr = (* env)-> GetObjectField (env, thiz, instanceFieldId); staticFieldStr = (* env)-> GetStaticObjectField (env, clazz, staticFieldId );

To prove that we have successfully obtained the domain, convert the jstring into a c string and print it out. (For the printing log, see the log printing of NDK development ):

    const jbyte* str1;    const jbyte* str2;    str1 = (*env)->GetStringUTFChars(env,instanceFieldStr,0);    str2 = (*env)->GetStringUTFChars(env,staticFieldStr,0);    LOGI("the string is :%s",str1);    LOGI("the string is :%s",str2);

Print result:

The complete program is as follows:

Void Merge (JNIEnv * env, jobject thiz) {jclass clazz; // define the domain ID jfieldID struct; jfieldID staticFieldId; // define the domain jstring instanceFieldStr; jstring staticFieldStr; // obtain the class clazz = (* env) through object reference-> GetObjectClass (env, thiz); // obtain the domain ID instanceFieldId = (* env) through the class) -> GetFieldID (env, clazz, "instanceFiled", "Ljava/lang/String;"); staticFieldId = (* env)-> GetStaticFieldID (env, clazz, "staticFiled ", "Ljava/lang/String;"); // obtain the instanceFieldStr = (* env)-> GetObjectField (env, thiz, instanceFieldId); staticFieldStr = (* env) through the domain ID) -> GetStaticObjectField (env, clazz, staticFieldId); // convert jstring to a C string and print it out. const jbyte * str1; const jbyte * str2; str1 = (* env) -> GetStringUTFChars (env, instanceFieldStr, 0); str2 = (* env)-> GetStringUTFChars (env, staticFieldStr, 0); LOGI ("the string is: % s ", str1); LOGI ("the string is: % s", str2 );}
Summary

As you can see, it takes three steps to obtain a single domain, which is too troublesome. This often affects the program performance. Therefore, we recommend that you use a domain in the native method, it is best to pass these fields as parameters to native methods instead of returning native code to Java.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. I am very grateful if you have any mistakes.

Related Article

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.