This section is used as a reference section for JNI functions. It provides a complete list of all JNI features, as well as the exact layout of the JNI function table.

Note the use of the term “must” to describe limitations on JNI programmers. For example, when you see that a JNI function must accept a non-NULL object, it is your responsibility to ensure that NULL is not passed to that JNI function. Therefore, the JNI implementation does not need to perform NULL pointer checking in this JNI function.

Part of this section is adapted from Netscape’s JRI documentation.

Reference materials are grouped according to their usage. The reference section consists of the following functional areas:

  • Version information
  • A Class action
  • abnormal
  • Global and local references
  • Weak global reference
  • The object operation
  • Access the fields of the object
  • Calling instance methods
  • Accessing static fields
  • Calling static methods
  • String manipulation
  • An array of operating
  • Register local methods
  • Monitor the operation
  • NIO support
  • Reflection support
  • The JVM interface

Table of interface functions

Each function can be accessed at a fixed offset with the JNIEnv argument. The JNIEnv type is a pointer to a structure that holds Pointers to all JNI functions. It is defined as follows:

typedef const struct JNINativeInterface *JNIEnv; 
Copy the code

The VM initialization menu is shown in the following code. Note that the first three entries are reserved for future compatibility with COM. In addition, we kept some additional NULL entries near the beginning of the function table, so that, for example, future class-related JNI operations could be added after FindClass rather than at the end of the table.

Note that the function table can be shared among all JNI interface Pointers.

const struct JNINativeInterface ... = {

    NULL,
    NULL,
    NULL,
    NULL,
    GetVersion,

    DefineClass,
    FindClass,

    FromReflectedMethod,
    FromReflectedField,
    ToReflectedMethod,

    GetSuperclass,
    IsAssignableFrom,

    ToReflectedField,

    Throw,
    ThrowNew,
    ExceptionOccurred,
    ExceptionDescribe,
    ExceptionClear,
    FatalError,

    PushLocalFrame,
    PopLocalFrame,

    NewGlobalRef,
    DeleteGlobalRef,
    DeleteLocalRef,
    IsSameObject,
    NewLocalRef,
    EnsureLocalCapacity,

    AllocObject,
    NewObject,
    NewObjectV,
    NewObjectA,

    GetObjectClass,
    IsInstanceOf,

    GetMethodID,

    CallObjectMethod,
    CallObjectMethodV,
    CallObjectMethodA,
    CallBooleanMethod,
    CallBooleanMethodV,
    CallBooleanMethodA,
    CallByteMethod,
    CallByteMethodV,
    CallByteMethodA,
    CallCharMethod,
    CallCharMethodV,
    CallCharMethodA,
    CallShortMethod,
    CallShortMethodV,
    CallShortMethodA,
    CallIntMethod,
    CallIntMethodV,
    CallIntMethodA,
    CallLongMethod,
    CallLongMethodV,
    CallLongMethodA,
    CallFloatMethod,
    CallFloatMethodV,
    CallFloatMethodA,
    CallDoubleMethod,
    CallDoubleMethodV,
    CallDoubleMethodA,
    CallVoidMethod,
    CallVoidMethodV,
    CallVoidMethodA,

    CallNonvirtualObjectMethod,
    CallNonvirtualObjectMethodV,
    CallNonvirtualObjectMethodA,
    CallNonvirtualBooleanMethod,
    CallNonvirtualBooleanMethodV,
    CallNonvirtualBooleanMethodA,
    CallNonvirtualByteMethod,
    CallNonvirtualByteMethodV,
    CallNonvirtualByteMethodA,
    CallNonvirtualCharMethod,
    CallNonvirtualCharMethodV,
    CallNonvirtualCharMethodA,
    CallNonvirtualShortMethod,
    CallNonvirtualShortMethodV,
    CallNonvirtualShortMethodA,
    CallNonvirtualIntMethod,
    CallNonvirtualIntMethodV,
    CallNonvirtualIntMethodA,
    CallNonvirtualLongMethod,
    CallNonvirtualLongMethodV,
    CallNonvirtualLongMethodA,
    CallNonvirtualFloatMethod,
    CallNonvirtualFloatMethodV,
    CallNonvirtualFloatMethodA,
    CallNonvirtualDoubleMethod,
    CallNonvirtualDoubleMethodV,
    CallNonvirtualDoubleMethodA,
    CallNonvirtualVoidMethod,
    CallNonvirtualVoidMethodV,
    CallNonvirtualVoidMethodA,

    GetFieldID,

    GetObjectField,
    GetBooleanField,
    GetByteField,
    GetCharField,
    GetShortField,
    GetIntField,
    GetLongField,
    GetFloatField,
    GetDoubleField,
    SetObjectField,
    SetBooleanField,
    SetByteField,
    SetCharField,
    SetShortField,
    SetIntField,
    SetLongField,
    SetFloatField,
    SetDoubleField,

    GetStaticMethodID,

    CallStaticObjectMethod,
    CallStaticObjectMethodV,
    CallStaticObjectMethodA,
    CallStaticBooleanMethod,
    CallStaticBooleanMethodV,
    CallStaticBooleanMethodA,
    CallStaticByteMethod,
    CallStaticByteMethodV,
    CallStaticByteMethodA,
    CallStaticCharMethod,
    CallStaticCharMethodV,
    CallStaticCharMethodA,
    CallStaticShortMethod,
    CallStaticShortMethodV,
    CallStaticShortMethodA,
    CallStaticIntMethod,
    CallStaticIntMethodV,
    CallStaticIntMethodA,
    CallStaticLongMethod,
    CallStaticLongMethodV,
    CallStaticLongMethodA,
    CallStaticFloatMethod,
    CallStaticFloatMethodV,
    CallStaticFloatMethodA,
    CallStaticDoubleMethod,
    CallStaticDoubleMethodV,
    CallStaticDoubleMethodA,
    CallStaticVoidMethod,
    CallStaticVoidMethodV,
    CallStaticVoidMethodA,

    GetStaticFieldID,

    GetStaticObjectField,
    GetStaticBooleanField,
    GetStaticByteField,
    GetStaticCharField,
    GetStaticShortField,
    GetStaticIntField,
    GetStaticLongField,
    GetStaticFloatField,
    GetStaticDoubleField,

    SetStaticObjectField,
    SetStaticBooleanField,
    SetStaticByteField,
    SetStaticCharField,
    SetStaticShortField,
    SetStaticIntField,
    SetStaticLongField,
    SetStaticFloatField,
    SetStaticDoubleField,

    NewString,

    GetStringLength,
    GetStringChars,
    ReleaseStringChars,

    NewStringUTF,
    GetStringUTFLength,
    GetStringUTFChars,
    ReleaseStringUTFChars,

    GetArrayLength,

    NewObjectArray,
    GetObjectArrayElement,
    SetObjectArrayElement,

    NewBooleanArray,
    NewByteArray,
    NewCharArray,
    NewShortArray,
    NewIntArray,
    NewLongArray,
    NewFloatArray,
    NewDoubleArray,

    GetBooleanArrayElements,
    GetByteArrayElements,
    GetCharArrayElements,
    GetShortArrayElements,
    GetIntArrayElements,
    GetLongArrayElements,
    GetFloatArrayElements,
    GetDoubleArrayElements,

    ReleaseBooleanArrayElements,
    ReleaseByteArrayElements,
    ReleaseCharArrayElements,
    ReleaseShortArrayElements,
    ReleaseIntArrayElements,
    ReleaseLongArrayElements,
    ReleaseFloatArrayElements,
    ReleaseDoubleArrayElements,

    GetBooleanArrayRegion,
    GetByteArrayRegion,
    GetCharArrayRegion,
    GetShortArrayRegion,
    GetIntArrayRegion,
    GetLongArrayRegion,
    GetFloatArrayRegion,
    GetDoubleArrayRegion,
    SetBooleanArrayRegion,
    SetByteArrayRegion,
    SetCharArrayRegion,
    SetShortArrayRegion,
    SetIntArrayRegion,
    SetLongArrayRegion,
    SetFloatArrayRegion,
    SetDoubleArrayRegion,

    RegisterNatives,
    UnregisterNatives,

    MonitorEnter,
    MonitorExit,

    GetJavaVM,

    GetStringRegion,
    GetStringUTFRegion,

    GetPrimitiveArrayCritical,
    ReleasePrimitiveArrayCritical,

    GetStringCritical,
    ReleaseStringCritical,

    NewWeakGlobalRef,
    DeleteWeakGlobalRef,

    ExceptionCheck,

    NewDirectByteBuffer,
    GetDirectBufferAddress,
    GetDirectBufferCapacity,

    GetObjectRefType
  };
Copy the code

Version information

GetVersion

jint GetVersion(JNIEnv *env);
Copy the code

Returns the version of the local method interface.

LINKAGE:

Index 4 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

RETURNS:

Returns the major version number 16 bits higher and the minor version number 16 bits lower.

In JDK/JRE 1.1, GetVersion() returns 0x00010001.

