jni引用類型
與基本數(shù)據(jù)類型不同,引用類型對(duì)原生方法是不透明的,它們內(nèi)部的數(shù)據(jù)結(jié)構(gòu)并不直接向原生代碼公開(kāi)
Java類型 原生類型
java.lang.Class jclass
java.lang.Throwable jthorwable
java.lang.String jstring
Other objects jobjects
java.lang.Object[] jobjectArray
boolean[] jbooleanArray
byte[] jbooleanArray
char[] jcharArray
short[] jshortArray
int[] jintArray
long[] jlongArray
float[] jfloatArray
double[] jdoubleArray
Other arrays Jarray
jni中基礎(chǔ)類型
基礎(chǔ)類型是typedef 從c類型定義的
jni類型和c類型一致
基本數(shù)據(jù)類型可以直接與C/C++的相應(yīng)基本數(shù)據(jù)類型映射,JNI用類型定義使得這種映射對(duì)開(kāi)發(fā)人員透明
Java類型 JNI類型 C/C++類型 大小
Boolean Jblloean unsigned char 無(wú)符號(hào)8位
Byte Jbyte char 有符號(hào)8位
Char Jchar unsigned short 無(wú)符號(hào)16位
Short Jshort short 有符號(hào)16位
Int Jint int 有符號(hào)32位
Long Jlong long long 有符號(hào)64位
Float Jfloat float 32位
Double Jdouble double 64位
字符串
jboolean b = false;
const char* pop_char = env->GetStringUTFChars(pop_file, &b);
數(shù)組
1、一維數(shù)組 jdoubleArray xy
jdouble* para_data = env->GetDoubleArrayElements(para_array, NULL);
2、二維數(shù)組
參數(shù)類型 jobjectArray jobjarr
jint rows = env->GetArrayLength(jobjarr);
for (int i = 0; i < rows; i++)
{
jdoubleArray jniarray = (jdoubleArray)env->GetObjectArrayElement(jobjarr, i);
jdouble*coldata = env->GetDoubleArrayElements(jniarray, NULL);
data_x[i] = coldata[0];
data_y[i] = coldata[1];
data_z[i] = coldata[2];
env->ReleaseDoubleArrayElements(jniarray, coldata, 0);
}
3、返回一維數(shù)組 jdoubleArray
jdoubleArray newIntArray = env->NewDoubleArray(3);
jdouble fill[3];
for (int i = 0; i < 3; i++) {
fill[i] = result[i]; // put whatever logic you want to populate the values here.
}
//把jint指針中的元素設(shè)置到j(luò)intArray對(duì)象中
env->SetDoubleArrayRegion(newIntArray, 0, 3, fill);
4、返回二維數(shù)組 jobjectArray
jobjectArray returnjArr;
jclass doubleArr = env->FindClass("[D");
returnjArr = env->NewObjectArray(rows, doubleArr, NULL);
for (int i = 0; i < rows; i++)
{
jdoubleArray colArr = env->NewDoubleArray(3);
jdouble fill[3];
for (int j = 0; j < 3; j++) {
fill[j] = result[i][j];
}
env->SetDoubleArrayRegion(colArr, 0, 3, fill);
env->SetObjectArrayElement(returnjArr, i, colArr);
env->DeleteLocalRef(colArr);
}
- 1. Introduction
- 2. Design Overview
- 3. JNI Types and Data Structures
-
4. JNI Functions
- Interface Function Table
- Version Information
- Class Operations
- Exceptions
- Global and Local References
- Weak Global References
- Object Operations
- Accessing Fields of Objects
- Calling Instance Methods
- Accessing Static Fields
- Calling Static Methods
- String Operations
-
Array Operations
- GetArrayLength
- NewObjectArray
- GetObjectArrayElement
- SetObjectArrayElement
- New<PrimitiveType>Array Routines
- Get<PrimitiveType>ArrayElements Routines
- Release<PrimitiveType>ArrayElements Routines
- Get<PrimitiveType>ArrayRegion Routines
- Set<PrimitiveType>ArrayRegion Routines
- GetPrimitiveArrayCritical, ReleasePrimitiveArrayCritical
- Registering Native Methods
- Monitor Operations
- NIO Support
- Reflection Support
- Java VM Interface
- 5. The Invocation API