Day Vision

Reading Everyday,Extending Vision

Android JNI Learning (IV) – Chinese API for common methods of JNI

2023-02-28 04:28:00


PS:GetMethodID()会造成还未初始化的类,进行初始化如果想获取构造函数的ID,请提供init作为方法名称,并将void(V)作为返回类型参数解释:env:JNI接口指针clazz:Java类对象name:以0结尾的,并且是"ut


This series of articles is as follows:

The mind map is as follows:


image.png

This article is mainly a combination

一、Interface Function Table(接口函数表)

Each function can pass

The table of virtual machine initialization functions, as shown in the following code, the first three entries are reserved for future COM compatibility.Also, we keep some extra NULL entries near the beginning of the function table, for example, future class-related JNI operations can be added after the FindClass instead of at the end of the table.Note that function tables can be shared among all JNI interface pointers.

Let's start with it

Let's talk about it in detail

二、获取JNI版本信息

In the JNIENV pointer, there is a function to get the version of JNI:

jint 

This method mainly returns the version information of the local JNI method interface.The return value is different in different JDK environments, as follows:

  • In JDK/JRE 1.1, return
  • In JDK/JRE 1.2, return
  • In JDK/JRE 1.3, return
  • In JDK/JRE 1.4, return

These numbers are not random shots, in fact, has long been defined as a macro, as follows:

Java class operations

(1) Define the class (load class)

jclass 

This function mainly loads the class from the buffer containing the data, which contains the original class data that was not referenced by the virtual machine when the class was called.

Explanation of the parameters:

  • env:JNI接口指针
  • name:所定义的类名或者接口名,该字符串有modefied UTF-8编码
  • loader: Assigned to the defined class loader
  • buf: contains the buffer of the .class file data
  • bufLen:buffer长度

Return: A Java class object that returns NULL when an error occurs

Exceptions that may be thrown:

  • If this Java class is not specified, it will be thrown
  • If a class/interface is a parent/parent interface of its own, it is thrown
  • If there is not enough memory, it is thrown
  • If you want to try to define a class in a Java package, it will be thrown

(二)、查找类

jclass 

There are two cases where one is JDK release 1.1 and the other is JDK release 1.2

Explanation of the parameters:

  • env:JNI接口指针
  • name: A fully qualified class name, that is, containing "package name" "/" class name.For example: such as

Return:

Exceptions that may be thrown:

  • If this Java class is not specified, it will be thrown
  • If it is a class/interface that is a parent of itself, it is thrown
  • If the class/interface definition is not found
  • If there is not enough memory, it is thrown

Find the parent class

jclass 

如果clazz不是Object类,则此函数将返回表示该clazz的父类的Class对象,如果该类是Object,或者clazz代表接口,则此函数返回NULL。

Explanation of the parameters:

  • ENV: JNI interface pointer
  • clazz: Java's Class class

返回:

(4) Safe conversion

jboolean 

判断clazz1的对象是否可以安全地转化为clazz2的对象

Explanation of the parameters:

  • env:JNI接口指针
  • clazz1: Java's Class class, i.e. the class that needs to be converted
  • clazz2: Java's Class class, i.e. the class that needs to be converted to the target

Return:

  • If clazz1 and clazz2 are the same Java class.
  • If clazz1 is a subclass of clazz2
  • If clazz1 is the implementation class of the clazz2 interface

Exception, operation

Throw an exception

jint 

You Pass in a jthrowable object and throw it in JNI

Explanation of the parameters:

  • env: JNI interface pointer
  • jthrowable:一个Java的java.lang.Throwable对象

返回:

Exceptions that may be thrown:

(2) Construct a new exception and throw it

jint 

传入一个message,并用其构造一个异常并且抛出。

Explanation of the parameters:

  • env:JNI接口指针
  • jthrowable: A Java java.lang.Throwable object
  • message: A message used to construct a java.lang.Throwable object, the string is encoded in modified UTF-8

Return:

Possible exceptions to throw:

(3) Check whether an exception has occurred and throw an exception

jthrowable 

Detects if an exception has occurred, and if so, returns a reference to the exception (before calling the ExceptionClear() function, or before Java handles the exception), or returns NULL if no exception has occurred.

入参解释:

  • env: JNI interface pointer