In JDK/JRE 1.2, GetVersion() returns 0x00010002.

In JDK/JRE 1.4, GetVersion() returns 0x00010004.

In JDK/JRE 1.6, GetVersion() returns 0x00010006.

constant

SINCE the JDK/JRE 1.2:

#define JNI_VERSION_1_1 0x00010001
#define JNI_VERSION_1_2 0x00010002

/* Error codes */
#define JNI_EDETACHED    (-2)              /* thread detached from the VM */
#define JNI_EVERSION     (-3)              /* JNI version error 
Copy the code

SINCE the JDK/JRE 1.4:

#define JNI_VERSION_1_4 0x00010004
Copy the code

SINCE the JDK/JRE 1.6:

#define JNI_VERSION_1_6 0x00010006
Copy the code

Class operation

3.1 DefineClass

jclass DefineClass(JNIEnv *env, const char *name, jobject loader,
const jbyte *buf, jsize bufLen);
Copy the code

Loads a class from a buffer of raw class data. After the DefineClass call returns, the VM does not reference the buffer containing the original class data and can discard it if needed.

LINKAGE:

Index 5 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Name: The name of the class or interface to define, using the modified UTF-8 encoding.

Loader: A classloader assigned to a defined class.

Buf: buffer containing. Class file data.

BufLen: indicates the buffer length.

RETURNS:

If an error occurs, a Java class object or NULL is returned.

THROWS:

ClassFormatError: If class data does not specify a valid class.

ClassCircularityError: If the class or interface is its own superclass or superinterface.

OutOfMemoryError: If the system is out of memory.

SecurityException: If the caller tries to define a class in the Java package tree.

3.2 FindClass

jclass FindClass(JNIEnv *env, const char *name);
Copy the code

In JDK 1.1, this function loads a locally defined class. It searches the directories and zip files specified by the CLASSPATH environment variable for classes with the specified name.

Starting with Java 2 SDK version 1.2, the Java security model allows non-system classes to load and invoke native methods. FindClass locates the classloader associated with the current local method; That is, a classloader for a class that declares local methods. If the local method belongs to a system class, no classloader is involved. Otherwise, the appropriate class loader is called to load and link the specified class.

As of version 1.2 of the Java 2 SDK, when FindClass is called through the call interface, there is no current native method or its associated classloader. In this case, use this. GetSystemClassLoader results. This is the class loader created by the virtual machine for the application and is able to locate classes listed in the java.class.path property.

The name parameter is a fully qualified class name or array type signature. For example, the fully qualified class name of the java.lang.String class is: “Java /lang/String”

The array type signature of java.lang.Object [] is: “[Ljava/lang/Object;”

LINKAGE:

Index 6 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Name: Fully qualified class name (that is, package name, separated by a “/” followed by the class name). Returns the array class if the name begins with a “[” (array signature character). The string is encoded in the modified UTF-8 encoding.

RETURNS:

Returns the class object from the fully qualified name, or NULL if the class cannot be found.

THROWS:

ClassFormatError: If class data does not specify a valid class.

ClassCircularityError: If the class or interface is its own superclass or superinterface.

NoClassDefFoundError: If the definition of the requested class or interface is not found.

OutOfMemoryError: If the system is out of memory.

3.3 GetSuperclass

jclass GetSuperclass(JNIEnv *env, jclass clazz);
Copy the code

If clazz represents any class other than the Object class, this function returns an Object representing the superclass of the class specified by Clazz.

This function returns NULL if clazz specifies class Object, or if clazz represents an interface.

LINKAGE:

Index 10 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

RETURNS:

Returns the superclass of the class represented by Clazz, or NULL.

3.4 IsAssignableFrom

jboolean IsAssignableFrom(JNIEnv *env, jclass clazz1, jclass clazz2);
Copy the code

Determines whether objects of CLazz1 can be safely converted to clazz2.

LINKAGE:

Index 11 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz1: The first class parameter.

Clazz2: The second class parameter.

RETURNS:

JNI_TRUE is returned if any of the following conditions are met:

  • The first and second class arguments refer to the same Java class.
  • The first class is a subclass of the second.
  • The first class has the second class as one of its interfaces.

Four, abnormal

4.1 Throw

jint Throw(JNIEnv *env, jthrowable obj);
Copy the code

Causes a java.lang.Throwable object to be thrown.

LINKAGE:

Index 13 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: a java.lang.Throwable object.

RETURNS:

Returns 0 on success; Negative on failure.

THROWS:

Java.lang.Throwable object obj.

4.2 ThrowNew

jint ThrowNew(JNIEnv *env, jclass clazz, const char *message);
Copy the code

Constructs an exception object from the specified class using the message specified by Message and causes the exception to be thrown.

LINKAGE:

Index 14 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a subclass of java.lang.Throwable.

Message: The message used to construct a java.lang.Throwable object. The string is encoded in modified UTF-8.

RETURNS:

Returns 0 on success; Negative on failure.

THROWS:

Newly constructed java.lang.Throwable object.

4.3 ExceptionOccurred

jthrowable ExceptionOccurred(JNIEnv *env);
Copy the code

Determines whether an exception is thrown. Exceptions are thrown until the native code calls ExceptionClear() or Java code handles the exception.

LINKAGE:

Index 15 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

RETURNS:

Returns the exception object currently being thrown, or NULL if no exception is currently being thrown.

4.4 ExceptionDescribe

void ExceptionDescribe(JNIEnv *env);
Copy the code

Print stack exceptions and tracebacks to a system error reporting channel, such as STderr. This is a convenience routine for debugging.

LINKAGE:

Index 16 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

4.5 ExceptionClear

void ExceptionClear(JNIEnv *env);
Copy the code

Clears any exceptions that are currently being thrown. This routine is invalid if no exception is currently thrown.

LINKAGE:

Index 17 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

4.6 a FatalError

void FatalError(JNIEnv *env, const char *msg);
Copy the code

Raises a fatal error and does not want the VM to recover. This function does not return.

LINKAGE:

Index 18 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

MSG: error message. The string is encoded in modified UTF-8.

4.7 ExceptionCheck

We introduced a convenience function to check for pending exceptions without creating a local reference to the exception object.

jboolean ExceptionCheck(JNIEnv *env);
Copy the code

Return JNI_TRUE if there is a pending exception; Otherwise, JNI_FALSE is returned.

LINKAGE:

Index 228 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

Global and local references

5.1 Global Reference

5.1.1 NewGlobalRef

jobject NewGlobalRef(JNIEnv *env, jobject obj);
Copy the code

Creates a new global reference to the object referenced by the obj argument. Obj arguments can be global or local references. Global references must be handled explicitly by calling DeleteGlobalRef().

LINKAGE:

Index 21 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: global or local reference.

RETURNS:

Returns a global reference, or NULL if the system is out of memory.

5.1.2 DeleteGlobalRef

void DeleteGlobalRef(JNIEnv *env, jobject globalRef);
Copy the code

Delete the global reference pointed to by globalRef.

LINKAGE:

Index 22 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

GlobalRef: Global reference.

5.2 Local Reference

A local reference is valid for the duration of a local method call. They are released automatically when the local method returns. Each local reference costs a certain amount of Java virtual machine resources. Programmers need to ensure that local methods do not overassign local references. Although local references are automatically released after local methods are returned to Java, overallocating local references can cause the VM to run out of memory during the execution of local methods.

5.2.1 DeleteLocalRef

void DeleteLocalRef(JNIEnv *env, jobject localRef);
Copy the code

Delete the local reference pointed to by localRef.

LINKAGE:

Index 23 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

LocalRef: local reference.

Please note that

JDK/JRE 1.1 provides the above DeleteLocalRef function for programmers to manually remove local references. For example, if native code iterates through a potentially large array of objects and uses one element in each iteration, it is best to remove local references to array elements that are no longer in use before creating new local references in the next iteration.

Starting with JDK/JRE 1.2, there is an additional set of functions for local reference lifecycle management. These are the four functions listed below.

5.2.2 EnsureLocalCapacity

jint EnsureLocalCapacity(JNIEnv *env, jint capacity);
Copy the code

Ensure that at least a given number of local references can be created in the current thread. Returns 0 on success; Otherwise, a negative number is returned and OutOfMemoryError is thrown.

Before entering the local method, the VM automatically ensures that at least 16 local references can be created.

For backward compatibility, the VM allocates local references that exceed the guaranteed capacity (as a debugging support, the VM may warn the user that too many local references have been created. In the JDK, programmers can open these messages using the -verbose:jni command-line option). If no more local references can be created beyond the guaranteed capacity, the VM calls FatalError.

LINKAGE:

Index 26 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

5.2.3 requires PushLocalFrame

jint PushLocalFrame(JNIEnv *env, jint capacity);
Copy the code

Create a new local reference framework where at least a given number of local references can be created. Returns 0 on success and a negative number and a pending OutOfMemoryError on failure.

Note that local references that were created in the previous local frame are still valid in the current local frame.

LINKAGE:

Index 19 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

5.2.4 PopLocalFrame

jobject PopLocalFrame(JNIEnv *env, jobject result);
Copy the code

Pops up the current local reference frame, frees all local references, and returns local references in the previous local reference frame of the given result object.

If you do not need to return a reference to the previous frame, NULL is passed as the result.

LINKAGE:

Index 20 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

5.2.5 NewLocalRef

jobject NewLocalRef(JNIEnv *env, jobject ref);
Copy the code

Creates a new local reference to an object that references the same object as ref. A given reference can be global or local. If ref references NULL, NULL is returned.

LINKAGE:

Index 25 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

Weak global references

A weak global reference is a special kind of global reference. Unlike normal global references, weak global references allow garbage collection of underlying Java objects. Weak global references can be used in any case where global or local references are used. When the garbage collector runs, it frees the underlying object if the object is referenced only by weak references. A weak global reference to a free object is functionally equivalent to NULL. A programmer can detect whether a weak global reference points to a freed object by comparing a weak reference to NULL using IsSameObject.

Weak global references in JNI are simplified versions of Java weak references that are available as part of the Java 2 Platform API (the java.lang.ref package and its classes).

Clarification (added June 2001)

Because garbage collection can occur when a local method runs, objects referenced by weak global references can be freed at any time. While it is possible to use weak global references where global references are used, it is usually inappropriate to do so because they may be functionally equivalent to NULL without notice.

While IsSameObject can be used to determine whether a weak global reference refers to a freed object, it does not prevent the object from being freed immediately afterwards. Therefore, programmers may not rely on this check to determine whether weak global references can be used (as non-null references) in any future JNI function calls.

To overcome this inherent limitation, it is recommended to use the JNI functions NewLocalRef or NewGlobalRef to get a standard (strong) local or global reference to the same object and use that strong reference to access the target object. These functions return NULL if the object has been freed, otherwise a strong reference is returned (which prevents the object from being freed). When immediate access to the object is no longer needed, the new reference should be explicitly removed, allowing the object to be released.

Weak global references are weaker than other types of weak references, such as SoftReference or Java objects of the WeakReference class. A weak global reference to a specific object is not functionally equivalent to NULL until the SoftReference or WeakReference object that references the same specific object clears its reference.

Weak global references are weaker than Java’s internal references to objects that need to be finalised. Weak global references will not be functionally equivalent to NULL until finalization of the reference object, if any, has been completed.

The interaction between a weak global reference and a virtual reference is undefined. Specifically, the JVM’s implementation may (or may not) handle weak global references after virtual references, and may (or may not) use weak global references to preserve objects that are also referenced by virtual reference objects. Such undefined weak global references should be avoided.

6.1 NewWeakGlobalRef

jweak NewWeakGlobalRef(JNIEnv *env, jobject obj);
Copy the code

Create a new weak global reference. If obj references NULL, or the VM is out of memory, null is returned. If the VM is out of memory, an OutOfMemoryError is thrown.

LINKAGE:

Index 226 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

6.2 DeleteWeakGlobalRef

void DeleteWeakGlobalRef(JNIEnv *env, jweak obj);
Copy the code

Delete the VM resources required for the given weak global reference.

LINKAGE:

Index 227 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

7. Object operation

7.1 AllocObject

jobject AllocObject(JNIEnv *env, jclass clazz);
Copy the code

Allocates a new Java object without calling any of its constructors, returning a reference to the object. Clazz arguments cannot reference array classes.

LINKAGE:

Index 27 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

RETURNS:

Returns a Java object, or NULL if it cannot be constructed.

THROWS:

InstantiationException: If the class is an interface or abstract class

OutOfMemoryError: If the system is out of memory.

7.2 NewObject, NewObjectA NewObjectV

jobject NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...) ; jobject NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args); jobject NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);Copy the code

