Summary
Jace is a free open-source tool that allows us to easily develop JNI (Java Native Interface) code. This article gives a detailed analysis of the JNI API and how to use Jace to solve these problems.
Without a deeper understanding, we would have thought that Sun was designing JNI to keep Java programmers from using it. After all, type safety is a fake, and without the error checking mechanism, a simple Java method call requires 4 or more JNI calls, which are obvious deficiencies of JNI. In addition, we must also manage jnienv pointers, which cannot be used in multiple threads, and must be selected for each possible operation in 9 function calls, and access to exception information is also very difficult. This is only part of the problem with JNI, and we can find many other problems.
Many of these restrictions are related to the binding of JNI and C, and the C language itself is very poorly supported for type safety and exception handling mechanisms. Although most programmers now have the ability to write code in C + +, Sun has not given up C programmers, which is why JNI is currently in this situation. Unfortunately, this difficult API poses a lot of difficulties for developers.
Jace is a free open source toolkit designed to make JNI programming easier. It supports the automatic generation of C + + proxy classes by Java class files and the integration of C + + and Java exceptions, arrays, packages, and objects, and manages the thread bindings and lifecycles of Java references. More importantly, it enables us to develop modules that are smaller, easier to understand, and type safe at compile time.
Type System of JNI
The most basic feature of Jace is that it uses C + + proxy classes to express Java types. To really understand the merits of the proxy class, we first need to look at the type system of JNI. Sun uses 24 types of C in JNI to represent all possible Java types. JNI contains 9 simple types:
Jboolean
Jbyte
Jchar
Jshort
Jint
Jlong
jdouble
jfloat
void
JNI has 14 types of references, as shown in the following illustration:
(Figure: PICTURE01)
In addition, JNI has a composite type jvalue that can express all the simple and reference types.