返回:

(4) print the stack information of the exception

Print the stack information for this exception

Explanation of the parameters:

  • env:JNI接口指针

(五)、清除异常的堆栈信息

Clear the exception being thrown, this function does not work if no exception is currently thrown

Explanation of the parameters:

  • env: JNI interface pointer

(六)、致命异常

Fatal Exception is used to output an exception message and terminate the current VM instance, that is, to exit the program.

Explanation of the parameters:

  • ENV: JNI interface pointer
  • MSG: Error message for the exception, encoded in modified UTF-8

(7) , just check if there is an exception

jboolean 

Check to see if an exception has occurred, and if it has, return JNI, otherwise return JNI

入参解释:

  • env:JNI接口指针

返回:

五、全局引用和局部引用

Create a global reference

jobject 

Create a global reference to the object obj, which can be global or local.Global references must be handled by DeleteGlobalRef() display.

Parameter interpretation:

  • env: JNI interface pointer
  • obj: object object

Return:

Delete global references

Remove global references

Explanation:

  • env: JNI interface pointer
  • globalRef: a global reference that needs to be deleted

(3) Delete local references

Local references are valid only for the lifetime of the local interface call and are automatically released when the local method returns. Each local reference consumes a certain amount of virtual machine resources, and while local references can be destroyed automatically, programmers need to be careful not to over-allocate local references in local methods, over-allocating local references can cause the virtual machine to overflow memory while executing local methods.

Deletes a local reference with localRef

Parameter interpretation

  • env:JNI接口指针
  • localRef: The local reference that needs to be removed

JDK/JRE 1.1 provides the DeleteLocalRef function above so that programmers can manually remove local references.

(4) setting the capacity of local variables

jint 

In the current thread, limit the number of local reference creations by passing in a capacity capacity.Success returns 0, otherwise a negative number is returned and an OutOfMemoryError is thrown.The VM automatically ensures that at least 16 local references can be created.

Parameter interpretation

  • env: JNI interface pointer
  • capacity: Capacity

Back:

For backward compatibility, if the virtual machine creates an out-of-capacity local reference. The VM calls FatalError to ensure that no more local references can be created. In debug mode, the virtual machine recalls the user issuing warning and prompts for more local references to be created, and in the JDK, the programmer can provide the-verbose: JNI command-line option to open the message

(5) Create a new frame on top of the old one

jint 

Re-create a local variable container when the local variable capacity has been set. Success returns 0, failure returns a negative number and throws an OutOfMemoryError exception.

Note: In the current local frame, the local reference created by the previous local frame is still valid

Parameter interpretation

  • ENV: JNI interface pointer
  • Capacity:

(6) Release a local reference

jobject PopLocalFrame(JNIEnv *env,jobject result)

Pops up the current local reference frame and releases all local references.Returns the local reference that corresponds to a given result object in the previous local reference frame.If no reference is returned, set result to NULL

Parameter interpretation

  • env:JNI接口指针
  • Result: a local reference that needs to be released

(vii) Create a local reference

jobject 

Create a local reference that references from the ref.ref can be global or local, or return NULL if ref is NULL.

Parameter interpretation

  • ENV: JNI interface pointer
  • Ref: you can try a local reference or a global reference.

(8) Weak global references

A weak global reference is a special kind of global reference, unlike a general global reference, a weak global reference allows the underlying Java object to be garbage collected.Weak global references can be applied wherever global or local references are used.When the garbage collector is running, it will free the underlying variable if the object is only referenced by a weak reference.A weak Ruan Ju reference points to a freed object equivalent to NULL.Programmers can detect whether a weak global application points to a freed object by using isSampleObject to contrast weak references and NULL.Weak global references in JNI are a simplified version of Java weak references that are valid in the Java Platform API.

While the Native method is running, the garbage collector may be working, and the object pointed to by the weak reference may be freed at any time.Weak global references can be applied wherever global references are used, and are usually not appropriate because they may program NULL when they are not careful.

When ISSAMPLEOBJECT is able to identify whether a weak global reference is pointing to a freed object, this does not prevent the object from being released immediately after detection. This means that the programmer can not rely on this method to identify whether a weak global reference can be used in subsequent JNI function calls.