Construct a new Java object. The method ID indicates which constructor method to call. This ID must be obtained by calling GetMethodID() with the method name and return type void (V). Clazz arguments cannot reference array classes.

NewObject

The programmer passes all the arguments to the constructor immediately after the methodID argument. NewObject() takes these parameters and passes them to the Java methods the programmer wants to call.

LINKAGE:

Index 28 in the JNIEnv interface function table.

NewObjectA

The programmer puts all the arguments passed to the constructor in the args array of jValues, which follows the methodID argument. NewObjectA() takes the arguments in this array and passes them to the Java methods the programmer wants to call.

LINKAGE:

Index 30 in the JNIEnv interface function table.

NewObjectV

The programmer puts all the arguments passed to the constructor in the args argument of type va_list, which follows methodID. NewObjectV() takes these arguments and passes them to the Java methods the programmer wants to call.

LINKAGE:

Index 29 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

MethodID: methodID of the constructor.

Additional arguments to NewObject: arguments to the constructor.

Additional arguments to NewObjectA: args: array of constructor arguments.

Additional arguments to NewObjectV: args: va_list of arguments to the constructor.

RETURNS:

Returns a Java object, or NULL if it cannot be constructed.

THROWS:

InstantiationException: If the class is an interface or abstract class

OutOfMemoryError: If the system is out of memory.

Any exceptions thrown by the constructor.

7.3 GetObjectClass

jclass GetObjectClass(JNIEnv *env, jobject obj);
Copy the code

Returns the class of the object.

LINKAGE:

Index 31 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: a Java object (cannot be NULL).

RETURNS:

Returns a Java class object.

7.4 GetObjectRefType

jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj);
Copy the code

Returns the type of the object referenced by the obj argument. The obj argument can be a local, global, or weak global reference.

LINKAGE:

Index 232 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: local, global, or weak global reference.

Vm: Virtual machine instance from which the interface will be retrieved.

Env: a pointer to the current thread’s JNI interface pointer.

Version: JNI version of the request.

RETURNS:

The GetObjectRefType function returns one of the following enumerated values defined as jobjectRefType:

JNIInvalidRefType = 0,
JNILocalRefType = 1,
JNIGlobalRefType = 2,
JNIWeakGlobalRefType = 3
Copy the code

If the argument obj is a weak global reference type, the return will be JNIWeakGlobalRefType.

If the argument obj is a global reference type, the return value will be JNIGlobalRefType.

If the argument obj is a local reference type, the return will be JNILocalRefType.

If the obj argument is not a valid reference, the return value of this function will be JNIInvalidRefType.

An invalid reference is one that is not a valid handle. That is, the obj pointer address does not point to a location in memory allocated from the Ref creation function or returned from the JNI function.

Therefore,NULL will be an invalid reference, and GetObjectRefType(env,NULL) will return JNIInvalidRefType.

A NULL reference (a reference to NULL), on the other hand, returns the reference type at which the NULL reference was originally created.

Cannot use GetObjectRefType on deleted references.

Because references are typically implemented as Pointers to in-memory data structures that may be reused by any reference allocation service in the VM, once deleted, you do not specify what value GetObjectRefType will return.

SINCE:

The JDK/JRE 1.6

7.5 IsInstanceOf

jboolean IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz);
Copy the code

Tests whether an object is an instance of a class.

LINKAGE:

Index 32 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: A Java object.

Clazz: a Java class object.

RETURNS:

If obj can be converted to clazz, return JNI_TRUE; Otherwise, JNI_FALSE is returned. A NULL object can be cast to any class.

7.6 IsSameObject

jboolean IsSameObject(JNIEnv *env, jobject ref1, jobject ref2);
Copy the code

Tests whether two references refer to the same Java object.

LINKAGE:

Index 24 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Ref1: A Java object.

Ref2: A Java object.

RETURNS:

If ref1 and ref2 reference the same Java object, return JNI_TRUE, or NULL for both; Otherwise, JNI_FALSE is returned.

Access the fields of the object

8.1 GetFieldID

jfieldID GetFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
Copy the code

Returns the field ID of an instance (non-static) field of the class. The field is specified by its name and signature. The GetField and SetField families of visitor functions use field ids to retrieve object fields.

GetFieldID() causes an uninitialized class to be initialized.

GetFieldID() cannot be used to get the length field of the array. Use GetArrayLength() instead.

LINKAGE:

Index 94 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

Name: name of a field in a modified UTF-8 string ending with 0.

Sig: field signature in a modified UTF-8 character string ending with 0.

RETURNS:

Returns the field ID, or NULL on failure.

THROWS:

NoSuchFieldError: if the specified field cannot be found.

ExceptionInInitializerError: if a class initialization program because of abnormal and failure.

OutOfMemoryError: If the system is out of memory.

8.2 the GetField series

NativeType Get<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID);
Copy the code

Accessor routines in this family return the value of an instance (non-static) field of an object. The field to access is specified by the field ID obtained by calling GetFieldID().

The following table describes the GetField routine name and result type. You should either replace type in GetField with the Java type of the field, or use an actual routine name in the table, and replace NativeType with the corresponding NativeType of that routine.

GetField Routine name The local type
GetObjectField() jobject
GetBooleanField() jboolean
GetByteField() jbyte
GetCharField() jchar
GetShortField() jshort
GetIntField() jint
GetLongField() jlong
GetFloatField() jfloat
GetDoubleField() jdouble

