13.1 Summary of the JNI Functions
13.1.1 Directly-Exported Invocation Interface Functions(直接导出的调用接口函数)
A virtual machine implementation directly exports the following three functions as part of the invocation interface:
Note these are the only symbols exported for JNI by the VM.
//注意,这三个接口只是由VM为JNI导出的三个符号!!!不用用户调用!!!
1 JNI_GetDefaultJavaVMInitArgs
2 JNI_CreateJavaVM
3 JNI_GetCreatedJavaVMs
13.1.2 The JavaVM Interface(JavaVM接口)
The JavaVM interface is a pointer to a pointer to a function table.The first three entries in the function table are reserved,The remaining four entries are part of the invocation interface:
1 DestroyJavaVM
2 AttachCurrentThread
3 DetachCurrentThread
4 GetEnv
13.1.3 Functions Defined in Native Libraries(Native库中定义的函数)
When the virtual machine implementation
loads a native library
, it searches for and invokes the exported function entry
JNI_OnLoad
.
When the virtual machine implementation unloads a native library, it searches for and invokes the exported function entry JNI_OnUnload.
1 JNI_OnLoad
2
JNI_OnUnload
13.1.4 The JNIEnv Interface(JNIEnv接口)
A JNIEnv interface pointer is valid only in a particular thread,
the JavaVM interface pointer is valid for all threads in a virtual machine instance.
A JNIEnv interface pointer is a pointer to a pointer to
a function table
.
Seven entries are set NULL.The remainder of this chapter will cover all the entries in the JNIEnv interface in detail. For now we give a high-level overview.
[
我们先来做个总结
]:
(1)Version Information
1 GetVersion:返回JNIEnv interface的版本号
(2)
Class and Interface Operations
1 DefineClass:从一个buffer里定义一个类
2 FindClass:返回一个类的引用
类名:"java/lang/String" //class name
签名:"Ljava/lang/Object" //signature
env->FindClass("java/lang/String");
env->FindClass(HELLOLIBS_JNI_CLASS_NAME);
#define HELLOLIBS_JNI_CLASS_NAME "com/test/hello_libs/jni/JNIManager"
env->FindClass("java/lang/Integer");
env->FindClass("[I") //int[]
3 GetSuperclass:
返回基类,除了java.lang.Object类(因为java.lang.Object是最基类)
(3)
Exceptions
1 Throw and ThrowNew:
Throw:丢弃一个现有的异常对象,在当前线程触发一个新的异常
ThrowNew:在当前线程触发一个异常,并自定义输出异常信息
2 FatalError:打印信息并终止当前虚拟机实例
3
ExceptionCheck:
检查是否发生了异常,若有异常返回JNI_TRUE,否则返回JNI_FALSE
4 ExceptionClear:
清除异常堆栈信息
5 ExceptionDescribe:
打印异常的堆栈信息
6 ExceptionOccurred:
检查是否发生了异常,若有异常返回该异常的引用,否则返回NULL
(4)
Global and Local References
1 NewGlobalRef,
DeleteGlobalRef:创建/删除一个全局引用
输入参数可以是全局引用,弱全局引用,局部引用
2 NewWeakGlobalRef,DeleteWeakGlobalRef:弱全局引用
3 NewLocalRef,
DeleteLocalRef:局部引用
笔记[2019.08.26]:在 JNI 编程中避免内存泄漏
(5)
Object Operations
1 AllocObject:分配一个未初始化的对象
2 NewObject:分配一个对象并运行构造函数
3 GetObjectClass:返回某实例对应的类
4 IsInstanceOf:检查某对象是否为某类的实例
5 IsSameObject:检查两个引用是否指向同一个对象
(6)
Instance Field Access
1 GetFieldID:返回一个实例的域ID
2 Get<Type>Field,Set<Type>Field:获取一个实例的域/设置一个实例的域