If you want to solve the above problem, it is recommended to use the JNI function NewLocalRef or NewGlobalRef to point to the same object with standard global and local references. These functions return NULL if the exclusive has been freed. Otherwise, a strong reference is returned (which guarantees that the object will not be released) . When you do not need to access this object, the new reference must be explicitly deleted.

1. Create a global weak reference
jweak 

Create a new weak global reference. Returns NULL if OBJ points to NULL. If the VM overflows, an exception OutOfMemoryError is thrown.

Parameter interpretation

  • env:JNI接口指针
  • obj: Reference object

Back:

Remove global weak references

The VM deletes the corresponding resource based on the given weak global reference.

Parameter interpretation

  • env: JNI interface pointer
  • OBJ: a weak global reference to be deleted

六、对象操作

(1) Create a Java object directly

jobject 

Allocates a new Java object without using any constructors, returning a reference to the object.

Explanation:

  • env: JNI interface pointer
  • Clazz: : Java class object

Back:

Abnormal:

  • If the class is an interface or an abstract class, an InstantiationException is thrown
  • If it is a memory overflow, an OutOfMemoryError is thrown

(二)、根据某个构造函数来创建Java对象

jobject 

To construct a new Java object, methodID indicates that a constructor needs to be called.This ID must be obtained by calling GetMethodID(), GetMethodID() as the function name, void(V) as the return value.The clazz parameter cannot box an array class

  • : you need to put all the constructor input arguments after the parameter methodID. NewObject () takes these arguments and passes them to the Java constructor that needs to be called
  • :在methodID后面,放了一个类型为jvalue的参数数组——args,该数组存放着所有需要传递给构造函数的参数。NewObjectA()接收到这个数组中的所有参数,并且按照顺序将它们传递给需要调用的Java方法。
  • :在methodID后面,放了一个类型为va_list的args,参数存放着所有需要传递给构造函数的参数。NewObjectv()接收到所有的参数,并且按照顺序将它们传递给需要调用的Java方法。

Explanation:

  • ENV: JNI interface pointer
  • Clazz: : Java class
  • Methodid: Method ID of the constructor

Additional parameters:

  • Additional parameters to NewObject: arguments are arguments to the constructor
  • Additional arguments to NewObjectA: args is an array of arguments to the constructor
  • NewObjectV的附加参数:args是构造函数的参数list

Return:

Exception:

(三)、获取某个对象的“类”

jclass 

Returns the class corresponding to obj

Parameter interpretation

  • ENV: JNI interface pointer
  • obj:Java对象,不能为NULL

参数:

Back:

(4) Get the "type" of an object

jobjectRefType 

The obj parameter is returned so it points to the type of the object, and the parameter obj can be a local variable, a global variable, or if referenced globally.

Parameter interpretation

  • env:JNI接口指针
  • OBJ: local, global, or weak global references

Return:

  • JNI invalidreftype = 0: represents that the OBJ parameter is not a valid reference type
  • Jnilocalreftype = 1: represents the OBJ parameter as a local variable type
  • JNIGlobalRefType=2:代表obj参数是全局变量类型
  • JNIWeakGlobalRefType=3: Indicates that the obj parameter is a weak global valid reference

An invalid reference is a reference without a reference.That is, the pointer to obj does not point to the address in memory where the function was created, or has already returned from the JNI function.So NULL is an invalid reference.moreover

PS: this function can not be called while the reference is being deleted

(5) to determine whether an object is a subclass of a“Class”

jboolean 

Test if OBJ is an instance of clazz

参数:

  • env: JNI interface pointer
  • obj:一个Java对象
  • clazz:一个Java的类

Back:

(6) determine whether two references point to the same reference

jboolean 

Determine whether two references point to the same object

Explanation:

  • env:JNI接口指针
  • ref1: Java object
  • ref2: Java object

Return:

Return the attribute ID

jfieldID 

Gets the non-static attribute ID of a class

Explanation:

  • ENV: JNI interface pointer
  • Clazz: A Java class object
  • name: The property name that ends with "0" and the character type is "utf-8"
  • sig: Attribute signature ending with "0" and character type "utf-8"

Return

Abnormal:

PS:

Return the attribute ID series

NativeType 

Returns the value of a non-static property of a class, short for a set of functions, as follows:

jobject        GetObjectField(JNIEnv *env,jobject obj,jfieldID fielD)   
jboolean     GetBooleanField(JNIEnv *env,jobject obj,jfieldID fielD)
jbyte           GetByteField(JNIEnv *env,jobject obj,jfieldID fielD)
jchar           GetCharField(JNIEnv *env,jobject obj,jfieldID fielD)
jshort          GetShortField(JNIEnv *env,jobject obj,jfieldID fielD)
jint              GetIntField(JNIEnv *env,jobject obj,jfieldID fielD)
jlong           GetLongField(JNIEnv *env,jobject obj,jfieldID fielD)
jfloat           GetFloatField(JNIEnv *env,jobject obj,jfieldID fielD)
jdouble       GetDoubleField(JNIEnv *env,jobject obj,jfieldID fielD)

Explanation:

Back:

(九)、设置属性id系列

 Set

Sets the value of a non-static property of a class.Which of these properties passes

参数解释:

  • ENV: JNI interface pointer
  • obj: Java object, cannot be empty
  • fieldID: A valid attribute ID
  • Value: the new value of the property

Get a method ID for a class

jmethodID 

Returns the method ID of a class or interface, which can be defined in the parent class of CLAZZ and then inherited by clazz. We identify a method based on its name and signature.

PS:

Explanation:

  • ENV: JNI interface pointer
  • clazz: Java class object
  • Name: The method name of a 0-terminated, “Utf-8” string
  • sig: Method signature of a string ending in 0 and "utf-8"

Back:

Exception:

A“Family” of non-static methods that call an instance of Java

NativeType Call

These columns are all in

PS: when a“Private” function or constructor is called, the methodID must be a method of the OBJ class, not a method of its parent class.

Let's take a look at their differences

  • CallMethod: you need to put the
  • Callmethoda: Yes
  • CallMethodV:在
Call

Explanation:

  • env: JNI interface pointer
  • obj:对应的Java对象
  • methodID: The method ID of a method

Return:

Abnormal:

(12) Call a non-abstract method of a class

Call an instance method in the parent class, as follows

CallNonvirtual<type>Method 
CallNonvirtual<type>MethodA 
CallNonvirtual<type>MethodV 

The details are as follows:

NativeType CallNonvirtual

This series of operations is to call a non-static method of an instance of a Java object based on a specific class and its method ID, and the methodID parameter needs to be obtained by calling GetMethodID().

And Call & LT; Type & GT; Method

Let's look at their differences

  • Callnonvirtual & LT; Type & GT; Method: you need to put the
  • Callnonvirtual & LT; Type & GT; Method: in
  • CallNonvirtual<type>MethodV: In

Expand the above series of methods as follows:

CallNonvirtual

Parameter interpretation:

  • ENV: JNI interface pointer
  • obj:Java对象
  • clazz: Java class
  • methodID: The method ID

Back:

Throw an exception:

Get static properties

jfieldID 

Gets a static attribute ID of a class and determines which attribute it is based on the attribute name and label.

Parameter interpretation:

  • env: JNI interface pointer
  • Clazz: Java class
  • Name: the property name of the static property, which is an encoded format“UTF-8” and a 0-terminated string.
  • SIG: the signature of the attribute, which is a 0-terminated string in the encoding format“Utf-8”.

Back:

Abnormal:

(14) Obtain a static property series

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

This series returns the values of static properties of an object.Can pass

The function name and return value are shown below, so you only need to replace them

GetStatic<type>Field Routine Name      Native Type
GetStaticObjectField()                 jobject
GetStaticBooleanField()                jboolean
GetStaticByteField()                   jbyte
GetStaticCharField()                   jchar
GetStaticShortField()                  jshort
GetStaticIntField()                    jint
GetStaticLongField()                   jlong
GetStaticFloatField()                  jfloat
GetStaticDoubleField()                 jdouble

Explanation:

  • ENV: JNI interface pointer
  • Clazz: Java class
  • field: Static property ID

Return:

(十五)、设置静态属性系列

 SetStatic

This series is the value of the static property of the set class.Can pass

Below is a detailed description of the function name and its value, which you can access through the