Table -GetField family of accessor routines

LINKAGE:

Index in JNIEnv interface function table:

GetField Routine name The index
GetObjectField() 95
GetBooleanField() 96
GetByteField() 97
GetCharField() 98
GetShortField() 99
GetIntField() 100
GetLongField() 101
GetFloatField() 102
GetDoubleField() 103

Table -GetField family of accessor routines

PARAMETERS:

Env: JNI interface pointer.

Obj: a Java object (cannot be NULL).

FieldID: indicates a valid fieldID.

RETURNS:

Returns the contents of the field.

8.3 SetField series

void Set<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID, NativeType value);
Copy the code

This family of accessor routines sets the value of an instance (non-static) field of an object. The field to access is specified by the field ID obtained by calling GetFieldID().

The following table describes SetField routine names and value types. You should either replace type in SetField with the Java type of the field, or use an actual routine name in the table, and replace NativeType with the corresponding NativeType of that routine.

SetField routines The local type
SetObjectField() jobject
SetBooleanField() jboolean
SetByteField() jbyte
SetCharField() jchar
SetShortField() jshort
SetIntField() jint
SetLongField() jlong
SetFloatField() jfloat
SetDoubleField() jdouble

Table -SetField series of accessor routines

LINKAGE:

JNIEnv index in the interface function table.

SetField routines The index
SetObjectField() 104
SetBooleanField() 105
SetByteField() 106
SetCharField() 107
SetShortField() 108
SetIntField() 109
SetLongField() 110
SetFloatField() 111
SetDoubleField() 112

Table -SetField series of accessor routines

PARAMETERS:

Env: JNI interface pointer.

Obj: a Java object (cannot be NULL).

FieldID: indicates a valid fieldID.

Value: indicates the new value of the field.

9. Call instance methods

9.1 GetMethodID

jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
Copy the code

Returns the method ID of an instance (non-static) method of a class or interface. Methods can be defined in a clazz superclass and inherited by Clazz. A method is determined by its name and signature.

GetMethodID() causes an uninitialized class to be initialized.

To get the constructor’s method ID, provide it as the method name and use void(V) as the return type.

LINKAGE:

Index 33 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

Name: method name in a modified UTF-8 string ending in 0.

Sig: method signature in modified UTF-8 string ending in 0.

RETURNS:

Returns the method ID, or NULL if the specified method is not found.

THROWS:

NoSuchMethodError: if the specified method cannot be found.

ExceptionInInitializerError: if a class initialization program because of abnormal and failure.

OutOfMemoryError: If the system is out of memory.

9.2 CallMethod, CallMethodA, CallMethodV

NativeType Call<type>Method(JNIEnv *env, jobject obj, jmethodID methodID, ...) ; NativeType Call<type>MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args); NativeType Call<type>MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args);Copy the code

Methods from these three action families are used to call Java instance methods from local methods. They just have different mechanisms for passing arguments to the methods they call.

These families of operations invoke instance (non-static) methods on Java objects based on the specified method ID. The methodID parameter must be obtained by calling GetMethodID().

When these functions are used to call private methods and constructors, the method ID must be derived from obj’s real class, not from one of its superclasses.

CallMethod routines

The programmer places all the parameters to be passed to the method immediately after the methodID parameter. The CallMethod routine takes these parameters and passes them to the Java methods the programmer wishes to invoke.

CallMethodA routines

The programmer puts all of the method’s arguments into the args array of jValues, which follows the methodID argument. The CallMethodA routine takes the arguments from this array and passes them to the Java methods the programmer wants to call.

CallMethodV routines

The programmer puts all of the method’s arguments in an args argument of type va_list, which follows methodID. The CallMethodV routine takes the parameters and passes them to the Java methods the programmer wants to call.

The following table describes each method call routine by result type. You should replace the type in CallMethod with the Java type of the method you are calling (or one of the actual method call routine names in the table), and replace NativeType with the corresponding NativeType of that routine.

CallMethod Routine name The local type
CallVoidMethod() CallVoidMethodA() CallVoidMethodV() void
CallObjectMethod() CallObjectMethodA() CallObjectMethodV() jobject
CallBooleanMethod() CallBooleanMethodA() CallBooleanMethodV() jboolean
CallByteMethod() CallByteMethodA() CallByteMethodV() jbyte
CallCharMethod() CallCharMethodA() CallCharMethodV() jchar
CallShortMethod() CallShortMethodA() CallShortMethodV() jshort
CallIntMethod() CallIntMethodA() CallIntMethodV() jint
CallLongMethod() CallLongMethodA() CallLongMethodV() jlong
CallFloatMethod() CallFloatMethodA() CallFloatMethodV() jfloat
CallDoubleMethod() CallDoubleMethodA() CallDoubleMethodV() jdouble

Table-instance method call routines

LINKAGE:

Index in JNIEnv interface function table:

CallMethod Routine name The index
CallVoidMethod() CallVoidMethodA() CallVoidMethodV() 61 63 62
CallObjectMethod() CallObjectMethodA() CallObjectMethodV() 34 to 36 35
CallBooleanMethod() CallBooleanMethodA() CallBooleanMethodV() 37 and 38
CallByteMethod() CallByteMethodA() CallByteMethodV() 40 and 41
CallCharMethod() CallCharMethodA() CallCharMethodV() 43 45 44
CallShortMethod() CallShortMethodA() CallShortMethodV() 46 to 48 47
CallIntMethod() CallIntMethodA() CallIntMethodV() 49 51 50
CallLongMethod() CallLongMethodA() CallLongMethodV() 52 and 53 54
CallFloatMethod() CallFloatMethodA() CallFloatMethodV() 55 to 57 56
CallDoubleMethod() CallDoubleMethodA() CallDoubleMethodV() 58, 60 59

Table-instance method call routines

PARAMETERS:

Env: JNI interface pointer.

Obj: A Java object.

MethodID: indicates the ID of a method.

Additional arguments to the CallMethod routine:

Arguments to a Java method.

Additional arguments to the CallMethodA routine:

Args: A set of parameters.

Additional parameters to the CallMethodV routine:

Args: vA_list of an argument.

RETURNS:

Returns the result of calling a Java method.

THROWS:

An exception thrown during execution of a Java method.

9.3 CallNonvirtualMethod, CallNonvirtualMethodA, CallNonvirtualMethodV

NativeType CallNonvirtual<type>Method(JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, ...) ; NativeType CallNonvirtual<type>MethodA(JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, const jvalue *args); NativeType CallNonvirtual<type>MethodV(JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, va_list args);Copy the code

These families of operations invoke instance (non-static) methods on Java objects based on the specified class and method ID. The methodID parameter must be obtained by calling GetMethodID () on the clazz class.

The CallNonvirtualMethod family routines are different from the CallMethod family routines. The CallMethod routine calls the method based on the class of the object, while the CallNonvirtualMethod routine calls the method based on the class specified by the Clazz parameter, getting the method ID from that class. The method ID must be obtained from the object’s actual class or one of its superclasses.

CallNonvirtualMethod routines

The programmer passes all the parameters to the method immediately after the methodID argument. The CallNonvirtualMethod routine takes these parameters and passes them to the Java methods the programmer wishes to invoke.

CallNonvirtualMethodA routines

The programmer places all of the method’s arguments in the ARgs array of JValues immediately after the methodID argument. The CallNonvirtualMethodA routine takes the arguments in this array and passes them to the Java methods the programmer wants to call.

CallNonvirtualMethodV routines

The programmer puts all of the method’s arguments in an args argument of type va_list immediately after the methodID argument. The CallNonvirtualMethodV routine takes the parameters and passes them to the Java methods the programmer wants to call.

The following table describes each method call routine by result type. You should either replace type in CallNonvirtualMethod with the Java type of the method, or call one of the routine names with the actual method in the table, and replace NativeType with the corresponding NativeType of that routine.

CallNonvirtualMethod Routine name The local type
CallNonvirtualVoidMethod() CallNonvirtualVoidMethodA() CallNonvirtualVoidMethodV() void
CallNonvirtualObjectMethod() CallNonvirtualObjectMethodA() CallNonvirtualObjectMethodV() jobject
CallNonvirtualBooleanMethod() CallNonvirtualBooleanMethodA() CallNonvirtualBooleanMethodV() jboolean
CallNonvirtualByteMethod() CallNonvirtualByteMethodA() CallNonvirtualByteMethodV() jbyte
CallNonvirtualCharMethod() CallNonvirtualCharMethodA() CallNonvirtualCharMethodV() jchar
CallNonvirtualShortMethod() CallNonvirtualShortMethodA() CallNonvirtualShortMethodV() jshort
CallNonvirtualIntMethod() CallNonvirtualIntMethodA() CallNonvirtualIntMethodV() jint
CallNonvirtualLongMethod() CallNonvirtualLongMethodA() CallNonvirtualLongMethodV() jlong
CallNonvirtualFloatMethod() CallNonvirtualFloatMethodA() CallNonvirtualFloatMethodV() jfloat
CallNonvirtualDoubleMethod() CallNonvirtualDoubleMethodA() CallNonvirtualDoubleMethodV() jdouble

