Parse the local method to map the data types at the Java layer, and map the data types at the java layer.
Preface
The Java language defines different data types, such as basic types int and double, and all classes parent class objects. These are Java-level types, the processing process using local methods requires their corresponding types.
Rough Process
Local Methods written at the Java layer are compiled into bytecode by the compiler. bytecode records different types of parameters to the class file according to the specification, for example, B Indicates byte, I indicates int, J indicates long, and so on. The following local method is recorded as (Ljava/lang/Object; II) V.
public static native void test(Object o, int i, int i2);
The above method is registered in the JVM. When a local method is called, the data type will be converted according to the type ing, such as int-> jint and Object-> jobject. In fact, int and jint are the same in C ++, but another name is defined with typedef, while jobject is a pointer, the execution engine generates an Object when executing the Java layer logic. It has a special data structure on the JVM layer. Here, jobject is a pointer to this structure, when needed, you can forcibly convert the data structure to the JVM layer, and then perform operations on it. In addition, oop is used in JVM to represent object pointers.
Basic Type ing
Java Type |
Native Type |
Value |
Boolean |
Jboolean |
True or false |
Byte |
Jbyte |
-128 ~ 127 |
Short |
Jshort |
-Pow (2,15 )~ Pow (2, 15)-1 |
Int |
Jint |
-Pow (2,31 )~ Pow (2,31)-1 |
Long |
Jlong |
-Pow (2,63 )~ Pow (2,63)-1 |
Float |
Jfloat |
IEEE754 Standard Single-precision floating point number |
Double |
Jdouble |
IEEE754 standard double-precision floating point number |
Char |
Jchar |
16-bit unsigned, Unicode Character |
Reference Type ing
In addition to the basic type ing, other object types in the Java layer are reference types, so the local method corresponds to the jobject type. In addition, it also derives some frequently used subclasses, for example, jstring and jclass. The details are as follows,
class _jobject {};class _jclass : public _jobject {};class _jthrowable : public _jobject {};class _jstring : public _jobject {};class _jarray : public _jobject {};class _jbooleanArray : public _jarray {};class _jbyteArray : public _jarray {};class _jcharArray : public _jarray {};class _jshortArray : public _jarray {};class _jintArray : public _jarray {};class _jlongArray : public _jarray {};class _jfloatArray : public _jarray {};class _jdoubleArray : public _jarray {};class _jobjectArray : public _jarray {};
We can see that the _ jobject class is defined and the class is empty. Other classes include _ jclass _ jthrowable _ jstring _ jarray, which all inherit the _ jobject class. In addition, nine subclasses are derived from the array type, which correspond to the basic type array and the reference type array respectively.
After defining the class, define the pointer alias. Here is the type of the local method. In addition, these are definitions of C ++. If it is a C compiler, struct will be used to define _ jobject, rather than class.
typedef _jobject *jobject;typedef _jclass *jclass;typedef _jthrowable *jthrowable;typedef _jstring *jstring;typedef _jarray *jarray;typedef _jbooleanArray *jbooleanArray;typedef _jbyteArray *jbyteArray;typedef _jcharArray *jcharArray;typedef _jshortArray *jshortArray;typedef _jintArray *jintArray;typedef _jlongArray *jlongArray;typedef _jfloatArray *jfloatArray;typedef _jdoubleArray *jdoubleArray;typedef _jobjectArray *jobjectArray;
CPP empty class
The reference type above is defined as an empty class. Here we will understand the empty class of C ++. We usually need to define an empty class in the following two ways,
class Empty{}struct Empty{}
After the definition above, the size of the empty class is 1, but what is the use of an empty class if nothing exists? In fact, it can be used to distinguish different objects. Different objects defined by the empty class have different addresses, and objects operated by the new class also have different pointers, and the empty class can also distinguish different classes.
Pointer Conversion
So how can we associate these types of mappings? In fact, it is very simple. The answer is Pointer conversion. As mentioned above, Java-layer objects have certain data structures in JVM, that is, object pointers are represented by oop, jobject can be converted as follows. handle is of the jobject type.
oop result = *reinterpret_cast<oop*>(handle);
It is very convenient to convert it into oop. For example, you can use the klass to obtain metadata related to some classes.
Summary
Above, the types defined by the Java layer have the corresponding data types in the local method, and the source code of the Java layer is compiled into bytecode, And the type corresponding to the local method parameters is saved, during JVM execution, the corresponding types of methods can be converted according to different types. The types defined by local methods are empty classes. The main function is to bind objects and distinguish object types, you can access object or class metadata through pointer conversion whenever necessary.