SetStatic<type>Field Routine Name         NativeType
SetStaticObjectField()                    jobject
SetStaticBooleanField()                   jboolean
SetStaticByteField()                      jbyte
SetStaticCharField()                      jchar
SetStaticShortField()                     jshort
SetStaticIntField()                       jint
SetStaticLongField()                      jlong
SetStaticFloatField()                     jfloat
SetStaticDoubleField()                    jdouble

Explanation:

  • ENV: JNI interface pointer
  • Clazz: Java class
  • Field: static attribute ID
  • value: The value of the setting

(16) Obtain the static function ID

jmethodID 

Returns the static method ID of the class, which method is determined by its method name and signature.Call if the class has not been initialized

Parameter interpretation:

  • env: JNI interface pointer
  • clazz:Java类
  • name: The method name of the static method, encoded in "utf-8" and a string ending with 0
  • SIG: method signature, a string encoded in“UTF-8” and terminated by 0

Back:

Abnormal:

Call a series of static functions

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,jmethodID methodid, va_list args)

You can manipulate the static methods of a Java object based on the specified method ID. May Pass

Here's how to do it in detail

CallStatic

参数解释:

  • ENV: JNI interface pointer
  • Clazz: Java class
  • methodID:静态方法ID

Back:

异常:

7. String operations

(1) Create a string

jstring 

Parameter interpretation:

  • ENV: JNI interface pointer
  • unicodeChars: A pointer to a Unicode string
  • Len: the length of a Unicode string

Back:

Abnormal:

(2) Get the length of the string

jsize  

Returns the length of the Java string (number of unicode characters)

Parameter interpretation:

  • env:JNI接口指针
  • string: Java string object

Return:

Get a pointer to the string

 jchar

Returns a pointer to an array of Unicode characters of a string, which remains valid until the

Explanation:

  • ENV: JNI interface pointer
  • String: A Java string object
  • isCopy: A pointer to a Boolean value

Return:

Release the string

Through the VM, native code is no longer accessible

Explanation:

  • env: JNI interface pointer
  • String: A Java string object
  • chars: A pointer to a Unicode string

Create a UTF-8 string

jstring 

Create a UTF-8 string.

Explanation:

  • env:JNI接口指针
  • bytes: A pointer to a UTF-8 string

Return:

Exception:

Gets the length of a UTF-8 string

jsize 

以字节为单位,返回字符串UTF-8的长度。

Parameter interpretation:

  • env:JNI接口指针
  • String: Java string object

Return:

Gets a pointer to string utfchars

Returns a pointer to an array of UTF-8 characters, unless the array is used

Explanation:

  • env: JNI interface pointer
  • String: A Java String object
  • ISCOPY: a pointer to a Boolean value

Back:

(八)、释放UTFChars

With the virtual machine, native code no longer accesses UTF. UTF is a pointer that can be called

Parameter interpretation:

  • env: JNI interface pointer
  • String: A Java string object
  • utf: A pointer to the utf-8 string

注意:在JDK/JRE 1.1,程序员可以在用户提供的缓冲区获取基本类型数组元素,从JDK/JRE1.2开始,提供了额外方法,这些方法允许在用户提供的缓冲区获取Unicode字符(UTF-16编码)或者是UTF-8的字符。这些方法如下:

(九)、1.2新的字符串操作方法

1 Intercepts a string

In str (Unicode character), the len length is truncated from the start position and placed in buf.If it crosses bounds, a StringIndexOutOfBoundsException is thrown.

2 intercepts a string and converts it to UTF-8 format

将str(Unicode字符串)从start位置开始截取len长度并且将其转换为UTF-8编码,然后将结果防止在buf中。

3 takes a string and converts it to UTF-8 format
 jchar 

The above two functions are somewhat similar

PS:

  • 1. A GC started by another thread may be released from a block to continue execution only when the GC triggered by the current thread finishes blocking and releases the GC.
  • 2、在这个过程中,当前线程会一直阻塞,因为任何阻塞性调用都需要获取一个正在被其他线程持有的锁,而其他线程正等待GC。

Array operations

(1) Get the length of the array

jsize 

Returns the length of an array

Parameter interpretation:

  • ENV: JNI interface pointer
  • array: A Java array

Return:

Create an array of objects

jobjectArray 