Table – CallNonvirtualMethod routines

LINKAGE:

JNIEnv index in the interface function table.

CallNonvirtualMethod Routine name The index
CallNonvirtualVoidMethod() CallNonvirtualVoidMethodA() CallNonvirtualVoidMethodV() 91 93 92
CallNonvirtualObjectMethod() CallNonvirtualObjectMethodA() CallNonvirtualObjectMethodV() 64 66 65
CallNonvirtualBooleanMethod() CallNonvirtualBooleanMethodA() CallNonvirtualBooleanMethodV() 67 69 68
CallNonvirtualByteMethod() CallNonvirtualByteMethodA() CallNonvirtualByteMethodV() 70 72 71
CallNonvirtualCharMethod() CallNonvirtualCharMethodA() CallNonvirtualCharMethodV() 73 75 74
CallNonvirtualShortMethod() CallNonvirtualShortMethodA() CallNonvirtualShortMethodV() 76 78 77
CallNonvirtualIntMethod() CallNonvirtualIntMethodA() CallNonvirtualIntMethodV() 79 81 80
CallNonvirtualLongMethod() CallNonvirtualLongMethodA() CallNonvirtualLongMethodV() 82 84 83
CallNonvirtualFloatMethod() CallNonvirtualFloatMethodA() CallNonvirtualFloatMethodV() 85 87 86
CallNonvirtualDoubleMethod() CallNonvirtualDoubleMethodA() CallNonvirtualDoubleMethodV() 88 90 89

Table – CallNonvirtualMethod routines

PARAMETERS:

Env: JNI interface pointer.

Clazz: A Java class.

Obj: A Java object.

MethodID: indicates the ID of a method.

Additional arguments to the CallNonvirtualMethod routine: Arguments to the Java method.

Additional arguments to the CallNonvirtualMethodA routine: args: a set of arguments.

Additional arguments to the CallNonvirtualMethodV routine: args: a va_list of arguments.

RETURNS:

Returns the result of calling a Java method.

THROWS:

An exception thrown during execution of a Java method.

Access static fields

10.1 GetStaticFieldID

jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
Copy the code

Returns the field ID of the static field of the class. Fields are specified by their name and signature. The GetStaticField and SetStaticField families of accessor functions retrieve static fields using field IDS.

GetStaticFieldID() causes an uninitialized class to be initialized.

LINKAGE:

Index 144 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

Name: static field name in a modified UTF-8 string ending with 0.

Sig: field signature in a modified UTF-8 string ending with 0.

RETURNS:

Returns the field ID, or NULL if the specified static field is not found.

THROWS:

NoSuchFieldError: if the specified static field cannot be found.

ExceptionInInitializerError: if a class initialization program because of abnormal and failure.

OutOfMemoryError: If the system is out of memory.

10.2 GetStaticField

NativeType GetStatic<type>Field(JNIEnv *env, jclass clazz, jfieldID fieldID);
Copy the code

This family of accessor routines returns the value of the static field of the object. The field to access is specified by the field ID, which is obtained by calling GetStaticFieldID().

The following table describes the family of GET routine names and result types. You should either replace type in GetStaticField with the Java type of that field, or replace an actual static field visitor routine name in the table, and replace NativeType with the corresponding local type of that routine.

GetStaticField routine name The local type
GetStaticObjectField() jobject
GetStaticBooleanField() jboolean
GetStaticByteField() jbyte
GetStaticCharField() jchar
GetStaticShortField() jshort
GetStaticIntField() jint
GetStaticLongField() jlong
GetStaticFloatField() jfloat
GetStaticDoubleField() jdouble

LINKAGE:

JNIEnv index in the interface function table.

GetStaticField routine name The index
GetStaticObjectField() 145
GetStaticBooleanField() 146
GetStaticByteField() 147
GetStaticCharField() 148
GetStaticShortField() 149
GetStaticIntField() 150
GetStaticLongField() 151
GetStaticFloatField() 152
GetStaticDoubleField() 153

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

FieldID: static fieldID.

RETURNS:

Returns the contents of a static field.

10.3 SetStaticField

void SetStatic<type>Field(JNIEnv *env, jclass clazz, jfieldID fieldID, NativeType value);
Copy the code

Such accessor routines set the values of the static fields of the object. The field to access is specified by the field ID, which is obtained by calling GetStaticFieldID().

The following table describes the routine names and value types that are set. You should replace the type in SetStaticField with the Java type of that field, or one of the actual static field routine names set in the table, and replace NativeType with the corresponding local type of that routine.

SetStaticField routine name The local type
SetStaticObjectField() jobject
SetStaticBooleanField() jboolean
SetStaticByteField() jbyte
SetStaticCharField() jchar
SetStaticShortField() jshort
SetStaticIntField() jint
SetStaticLongField() jlong
SetStaticFloatField() jfloat
SetStaticDoubleField() jdouble

LINKAGE:

JNIEnv index in the interface function table.

SetStaticField routine name The index
SetStaticObjectField() 154
SetStaticBooleanField() 155
SetStaticByteField() 156
SetStaticCharField() 157
SetStaticShortField() 158
SetStaticIntField() 159
SetStaticLongField() 160
SetStaticFloatField() 161
SetStaticDoubleField() 162

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

FieldID: static fieldID.

Value: indicates the new value of the field.

Call static methods

11.1 GetStaticMethodID

jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
Copy the code

Returns the method ID of the static method of the class. A method is specified by its name and signature. GetStaticMethodID() causes an uninitialized class to be initialized.

LINKAGE:

Index 113 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: Java class object.

Name: Static method name in a modified UTF-8 string ending in 0.

Sig: method signature in a modified UTF-8 string ending in 0.

RETURNS:

Returns the method ID, or NULL on failure.

THROWS:

NoSuchMethodError: If the specified static method cannot be found.

ExceptionInInitializerError: if a class initializer failure due to abnormal.

OutOfMemoryError: If the system runs out of memory.

11.2 CallStaticMethod, CallStaticMethodA, and CallStaticMethodV

NativeType CallStatic<type>Method(JNIEnv *env, jclass clazz, jmethodID methodID, ...) ; NativeType CallStatic<type>MethodA(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args); NativeType CallStatic<type>MethodV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);Copy the code

This series of operations invokes static methods on Java objects based on the specified method ID. The methodID parameter must be obtained by calling GetStaticMethodID().

The method ID must come from Clazz, not from one of the superclasses.

CallStaticMethod

The programmer should pass all arguments to the method immediately after the methodID argument. The CallStaticMethod routine takes these parameters and passes them to the Java methods the programmer wants to call.

CallStaticMethodA

Programmers should place all of the method’s arguments in the ARgs array of JValues immediately after the methodID argument. The CallStaticMethodA routine takes the arguments from this array and passes them to the Java methods the programmer wants to call.

CallStaticMethodV

Programmers should place all of the method’s arguments in an args argument of type va_list immediately after the methodID argument. The CallStaticMethodV routine takes parameters and passes them to the Java methods the programmer wants to call.

The following table describes each method call routine by result type. You should either replace type in CallStaticMethod with the Java type of the method, or replace one of the actual method call routine names from the table, and replace NativeType with the corresponding NativeType of that routine.

CallStaticMethod Name of the routine The local type
CallStaticVoidMethod() CallStaticVoidMethodA() CallStaticVoidMethodV() void
CallStaticObjectMethod() CallStaticObjectMethodA() CallStaticObjectMethodV() jobject
CallStaticBooleanMethod() CallStaticBooleanMethodA() CallStaticBooleanMethodV() jboolean
CallStaticByteMethod() CallStaticByteMethodA() CallStaticByteMethodV() jbyte
CallStaticCharMethod() CallStaticCharMethodA() CallStaticCharMethodV() jchar
CallStaticShortMethod() CallStaticShortMethodA() CallStaticShortMethodV() jshort
CallStaticIntMethod() CallStaticIntMethodA() CallStaticIntMethodV() jint
CallStaticLongMethod() CallStaticLongMethodA() CallStaticLongMethodV() jlong
CallStaticFloatMethod() CallStaticFloatMethodA() CallStaticFloatMethodV() jfloat
CallStaticDoubleMethod() CallStaticDoubleMethodA() CallStaticDoubleMethodV() jdouble

Table -CallStaticMethod Call routine

LINKAGE:

JNIEnv index in the interface function table.

