Saturday, April 20, 2013

jni jnienv NewLocalRef example c c++ java


NewLocalRef

jobject NewLocalRef(JNIEnv *env, jobject ref);
Creates a new local reference that refers to the same object as ref. The given ref may be a global or local reference. Returns NULL if ref refers to null (NewLocalRef)
LINKAGE:
Index 25 in the JNIEnv interface function table.
SINCE:
JDK/JRE 1.2
Example - NewLocalRef

jstring
 MyNewString(JNIEnv *env, jchar *chars, jint len)
 {
     static jstring result;
 
     /* wstrncmp compares two Unicode strings */
     if (wstrncmp("CommonString", chars, len) == 0) {
         /* refers to the global ref caching "CommonString" */
         static jstring cachedString = NULL;
         if (cachedString == NULL) {
             /* create cachedString for the first time */
             jstring cachedStringLocal = ... ;
            /* cache the result in a global reference */
             cachedString = 
                 (*env)->NewGlobalRef(env, cachedStringLocal);
         }
         return (*env)->NewLocalRef(env, cachedString);
     }
 
     ... /* create the string as a local reference and store in
            result as a local reference */
     return result;
 }