Creates a new array of objects of type of its elements

Parameter interpretation:

  • env:JNI接口指针
  • length: The array size
  • elementClass: The array element class
  • initialElement: The initial value of the array element

返回:

Abnormal:

(3) Get an element in the array element

jobject 

Returns an element at a location within an element

参数解释:

  • env:JNI接口指针
  • array: A Java array
  • Index: Array Index

Return:

异常:

(4) Set the value of an element in the array

Sets the value of the index element subscripted.

Parameter interpretation:

  • env:JNI接口指针
  • array:Java数组
  • index:数组下标
  • value:数组元素的新值

Exception:

(五)、创建基本类型数组系列

ArrayType 

用于构造基本类型数组对象的一系列操作。下面说明了特定基本类型数组的创建函数。可以把New<PrimitiveType>Array替换为某个实际的基本类型数组创建函数 ,然后将ArrayType替换为相应的数组类型

PrimitiveType

参数解释:

  • env: JNI interface pointer
  • length: The length of the array

返回:

(六)、获取基本类型数组的中数组指针系列

NativeType 

An array pointer to a primitive type for a return type. The corresponding

A set of functions that return the body of an array of primitive types. The result will be valid until the corresponding release & LT; primitivetype & GT; arrayelements () function is called. Since the returned array may be a copy of the Java Array, changes to the returned array do not have to be reflected in the base type array until the“Release & LT; primitivetype & GT; arrayelements ()” function is called.

The following describes the specific functions for a particular primitive type of array element:

  • will
  • Replace ArrayType with the corresponding array type
  • Replace the NativeType with a local variable

不管布尔数组在Java虚拟机总如何表示,

Get

Explanation:

  • ENV: JNI interface pointer
  • Array: Java array
  • ISCOPY: a pointer to a Boolean value

返回:

Releasing Array series of basic types

 Release

Notifies the virtual machine Native that it no longer accesses the elements of the array.

If necessary, the function copies all transformations on the ELEMS to the original array elements.

The value of mode has the following three situations:

  • 0: Copy the contents and free the elems buffer
  • JNI: copies the content without releasing the ELEMS buffer
  • JNI: frees the buffer without copying possible changes

In most cases, the programmer passes“0” as a parameter, because this ensures consistent behavior for fixing and copying arrays. Other options give programmers better control over memory.

The specific functions of a particular primitive type array element are illustrated below:

  • Will
  • Replace ArrayType with the corresponding base array type
  • 将NativeType替换为本地变量

The following describes the details of primitive type array deallocation. You should make the following substitutions:

Release

参数解释:

  • ENV: JNI interface pointer
  • array: A Java array
  • elems: A pointer to an array of basic types
  • mode: Release mode

Copy the array series of past primitive types

 Get

Copies an array of primitive types to buff

The specific functions of a particular primitive type array element are illustrated below:

  • Will
  • Replace ArrayType with the corresponding base array type
  • Replace NativeType with a local variable
Get

Explanation:

  • env:JNI接口指针
  • Array: Java array
  • Start: Start the index
  • len: The length that needs to be copied
  • buf: Target buffer

Exception:

Copy the array of the basic type array back to the series

 Set

Mainly buffer copy of the basic type of array functions

The specific functions of a particular primitive type array element are illustrated below:

  • will
  • Replace the ArrayType with the corresponding base array type
  • Replace the NativeType with a local variable
Set

参数解释:

  • env: JNI interface pointer
  • array: A Java array
  • Start: Start the index
  • len: The length that needs to be copied
  • BUF: source buffer

Abnormal:

(十)、补充

Available from JDK/JER 1.1

虽然这两个函数与上面的

In the call

For example, the current thread cannot call the read function to read, and is being written to another stream.

IX System-level operations