CallStaticMethod Name of the routine The index
CallStaticVoidMethod() CallStaticVoidMethodA() CallStaticVoidMethodV() 141 143 142
CallStaticObjectMethod() CallStaticObjectMethodA() CallStaticObjectMethodV() 114 116 115
CallStaticBooleanMethod() CallStaticBooleanMethodA() CallStaticBooleanMethodV() 117 119 118
CallStaticByteMethod() CallStaticByteMethodA() CallStaticByteMethodV() 120 122 121
CallStaticCharMethod() CallStaticCharMethodA() CallStaticCharMethodV() 123 125 124
CallStaticShortMethod() CallStaticShortMethodA() CallStaticShortMethodV() 126 128 127
CallStaticIntMethod() CallStaticIntMethodA() CallStaticIntMethodV() 129 131 130
CallStaticLongMethod() CallStaticLongMethodA() CallStaticLongMethodV() 132 134 133
CallStaticFloatMethod() CallStaticFloatMethodA() CallStaticFloatMethodV() 135 137 136
CallStaticDoubleMethod() CallStaticDoubleMethodA() CallStaticDoubleMethodV() 138 140 139

Table -CallStaticMethod Call routine

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

MethodID: indicates the ID of a static method.

Additional arguments to the CallStaticMethod routine: arguments to the static method.

Additional arguments to the CallStaticMethodA routine: ARGS: a set of arguments.

Additional arguments to the CallStaticMethodV routine: args: a va_list of arguments.

RETURNS:

Returns the result of calling a static Java method.

THROWS:

An exception thrown during execution of a Java method.

String manipulation

12.1 NewString

jstring NewString(JNIEnv *env, const jchar *unicodeChars, jsize len);
Copy the code

Construct a new java.lang.String object from a Unicode character array.

LINKAGE:

Index 163 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

UnicodeChars: Pointer to a Unicode string.

Len: The length of the Unicode string.

RETURNS:

Returns a Java string object, or NULL if a string cannot be constructed.

THROWS:

OutOfMemoryError: If the system is out of memory.

12.2 GetStringLength

jsize GetStringLength(JNIEnv *env, jstring string);
Copy the code

Returns the length of the Java string (number of Unicode characters).

LINKAGE:

Index 164 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

String: Java string object.

RETURNS:

Returns the length of a Java string.

12.3 GetStringChars

const jchar * GetStringChars(JNIEnv *env, jstring string, jboolean *isCopy);
Copy the code

Returns a pointer to an array of Unicode characters for a string. This pointer is valid until ReleaseStringchars() is called.

If isCopy is not NULL, and if a copy is made, *isCopy is set to JNI_TRUE; If there is no replication, set it to JNI_FALSE.

LINKAGE:

Index 165 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

String: Java string object.

IsCopy: pointer to a Boolean value.

RETURNS:

Returns a pointer to a Unicode string, or NULL on failure.

12.4 ReleaseStringChars

void ReleaseStringChars(JNIEnv *env, jstring string, const jchar *chars);
Copy the code

Notifies the VM that native code no longer needs to access characters. The chars argument is a pointer to a string using GetStringChars().

LINKAGE:

Index 166 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

String: Java string object.

Chars: Pointer to a Unicode string.

12.5 NewStringUTF

jstring NewStringUTF(JNIEnv *env, const char *bytes);
Copy the code

Constructs a new java.lang.String object from the character array in the modified UTF-8 encoding.

LINKAGE:

Index 167 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Bytes: pointer to the modified UTF-8 string.

RETURNS:

Returns a Java string object, or NULL if a string cannot be constructed.

THROWS:

OutOfMemoryError: If the system is out of memory.

12.6 GetStringUTFLength

jsize GetStringUTFLength(JNIEnv *env, jstring string);
Copy the code

Returns the length of the modified UTF-8 representation of the string in bytes.

LINKAGE:

Index 168 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

String: Java string object.

RETURNS:

Returns the UTF-8 length of the string.

12.7 GetStringUTFChars

const char * GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy);
Copy the code

Returns a pointer to a byte array representing a string in the modified UTF-8 encoding. This array is valid until ReleaseStringUTFChars() is released.

If isCopy is not NULL, and if a copy is made, *isCopy is set to JNI_TRUE; If there is no replication, set it to JNI_FALSE.

LINKAGE:

Index 169 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

String: Java string object.

IsCopy: pointer to a Boolean value.

RETURNS:

Returns a pointer to the modified UTF-8 string, or NULL on failure.

12.8 ReleaseStringUTFChars

void ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf);
Copy the code

Notifies the VM that native code no longer needs to access UTF. The UTf argument is a pointer derived from a string using GetStringUTFChars().

LINKAGE:

Index 170 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

String: Java string object.

Utf: pointer to the modified UTF-8 string.

Pay attention to

In JDK/JRE 1.1, programmers can retrieve raw array elements in user-supplied buffers. Starting with JDK/JRE 1.2, an additional set of features is provided to allow native code to retrieve Unicode (UTF-16) or modified UTF-8 encoded characters in user-provided buffers. See the following features.

12.9 GetStringRegion

void GetStringRegion(JNIEnv *env, jstring str, jsize start, jsize len, jchar *buf);
Copy the code

Copies len Unicode characters at the beginning of the offset to the given buffer buf.

Raise StringIndexOutOfBoundsException index overflow.

LINKAGE:

Index 220 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

12.10 GetStringUTFRegion

void GetStringUTFRegion(JNIEnv *env, jstring str, jsize start, jsize len, char *buf);
Copy the code

Converts the Len number of Unicode characters starting at the offset to the modified UTF-8 encoding and places the result in the given buffer buF.

Raise StringIndexOutOfBoundsException index overflow.

LINKAGE:

Index 221 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

12.11 GetStringCritical, ReleaseStringCritical

const jchar * GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy);
void ReleaseStringCritical(JNIEnv *env, jstring string, const jchar *carray);
Copy the code

The semantics of these two functions are similar to the existing Get/ReleaseStringChars functions. If possible, the VM returns a pointer to the string element; Otherwise, make copies. However, there are significant limitations to how you can use these features. The Get/ReleaseStringCritical call includes sections of code, local code shall not issue any JNI calls, or lead to the current thread is blocked.

Get/ReleaseStringCritical limit and Get/ReleasePrimitiveArrayCritical similar restrictions.

LINKAGE (GetStringCritical) :

Index 224 in the JNIEnv interface function table.

LINKAGE (ReleaseStingCritical) :

Index 225 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

Array manipulation

13.1 GetArrayLength

jsize GetArrayLength(JNIEnv *env, jarray array);
Copy the code

Returns the number of elements in an array.

LINKAGE:

Index 171 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Array: a Java array object.

RETURNS:

Returns the length of the array.

13.2 NewObjectArray

jobjectArray NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement);
Copy the code

Construct a new array containing objects in class elementClass. All elements are initially set to initialElement.

LINKAGE:

Index 172 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Length: indicates the size of the array.

ElementClass: array elementClass.

InitialElement: Initializes the value.

RETURNS:

Returns a Java array object, or NULL if the array cannot be constructed.

THROWS:

OutOfMemoryError: If the system is out of memory.

13.3 GetObjectArrayElement

jobject GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index);
Copy the code

Returns an element of the Object array.

LINKAGE:

Index 173 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Array: a Java array.

Index: indicates the array index.

RETURNS:

Returns a Java object.

THROWS:

ArrayIndexOutOfBoundsException: if the index is not specified in the effective index in the array.

13.4 SetObjectArrayElement

void SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value);
Copy the code

Sets the elements of the Object array.

LINKAGE:

Index 174 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Array: a Java array.

Index: indicates the array index.

Value: indicates the new value.

THROWS:

ArrayIndexOutOfBoundsException: if the index is not specified in the effective index in the array.

ArrayStoreException: If the value’s class is not a subclass of the element class of the array.

13.5 NewArray

ArrayType New<PrimitiveType>Array(JNIEnv *env, jsize length);
Copy the code

A series of operations used to construct a new primitive array object. The following table describes the specific basic array constructors. You should replace NewArray with an actual raw array constructor routine name in this table, and ArrayType with the corresponding ArrayType for that routine.

NewArray routines An array type
NewBooleanArray() jbooleanArray
NewByteArray() jbyteArray
NewCharArray() jcharArray
NewShortArray() jshortArray
NewIntArray() jintArray
NewLongArray() jlongArray
NewFloatArray() jfloatArray
NewDoubleArray() jdoubleArray

LINKAGE:

JNIEnv index in the interface function table.

NewArray routines Index
NewBooleanArray() 175
NewByteArray() 176
NewCharArray() 177
NewShortArray() 178
NewIntArray() 179
NewLongArray() 180
NewFloatArray() 181
NewDoubleArray() 182

PARAMETERS:

Env: JNI interface pointer.

Length: indicates the length of the array.

RETURNS:

Returns a Java array, or NULL if an array cannot be constructed.

13.6 GetArrayElements

NativeType *Get<PrimitiveType>ArrayElements(JNIEnv *env, ArrayType array, jboolean *isCopy);
Copy the code

A series of functions that return the body of the basic array. The results are valid until the corresponding ReleaseArrayElements() function is called. Because the returned array may be a copy of a Java array, changes made to the returned array before ReleaseArrayElements() is called are not necessarily reflected in the original array.

If isCopy is not NULL, and if a copy is made, *isCopy is set to JNI_TRUE; If there is no replication, set it to JNI_FALSE.

