Lines Matching full:code
45 JNI is the Java Native Interface. It defines a way for code written in the
47 code, e.g. functions written in C/C++. It's VM-neutral, has support for loading code from
72 If a piece of code has no other way to get its JNIEnv, you should share
84 If you want to access an object's field from native code, you would do the following:
87 <li> Get the class object reference for the class with <code>FindClass</code>
89 <li> Get the field ID for the field with <code>GetFieldID</code>
92 <code>GetIntField</code>
102 in your native code. Because we are limiting ourselves to one VM per process, it's reasonable
108 the <code>jclass</code>
110 to <code>NewGlobalRef</code> (see the next section).
114 the IDs is to add a piece of code that looks like this to the appropriate class:
118 * We use a class initializer to allow the native code to cache some
137 Create a nativeClassInit method in your C/C++ code that performs the ID lookups. The code
149 This applies to all sub-classes of <code>jobject</code>, including
150 <code>jclass</code>, <code>jstring</code>, and <code>jarray</code>.
156 a "global" reference. The <code>NewGlobalRef</code> function takes the
159 <code>DeleteGlobalRef</code>.
163 from <code>FindClass</code>, e.g.:
172 <code>NewGlobalRef</code> on the same object may be different.
174 you must use the <code>IsSameObject</code> function.</strong> Never compare
175 references with "==" in native code.
179 in native code. The 32-bit value representing an object may be different
182 not use <code>jobject</code> values as keys.
187 <code>DeleteLocalRef</code> instead of letting JNI do it for you. The
190 <code>EnsureLocalCapacity</code> to reserve more.
193 references, and should not be passed to <code>NewGlobalRef</code>. The raw data
194 pointers returned by functions like <code>GetStringUTFChars</code>
195 and <code>GetByteArrayElements</code> are also not objects.
198 thread to the VM with AttachCurrentThread, the code you are running will
215 <code>GetStringChars</code> method
216 does not require a copy, whereas <code>GetStringUTFChars</code> requires a malloc and a UTF conversion. Note that
223 string functions return <code>jchar*</code> or <code>jbyte*</code>, which
230 and handing it to <code>NewStringUTF</code> without filtering it.
248 the <code>Get<PrimitiveType>ArrayElements</code> family of calls
251 is guaranteed to be valid until the corresponding <code>Release</code> call
255 call fails, you must ensure that your code doesn't try to Release a NULL
259 non-NULL pointer for the <code>isCopy</code> argument. This is rarely
262 The <code>Release</code> call takes a <code>mode</code> argument that can
266 <li><code>0</code>
271 <li><code>JNI_COMMIT</code>
277 <li><code>JNI_ABORT</code>
285 One reason for checking the <code>isCopy</code> flag is to know if
286 you need to call <code>Release</code> with <code>JNI_COMMIT</code>
288 changes and executing code that uses the contents of the array, you may be
291 efficient handling of <code>JNI_ABORT</code>. For example, you might want
297 Some have asserted that you can skip the <code>Release</code> call if
298 <code>*isCopy</code> is false. This is not the case. If no copy buffer was
302 Also note that the <code>JNI_COMMIT</code> flag does NOT release the array,
303 and you will need to call <code>Release</code> again with a different flag
312 There is an alternative to calls like <code>Get<Type>ArrayElements</code>
313 and <code>GetStringChars</code> that may be very helpful when all you want
323 This grabs the array, copies the first <code>len</code> byte
325 policies the <code>Get</code> call will either pin or copy the array contents.
327 we use <code>JNI_ABORT</code> so there's no chance of a third copy.
339 to call <code>Release</code> after something fails.
342 Similarly, you can use the <code>Set<Type>ArrayRegion</code> call
343 to copy data into an array, and <code>GetStringRegion</code> or
344 <code>GetStringUTFRegion</code> to copy characters out of a
345 <code>String</code>.
351 Your code is expected to notice the exception (via the function's return value,
352 <code>ExceptionCheck()</code>, or <code>ExceptionOccurred()</code>) and return,
375 Note that exceptions thrown by interpreted code do not "leap over" native code,
376 code are not handled by Dalvik.
377 The JNI <code>Throw</code> and <code>ThrowNew</code> instructions just
379 native code, the exception will be noted and handled appropriately.
381 Native code can "catch" an exception by calling <code>ExceptionCheck</code> or
382 <code>ExceptionOccurred</code>, and clear it with
383 <code>ExceptionClear</code>. As usual,
389 <code>getMessage "()Ljava/lang/String;"</code>, invoke it, and if the result
390 is non-NULL use <code>GetStringUTFChars</code> to get something you can
397 JNI does very little error checking. Calling <code>SetIntField</code>
399 <code>private</code> and <code>final</code>. The
400 goal is to minimize the overhead on the assumption that, if you've written it in native code,
403 Some VMs support extended checking with the "<code>-Xcheck:jni</code>" flag. If the flag is set, the VM
434 The Dalvik VM supports the <code>-Xcheck:jni</code> flag. For a
441 JNI checks can be modified with the <code>-Xjniopts</code> command-line
449 code writing outside the buffer, and the contents are erased before the
450 storage is freed to trip up code that uses the data after calling Release.
461 You can load native code from shared libraries with the standard
462 <code>System.loadLibrary()</code> call. The
463 preferred way to get at your native code is:
466 <li> Call <code>System.loadLibrary()</code> from a static class
468 <code>nativeClassInit()</code>.) The argument is the "undecorated"
472 <li> Provide a native function: <code><strong>jint JNI_OnLoad(JavaVM* vm, void* reserved)</strong></code>
474 <li>In <code>JNI_OnLoad</code>, register all of your native methods. You
481 The <code>JNI_OnLoad</code> function should look something like this if
496 You can also call <code>System.load()</code> with the full path name of the
502 <code>JNI_OnLoad</code> function.
511 One other note about <code>JNI_OnLoad</code>: any <code>FindClass</code>
513 that was used to load the shared library. Normally <code>FindClass</code>
526 when interacting with native code,
530 <code>long</code> field rather than an <code>int</code></strong>.
536 <li><code>DefineClass</code> is not implemented. Dalvik does not use
541 to <code>NewLocalRef</code>, <code>NewGlobalRef</code>, and
542 <code>DeleteWeakGlobalRef</code>. (The spec strongly encourages
545 <li><code>GetObjectRefType</code> (new in 1.6) is implemented but not fully