(a-RRB- method of registration

jint 

Register the local method according to the clazz parameter, and the methods parameter formulates an array of JNINativeMethod structures, which contains the local method name, signature, and function pointer.The name and signature are pointers to the code "UTF-8"; The nMethod parameter indicates the number of local methods in the array.

Here's what to say

Parameter interpretation:

  • ENV: JNI interface pointer
  • clazz: Java class object
  • methods:类中的native方法
  • NMETHOD: the number of local methods in the class

Return;

异常:

(二) 注销方法

jint 

Unregister the local method. The class recycles state that has not been registered by the function before.This function can no longer be called in native code, and it provides a way for specific programs to reload and relink local libraries.

参数解释:

  • JNI: interface pointer
  • Clazz: Java class object

Return:

(iii) Monitoring operations

jint 

obj引用的底层Java对象关联的监视器。obj引用不能为空。每个Java对象都有一个相关的监视器。如果当前线程已经有关联到obj的监视器,它将添加监视器的计数器来表示这个线程进入监视器的次数。如果关联至obj的监视器不属于任何线程,那当前线程将变成该监视器的拥有者,并设置计数器为1,如果其他计数器已经拥有了这个监视器,当前线程将进行等待直到监视器被释放,然后再获得监视器的拥有权。

Pass

为了避免死锁,通过

Explanation:

  • ENV: JNI interface pointer
  • OBJ: a normal Java object or class object

Return:

(iv) Monitor exit

jint 

The current thread owns the monitor associated with the OBJ, and the thread reduces the value of the counter to indicate how many times the thread has entered the monitor. If the value of the counter changes to 0, the thread releases the monitor. Native code can not call it directly

Parameter interpretation:

  • env: JNI interface pointer
  • obj: Normal Java object or class object

返回:

Exception:

NIO operation

Nio-related operations allow direct access by Native code

A new JNI function was introduced in JDK/JRE 1.4 that allows for direct buffering of checks and operations

  • NewDirectByteBuffer
  • GetDirectBufferAddress
  • GetDirectBufferCapacity

每个Java虚拟机的实现都必须支持这些功能,但并不是每个实现都需要支持对直接缓冲区的JNI访问。如果JVM不支持这种访问,那么

Returns the ByteBuffer

jobject 

Assign and return a direct one

The Native code of the object that calls this function and returns a byte buffer must ensure that the buffer points to a reliable region of memory that can be read and written.Entering an illegal memory location may return any value, the DNA will not have an obvious impression, and it may throw an abnormality.

Explanation:

  • env: JNIEnv interface pointer
  • address: The starting address of the memory region
  • Capacity: the size of the memory area

Back:

Exception:

(二) 返回直接缓冲区中对象的初始地址

Gets and returns Java. exe. Nio. The initial memory address of Buffer

This function allows native code to access the same memory region of Java code through a direct buffer object

参数解释:

  • env: JNIEnv interface pointer
  • BUF: Java. java. Nio. The Buffer object

Back:

(3) returns the memory capacity of the object in the direct buffer

jlong 

Gets and returns the memory capacity of java.nio.buffer.This capacity is the number of elements that can be accommodated in the memory area

Parameter:

  • env: JNIEnv interface pointer
  • buf:java.nio.Buffer对象

Return:

11. Reflection support

If the programmer knows

Transformation to obtain method ID

jmethodID 

Converts the java.lang.reflect.Method or java.lang.reflect.Constructor object to a method ID

Parameter interpretation:

  • env:JNIEnv接口指针
  • Method: Java. java. Lang. Reflect. Method or Java. exe. Lang. Reflect. Constructor object

Return:

(2) transform to get attribute ID

jfield 

将java.lang.reflect.Field转化域ID

Explanation:

  • env: JNIEnv interface pointer
  • field: java.lang.reflect.Field object

返回:

(3) Reverse conversion and obtain method objects

jobject 

将源自cls的方法ID转化为

参数解释:

  • env: JNIEnv interface pointer
  • clazz: Java class object
  • methodID: The method ID corresponding to the Java class
  • isStatic: Whether it is a static method

返回

异常:

(4) invert and get attribute object

jobject ToReflectedField(JNIEnv *env,jclass cls,jfieldID field,jboolean isStatic)

Convert the attribute ID derived from cls to

Explanation:

  • env:JNIEnv接口指针
  • cls:Java类对象
  • methodID: The property ID corresponding to Java
  • Isstatic: whether it is a static property

Return:

Abnormal:

12. Get a virtual machine

jint 

Returns the Java virtual machine interface corresponding to the current thread.The returned results are saved in the VM.

Parameter interpretation:

  • env: JNI interface pointer
  • VM: Save the virtual machine pointer

Return:

Last article