The following table describes the specific accessors for primitive array elements. You should make the following substitution:

  • Replace GetArrayElements with an actual original element accessor routine name in the table below.
  • Replace ArrayType with the corresponding ArrayType.
  • Replace NativeType with the corresponding NativeType of the routine.

Regardless of how Boolean arrays are represented in the JVM, GetBooleanArrayElements() always returns a pointer to JBooleans, one element per byte (unpacked representation). All arrays of other types are guaranteed to be contiguous in memory.

GetArrayElements routines An array type The local type
GetBooleanArrayElements() jbooleanArray jboolean
GetByteArrayElements() jbyteArray jbyte
GetCharArrayElements() jcharArray jchar
GetShortArrayElements() jshortArray jshort
GetIntArrayElements() jintArray jint
GetLongArrayElements() jlongArray jlong
GetFloatArrayElements() jfloatArray jfloat
GetDoubleArrayElements() jdoubleArray jdouble

LINKAGE:

JNIEnv index in the interface function table.

GetArrayElements routines Index
GetBooleanArrayElements() 183
GetByteArrayElements() 184
GetCharArrayElements() 185
GetShortArrayElements() 186
GetIntArrayElements() 187
GetLongArrayElements() 188
GetFloatArrayElements() 189
GetDoubleArrayElements() 190

PARAMETERS:

Env: JNI interface pointer.

Array: a Java string object.

IsCopy: pointer to a Boolean value.

RETURNS:

Returns a pointer to an array element, or NULL on failure.

13.7 ReleaseArrayElements

void Release<PrimitiveType>ArrayElements(JNIEnv *env, ArrayType array, NativeType *elems, jint mode);
Copy the code

A series of functions that inform the VM that native code no longer needs to access ElEMS. The elems parameter is a pointer derived from an array using the corresponding GetArrayElements() function. If necessary, this function copies all changes made to Elems back into the original array.

The mode parameter provides information about how to free the array buffer. Mode is invalid if elems is not a copy of the element in the array. Otherwise, the schema has the following effects, as shown in the following table:

model behavior
0 Copy back the content and release the Elems buffer
JNI_COMMIT Copy back the content, but do not release the Elems buffer
JNI_ABORT Release buffers without copying back possible changes

In most cases, the programmer passes “0” to the mode parameter to ensure consistent behavior between fixing and copying arrays. Other options give programmers more control over memory management and should be used with great caution.

The following table describes the specific routines that make up the original array handler family. You should make the following substitution:

  • Replace ReleaseArrayElements with an actual raw array handler routine name in the table below.
  • Replace ArrayType with the corresponding ArrayType.
  • Replace NativeType with the corresponding NativeType of the routine.
ReleaseArrayElements An array type The local type
ReleaseBooleanArrayElements() jbooleanArray jboolean
ReleaseByteArrayElements() jbyteArray jbyte
ReleaseCharArrayElements() jcharArray jchar
ReleaseShortArrayElements() jshortArray jshort
ReleaseIntArrayElements() jintArray jint
ReleaseLongArrayElements() jlongArray jlong
ReleaseFloatArrayElements() jfloatArray jfloat
ReleaseDoubleArrayElements() jdoubleArray jdouble

LINKAGE:

JNIEnv index in the interface function table.

ReleaseArrayElements The index
ReleaseBooleanArrayElements() 191
ReleaseByteArrayElements() 192
ReleaseCharArrayElements() 193
ReleaseShortArrayElements() 194
ReleaseIntArrayElements() 195
ReleaseLongArrayElements() 196
ReleaseFloatArrayElements() 197
ReleaseDoubleArrayElements() 198

PARAMETERS:

Env: JNI interface pointer.

Array: a Java array object.

Elems: Pointer to an array element.

Mode: indicates the release mode.

13.8 GetArrayRegion

void Get<PrimitiveType>ArrayRegion(JNIEnv *env, ArrayType array, jsize start, jsize len, NativeType *buf);
Copy the code

A set of functions that copy a region of the original array into a buffer.

The following table describes the specific accessors for primitive array elements. You should make the following substitutions:

  • Replace GetArrayRegion with an actual original element accessor routine name in the table below.
  • Replace ArrayType with the corresponding ArrayType.
  • Replace NativeType with the corresponding NativeType of the routine.
GetArrayRegion An array type The local type
GetBooleanArrayRegion() jbooleanArray jboolean
GetByteArrayRegion() jbyteArray jbyte
GetCharArrayRegion() jcharArray jchar
GetShortArrayRegion() jshortArray jshort
GetIntArrayRegion() jintArray jint
GetLongArrayRegion() jlongArray jlong
GetFloatArrayRegion() jfloatArray jloat
GetDoubleArrayRegion() jdoubleArray jdouble

LINKAGE:

JNIEnv index in the interface function table.

GetArrayRegion The index
GetBooleanArrayRegion() 199
GetByteArrayRegion() 200
GetCharArrayRegion() 201
GetShortArrayRegion() 202
GetIntArrayRegion() 203
GetLongArrayRegion() 204
GetFloatArrayRegion() 205
GetDoubleArrayRegion() 206

PARAMETERS:

Env: JNI interface pointer.

Array: a Java array.

Start: indicates the start index.

Len: Number of elements to copy.

Buf: target buffer.

THROWS:

ArrayIndexOutOfBoundsException: if an index of the area of the invalid.

13.9 SetArrayRegion

void Set<PrimitiveType>ArrayRegion(JNIEnv *env, ArrayType array, jsize start, jsize len, const NativeType *buf);
Copy the code

A set of functions used to copy back a region of the original array from the buffer.

The following table describes the specific accessors for primitive array elements. You should make the following substitution:

Replace the SetArrayRegion with an actual original element accessor routine name in the table below. Replace ArrayType with the corresponding ArrayType. Replace NativeType with the corresponding NativeType of the routine.

SetArrayRegion An array type The local type
SetBooleanArrayRegion() jbooleanArray jboolean
SetByteArrayRegion() jbyteArray jbyte
SetCharArrayRegion() jcharArray jchar
SetShortArrayRegion() jshortArray jshort
SetIntArrayRegion() jintArray jint
SetLongArrayRegion() jlongArray jlong
SetFloatArrayRegion() jfloatArray jfloat
SetDoubleArrayRegion() jdoubleArray jdouble

LINKAGE:

JNIEnv index in the interface function table.

SetArrayRegion The index
SetBooleanArrayRegion() 207
SetByteArrayRegion() 208
SetCharArrayRegion() 209
SetShortArrayRegion() 210
SetIntArrayRegion() 211
SetLongArrayRegion() 212
SetFloatArrayRegion() 213
SetDoubleArrayRegion() 214

PARAMETERS:

Env: JNI interface pointer.

Array: a Java array.

Start: indicates the start index.

Len: Number of elements to copy.

Buf: source buffer.

THROWS:

ArrayIndexOutOfBoundsException: if an index of the area of the invalid.

Note:

Starting with JDK/JRE 1.1, programmers can use the Get/ReleaseArrayElements function to Get Pointers to primitive array elements. If the VM supports fixation, a pointer to the raw data is returned; Otherwise, make copies. New features introduced starting with JDK/JRE 1.3 allow native code to get direct Pointers to array elements, even if the VM does not support fixation.

13.10 GetPrimitiveArrayCritical, ReleasePrimitiveArrayCritical

void * GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy);
void ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode);
Copy the code

The semantics of these two functions are very similar to the existing Get/ReleaseArrayElements functions. If possible, the VM returns a pointer to the original array; Otherwise, copy. However, there are significant limitations on how these functions can be used.

After calling GetPrimitiveArrayCritical, native code before calling ReleasePrimitiveArrayCritical shouldn’t run for a long time. We must think of the code in this pair of functions as running in a “critical region.” In critical areas, native code cannot call other JNI functions, nor can it call any system calls that might cause the current thread to block and wait for another Java thread. (For example, the current thread cannot call read on a stream that another Java thread is writing to.)

These limitations make it more likely that native code will get an uncopied version of the array, even if the VM does not support fixation. For example, when the local code holds a pointer to the array is acquired by GetPrimitiveArrayCritical, VM can temporarily disable garbage collection.

Can be nested to GetPrimtiveArrayCritical and ReleasePrimitiveArrayCritical more. Such as:

