JNI Study Notes 4 -- create a Java object (including javastring) using local code)

Source: Internet
Author: User

Original article: http://blog.csdn.net/jiben071/archive/2010/11/26/6036305.aspx

 

(1) create a Java object in the local code of C/C ++
1. Create a Java object
(1) functions newobject can create Java objects.
(2) getmethodid can obtain the jmethodid of the constructor. if the name of the method to be acquired is set to <init>, the constructor can be obtained.
(3) The signature of the return value type of the constructor is always void.
(4) Example:
Jclass clazz_date = env-> findclass ("Java/util/date ");
Jmethodid mid_date = env-> getmethodid (clazz_date, "<init>", "() V"); // constructor ID
Jobject now = env-> newobject (clazz_date, mid_date); // create a Java object
(5) Another method --> allocobject (not commonly used)

(2) access the Java String object in the C/C ++ local code
(1) in Java, the string object used is a Chinese character or an English character, which always occupies two bytes.
(2) Java through the JNI interface can be converted to Java string in C/C ++ wide string (wchar_t *), or return a UTF-8 string (char *) to C/C ++.

In turn, C/C ++ can create a Java-side String object through a wide string or a UTF-8-encoded string
(3) Functions
Getstringchars
Getstringutfchars
These two functions are used to obtain Java strings related to a jstring object.

You can obtain the UTF-16-encoded wide string (jchar *) and UTF-8-encoded string (char *).

For example:
Const jchar * getstringchars (jstring STR, jboolean * copied)
Const char * getstringutfchars (jstirng STR, jboolean * copied)
The first parameter is used to input a jstring variable pointing to the string object in Java.
The second parameter is used to input a jboolean pointer.

Both functions have two different actions:
1. Open the new memory, copy the string in Java to the memory, and then return the pointer

Pointer to the memory address
2. directly return the pointer to the string memory in Java. Do not change this

Memory content, which breaks the principle that string is always a constant in Java.

The second parameter is used to identify whether Java's String object has been copied.
If the passed jboolean pointer is not null, it will give the memory pointed to by the pointer

Whether the jni_true or jni_false mark has been copied
If null is input, the system does not care whether to copy the string. Therefore, it does not point to

Save assignment

The strings obtained using these two functions must be used when they are not used.

Releasestringchars/releasestringutfchars to release the copied content, or

Is to release a reference to a Java String object.

Releasestringchars (jstring jstr, const jchar * Str );
Releasestringutfchars (jstring jstr, const char * Str );
The first parameter specifies a jstring variable, that is, the source of the local string to be released
The second parameter is the local string to be released.

(4) getstringcritical
1. To increase the possibility of directly returning a pointer to a Java string (instead of copying it ),

Jdk1.2 has a new function getstringcritical/releasestringcritical.
2. There is a key zone between getstringcritical and releasestringcritical. Do not interrupt the operation in the key zone,

Or allocate any new objects to the JVM. Otherwise, the JVM will be deadlocked.
3. Although this function will increase the possibility of directly returning a pointer to a Java string, it will still return the copied string according to the actual situation.
4. getstringutfcritical is not supported

(5) getstringregion/getstringutfregion
1. Action: copy the Java string content directly to the character array of C/C ++. Before calling this function, you must have a C/C ++ assigned string,

Then pass in to this function to copy the string (this function does not allocate memory)
2. Example
// Copy the Java string and pass the buffer in UTF-8 Encoding
Getstringutfregion (jstring STR, jsize start, jsize Len, char * buffer );
// Copy the Java string and pass the buffer in UTF-16 Encoding
Getstringregion (jstring STR, jsize start, jsize Len, jchar * buffer );

(3) create a Java String object in the C/C ++ local code
1. Functions
Jstring newstring (const jchar * STR, jsize Len );
Jstring newstringutf (const char * Str );
// Obtain the string length
Jsize getstringlength (jstring Str );
Jsize getstringutflength (jstring Str );

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.