jint len = (*env)->GetArrayLength(env, arr1); jbyte *a1 = (*env)->GetPrimitiveArrayCritical(env, arr1, 0); jbyte *a2 = (*env)->GetPrimitiveArrayCritical(env, arr2, 0); /* We need to check if the VM is trying to make a copy. */ if (a1 == NULL || a2 == NULL) { ... /* memcpy(a1, a2, len); (*env)->ReleasePrimitiveArrayCritical(env, arr2, a2, 0); (*env)->ReleasePrimitiveArrayCritical(env, arr1, a1, 0);Copy the code

Please note that if the VM internal representation array of different formats, the GetPrimitiveArrayCritical might still have to generate a copy of the array. Therefore, we need to check whether the return value is NULL for possible out-of-memory situations.

LINKAGE (GetPrimitiveArrayCritical):

Link index 222 in the JNIEnv interface function table.

LINKAGE (ReleasePrimitiveArrayCritical):

The link index in the JNIEnv interface function table is 223.

SINCE:

The JDK/JRE 1.2

14. Register local methods

14.1 RegisterNatives

jint RegisterNatives(JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint nMethods);
Copy the code

Register local methods with the class specified by the Clazz parameter. The methods argument specifies an array of JNINativeMethod structures that contain the name, signature, and function pointer of the local method. The name and signature fields of the JNINativeMethod structure are Pointers to the modified UTF-8 string. The nMethods argument specifies the number of local methods in the array. JNINativeMethod structure is defined as follows:

typedef struct { 

    char *name; 

    char *signature; 

    void *fnPtr; 

} JNINativeMethod; 
Copy the code

Function Pointers must nominally have the following signature:

ReturnType (*fnPtr)(JNIEnv *env, jobject objectOrClass, ...) ;Copy the code

LINKAGE:

Index 215 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

Methods: local methods in a class.

NMethods: The number of local methods in a class.

RETURNS:

Returns “0” on success; Returns a negative value on failure.

THROWS:

NoSuchMethodError: if the specified method cannot be found or the method is not local.

14.2 UnregisterNatives

jint UnregisterNatives(JNIEnv *env, jclass clazz);
Copy the code

Unregister a local method of a class. The class returns to the state it was in before it was linked or registered with its local method functions.

This function should not be used in normal native code. Instead, it provides a way for special programs to reload and re-link local libraries.

LINKAGE:

Index 216 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Clazz: a Java class object.

RETURNS:

Returns “0” on success; Returns a negative value on failure.

15. Monitor Operation

15.1 MonitorEnter

jint MonitorEnter(JNIEnv *env, jobject obj);
Copy the code

Enter the monitor associated with the underlying Java object referenced by OBJ.

Each Java object has a monitor associated with it. If the current thread already owns a monitor associated with OBJ, it increases the counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with OBJ does not belong to any thread, the current thread becomes the owner of the monitor and sets the count of entries for this monitor to 1. If another thread already owns the monitor associated with OBJ, the current thread will wait until the monitor is released, and then try again to take ownership.

A monitor entered through the MonitorEnter JNI function call cannot be returned to exit using the Monitorexit Java virtual machine directive or the synchronized method. MonitorEnter JNI function calls and MonitorEnter Java virtual machine directives may compete to enter monitors associated with the same object.

To avoid deadlocks, the MonitorExit JNI call must be used to exit the monitor entered through the MonitorEnter JNI function call, unless the DetachCurrentThread call is used to implicitly release the JNI monitor.

LINKAGE:

Index 217 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: Plain Java object or class object.

RETURNS:

Returns “0” on success; Returns a negative value on failure.

15.2 MonitorExit

jint MonitorExit(JNIEnv *env, jobject obj);
Copy the code

The current thread must be the owner of the monitor associated with the underlying Java object referenced by OBJ. A thread decrement counter indicating how many times it has entered the monitor. If the value of the counter becomes zero, the current thread releases the monitor.

Native code may not use MonitorExit to exit a monitor entered through the synchronized method or monitorenter Java virtual machine directive.

LINKAGE:

Index 218 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Obj: Plain Java object or class object.

RETURNS:

Returns “0” on success; Returns a negative value on failure.

EXCEPTIONS:

Does not have any monitor IllegalMonitorStateException if the current thread.

16. NIO support

Nio-related entry points allow native code to access the Java.niO direct buffer. The contents of the direct buffer may exist in local memory outside of the normal garbage collection heap. For information about direct buffers, see the New I/O APIs and the specification of the Java.nio.byteBuffer class.

Three new functions introduced in JDK/JRE 1.4 allow JNI code to create, inspect, and manipulate direct buffers:

  • NewDirectByteBuffer
  • GetDirectBufferAddress
  • GetDirectBufferCapacity

Every implementation of the Java virtual machine must support these capabilities, but not every implementation needs to support JNI access to direct buffers. If the JVM does not support such access, the NewDirectByteBuffer and GetDirectBufferAddress functions must always return NULL, and the GetDirectBufferCapacity function must always return -1. If the JVM does support such access, these three functions must be implemented to return the appropriate values.

16.1 NewDirectByteBuffer

jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);
Copy the code

Allocate and return a direct java.nio.byteBuffer, which refers to a chunk of memory starting at address and extending capacity bytes.

Native code that calls this function and returns the generated byte-buffer object to Java-level code should ensure that the buffer references a valid region of memory available for reading and is written when appropriate. Attempting to access an invalid memory location from Java code returns an arbitrary value with no visible effect, or causes an unspecified exception to be thrown.

LINKAGE:

Index 229 in the JNIEnv interface function table.

PARAMETERS:

Env: indicates the JNIEnv interface pointer

Address: start address of memory area (cannot be NULL)

Capacity: Size of memory area (in bytes) (must be positive)

RETURNS:

Returns a local reference to the newly instantiated java.nio.byteBuffer object. If an exception occurs, or the virtual machine does not support JNI access to the direct buffer, NULL is returned.

EXCEPTIONS:

OutOfMemoryError: If the allocation of a ByteBuffer object fails

SINCE:

The JDK/JRE 1.4

16.2 GetDirectBufferAddress

void* GetDirectBufferAddress(JNIEnv* env, jobject buf);
Copy the code

Gets and returns the starting address of the memory region referenced by the given direct java.nio.buffer.

This function allows native code to access the same area of memory accessible to Java code through a buffer object.

LINKAGE:

Index 230 in the JNIEnv interface function table.

PARAMETERS:

Env: indicates the JNIEnv interface pointer

Buf: a direct java.nio.Buffer object (cannot be NULL)

RETURNS:

Returns the starting address of the memory region referenced by the buffer. NULL is returned if the memory region is not defined, if the given object is not direct java.nio.buffer, or if the virtual machine does not support JNI access to direct buffers.

SINCE:

The JDK/JRE 1.4

16.3 GetDirectBufferCapacity

jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf);
Copy the code

Gets and returns the capacity of the memory region referenced by the given direct java.nio.buffer. Capacity is the number of elements contained in a memory area.

LINKAGE:

Index 231 in the JNIEnv interface function table.

PARAMETERS:

Env: indicates the JNIEnv interface pointer

Buf: a direct java.nio.Buffer object (cannot be NULL)

RETURNS:

Returns the capacity of the memory region associated with the buffer. Returns -1 if the given object is not direct java.nio.buffer, if the object is an unaligned view Buffer and the processor architecture does not support unaligned access, or if the virtual machine does not support JNI access to the direct Buffer.

SINCE:

The JDK/JRE 1.4

Reflection support

If programmers know the name and type of a method or field, they can use JNI to invoke Java methods or access Java fields. The Java Core Reflection API allows programmers to introspect Java classes at run time. JNI provides a set of conversion functions between fields and method ids used in JNI for field and method objects used in the Java Core Reflection API.

17.1 FromReflectedMethod

jmethodID FromReflectedMethod(JNIEnv *env, jobject method);
Copy the code

The Java. Lang. Reflect the Method or Java. Lang. Reflect. The Constructor object into a Method ID.

LINKAGE:

Index 7 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

17.2 FromReflectedField

jfieldID FromReflectedField(JNIEnv *env, jobject field);
Copy the code

Convert java.lang.reflect.Field to a Field ID.

LINKAGE:

Index 8 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

17.3 ToReflectedMethod

jobject ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID methodID, jboolean isStatic);
Copy the code

Converts from CLS derived Method ID to Java. Lang. Reflect. The Method or Java. Lang. Reflect. The Constructor objects. If the method ID references a static field, isStatic must be set to JNI_TRUE, otherwise JNI_FALSE.

Throws OutOfMemoryError, or returns 0 on failure.

LINKAGE:

Index 9 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

17.4 ToReflectedField

jobject ToReflectedField(JNIEnv *env, jclass cls, jfieldID fieldID, jboolean isStatic);
Copy the code

Convert the Field ID derived from CLS to a java.lang.Reflect. Field object. If fieldID references a static field, isStatic must be set to JNI_TRUE, otherwise it must be set to JNI_FALSE.

Throws OutOfMemoryError, or returns 0 on failure.

LINKAGE:

Index 12 in the JNIEnv interface function table.

SINCE:

The JDK/JRE 1.2

JVM interface

18.1 GetJavaVM

jint GetJavaVM(JNIEnv *env, JavaVM **vm);
Copy the code

Returns the Java VM interface (used in the Invocation API) associated with the current thread. The result is placed where the second parameter vm points to.

LINKAGE:

Index 219 in the JNIEnv interface function table.

PARAMETERS:

Env: JNI interface pointer.

Vm: a pointer to where the result is placed.

RETURNS:

Returns “0” on success; Returns a negative value on failure.