1 JNI概述
JNI是java native interface的缩写,是用来从java调用C++/C代码,也可以从C++/C调用Java代码。
在jni.h中可以看到
class _Jv_JavaVM
{
public:
const struct JNIInvokeInterface *functions;
jint DestroyJavaVM ()
{ return functions->DestroyJavaVM (this); }
jint AttachCurrentThread (void **penv, void *args)
{ return functions->AttachCurrentThread (this, penv, args); }
jint DetachCurrentThread ()
{ return functions->DetachCurrentThread (this); }
jint GetEnv (void **penv, jint version)
{ return functions->GetEnv (this, penv, version); }
jint AttachCurrentThreadAsDaemon (void **penv, void *args)
{ return functions->AttachCurrentThreadAsDaemon (this, penv, args); }
};
typedef struct _Jv_JavaVM JavaVM;
struct JNINativeInterface{
void *reserved0; // 保留字0
void *reserved1; // 保留字1
void *reserved2; // 保留字2
void *reserved3; // 保留字3
//以是java虚拟机提供的一些接口函数,Java和C++/C的相互调用主要就是依赖于这些接口。
// function declaration
jint (JNICALL *GetVersion) (JNIEnv *);
jclass (JNICALL *DefineClass) (JNIEnv *, const char *,
jobject, const jbyte*, jsize); // typedef jint jsize
jclass (JNICALL *FindClass) (JNIEnv*, const char*);
jmethodID(JNICALL *FromReflectedMethod) (JNIEnv*, jobject);
jfieldID(JNICALL *FromReflectedField) (JNIEnv*, jobject);
jobject(JNICALL *toReflectedMethod) (JNIEnv*, jclass, jmethodID, jboolean);
jclass (JNICALL *GetSuperclass) (JNIEnv*, jclass);
jboolean (JNICALL *IsAssignableFrom) (JNIEnv *, jclass, jclass);
jobject (JNICALL *ToReflectedField )(JNIEnv*, jclass, jfieldID, jboolean);
jint (JNICALL *Throw) (JNIEnv *, jthrowable);
jint (JNICALL *ThrowaNew) (JNIEnv *, jclass, const char *);
jthrowable (JNICALL *ExceptionOccurred) (JNIEnv*);
void (JNICALL *ExceptionDescribe)(JNIEnv *);
void (JNICALL *ExceptionClear)(JNIEnv *);
void (JNICALL *FatalError) (JNIEnv*, const char *);
jint (JNICALL *Throw) (JNIEnv*, jclass, jfieldID, jboolean);
jint (JNICALL *ThrowNew) (JNIEnv*, jclass, const char*);
//局部帧
jint (JNICALL *PushLocalFrame) (JNIEnv*,jint);
jobject (JNICALL *PopLocalFrame)(JNIEnv*, jobject);
jobject (JNICALL *NewGlobalRef) (JNIEnv*, jobject);
void (JNICALL *DeleteGlobalRef) (JNIEnv*, jobject);
void (JNICALL *DeleteLocalRef) (JNIEnv*, jobject);
jboolean (JNICALL *IsSameObject) (JNIEnv*, jobject, jobject); //函数指针
jobject (JNICALL *AllocObject) (JNIEnv*, jclass);
jobject (JNICALL *NewObject)(JNIEnv*, jclass, jmethodID, ...);
jobject (JNICALL *NewObjectV)(JNIEnv*, jclass, jmethodID, va_list);
jobject (JNICALL *NewObjectA)(JNIEnv *, jclass,jmethodID, jvalue*);
jclass (JNICALL *GetObjectClass)(JNIEnv *, jobject);
jboolean (JNICALL *IsInstanceOf)(JNIEnv*, jobject, jclass);
jmethodID (JNICALL *GetMethodID) (JNIEnv*, jclass, const char*, const char*);
jobject (JNICALL *CallObjectMethod) (JNIEnv*, jobject, jmethodID, ...);
jobject (JNICALL *CallObjectMethodV) (JNIEnv*, jobject, jmethodID, va_list);
jobject (JNICALL *CallObjectMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jboolean (JNICALL *CallBooleanMethod) (JNIEnv *, jobject, jmethodID, ...);
jboolean (JNICALL *CallBooleanMethodV) (JNIEnv *, jobject, jmethodID,
va_list);
jboolean (JNICALL *CallBooleanMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jbyte (JNICALL *CallByteMethod) (JNIEnv *, jobject, jmethodID, ...);
jbyte (JNICALL *CallByteMethodV) (JNIEnv *, jobject, jmethodID, va_list);
jbyte (JNICALL *CallByteMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jchar (JNICALL *CallCharMethod) (JNIEnv *, jobject, jmethodID, ...);
jchar (JNICALL *CallCharMethodV) (JNIEnv *, jobject, jmethodID, va_list);
jchar (JNICALL *CallCharMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jshort (JNICALL *CallShortMethod) (JNIEnv *, jobject, jmethodID, ...);
jshort (JNICALL *CallShortMethodV) (JNIEnv *, jobject, jmethodID, va_list);
jshort (JNICALL *CallShortMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jint (JNICALL *CallIntMethod) (JNIEnv *, jobject, jmethodID, ...);
jint (JNICALL *CallIntMethodV) (JNIEnv *, jobject, jmethodID,va_list);
jint (JNICALL *CallIntMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jlong (JNICALL *CallLongMethod) (JNIEnv *, jobject, jmethodID, ...);
jlong (JNICALL *CallLongMethodV) (JNIEnv *, jobject, jmethodID, va_list);
jlong (JNICALL *CallLongMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jfloat (JNICALL *CallFloatMethod) (JNIEnv *, jobject, jmethodID, ...);
jfloat (JNICALL *CallFloatMethodV) (JNIEnv *, jobject, jmethodID, va_list);
jfloat (JNICALL *CallFloatMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jdouble (JNICALL *CallDoubleMethod) (JNIEnv *, jobject, jmethodID, ...);
jdouble (JNICALL *CallDoubleMethodV) (JNIEnv *, jobject, jmethodID, va_list);
jdouble (JNICALL *CallDoubleMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
void (JNICALL *CallVoidMethod) (JNIEnv *, jobject, jmethodID, ...);
void (JNICALL *CallVoidMethodV) (JNIEnv *, jobject, jmethodID, va_list);
void (JNICALL *CallVoidMethodA) (JNIEnv *, jobject, jmethodID, jvalue *);
jobject (JNICALL *CallNonvirtualObjectMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jobject (JNICALL *CallNonvirtualObjectMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jobject (JNICALL *CallNonvirtualObjectMethodA) (JNIEnv *, jobject, jclass,jmethodID, jvalue *);
jboolean (JNICALL *CallNonvirtualBooleanMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jboolean (JNICALL *CallNonvirtualBooleanMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jboolean (JNICALL *CallNonvirtualBooleanMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jbyte (JNICALL *CallNonvirtualByteMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jbyte (JNICALL *CallNonvirtualByteMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jbyte (JNICALL *CallNonvirtualByteMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jchar (JNICALL *CallNonvirtualCharMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jchar (JNICALL *CallNonvirtualCharMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jchar (JNICALL *CallNonvirtualCharMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jshort (JNICALL *CallNonvirtualShortMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jshort (JNICALL *CallNonvirtualShortMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jshort (JNICALL *CallNonvirtualShortMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jint (JNICALL *CallNonvirtualIntMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jint (JNICALL *CallNonvirtualIntMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jint (JNICALL *CallNonvirtualIntMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jlong (JNICALL *CallNonvirtualLongMethod) (JNIEnv *, jobject, jclass,jmethodID, ...);
jlong (JNICALL *CallNonvirtualLongMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jlong (JNICALL *CallNonvirtualLongMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jfloat (JNICALL *CallNonvirtualFloatMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
jfloat (JNICALL *CallNonvirtualFloatMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
jfloat (JNICALL *CallNonvirtualFloatMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jdouble (JNICALL *CallNonvirtualDoubleMethod) (JNIEnv *, jobject, jclass,jmethodID, ...);
jdouble (JNICALL *CallNonvirtualDoubleMethodV) (JNIEnv *, jobject, jclass,jmethodID, va_list);
jdouble (JNICALL *CallNonvirtualDoubleMethodA) (JNIEnv *, jobject, jclass,jmethodID, jvalue *);
void (JNICALL *CallNonvirtualVoidMethod) (JNIEnv *, jobject, jclass, jmethodID, ...);
void (JNICALL *CallNonvirtualVoidMethodV) (JNIEnv *, jobject, jclass, jmethodID, va_list);
void (JNICALL *CallNonvirtualVoidMethodA) (JNIEnv *, jobject, jclass, jmethodID, jvalue *);
jfieldID (JNICALL *GetFieldID) (JNIEnv *, jclass, const char *, const char *);
jobject (JNICALL *GetObjectField) (JNIEnv *, jobject, jfieldID);
jboolean (JNICALL *GetBooleanField) (JNIEnv *, jobject, jfieldID);
jbyte (JNICALL *GetByteField) (JNIEnv *, jobject, jfieldID);
jchar (JNICALL *GetCharField) (JNIEnv *, jobject, jfieldID);
jshort (JNICALL *GetShortField) (JNIEnv *, jobject, jfieldID);
jint (JNICALL *GetIntField) (JNIEnv *, jobject, jfieldID);
jlong (JNICALL *GetLongField) (JNIEnv *, jobject, jfieldID);
jfloat (JNICALL *GetFloatField) (JNIEnv *, jobject, jfieldID);
jdouble (JNICALL *GetDoubleField) (JNIEnv *, jobject, jfieldID);
void (JNICALL *SetObjectField) (JNIEnv *, jobject, jfieldID, jobject);
void (JNICALL *SetBooleanField) (JNIEnv *, jobject, jfieldID, jboolean);
void (JNICALL *SetByteField) (JNIEnv *, jobject, jfieldID, jbyte);
void (JNICALL *SetCharField) (JNIEnv *, jobject, jfieldID, jchar);
void (JNICALL *SetShortField) (JNIEnv *, jobject, jfieldID, jshort);
void (JNICALL *SetIntField) (JNIEnv *, jobject, jfieldID, jint);
void (JNICALL *SetLongField) (JNIEnv *, jobject, jfieldID, jlong);
void (JNICALL *SetFloatField) (JNIEnv *, jobject, jfieldID, jfloat);
void (JNICALL *SetDoubleField) (JNIEnv *, jobject, jfieldID, jdouble);
jmethodID (JNICALL *GetStaticMethodID) (JNIEnv *, jclass, const char *, const char *);
jobject (JNICALL *CallStaticObjectMethod) (JNIEnv *, jclass, jmethodID, ...);
jobject (JNICALL *CallStaticObjectMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jobject (JNICALL *CallStaticObjectMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jboolean (JNICALL *CallStaticBooleanMethod) (JNIEnv *, jclass, jmethodID, ...);
jboolean (JNICALL *CallStaticBooleanMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jboolean (JNICALL *CallStaticBooleanMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jbyte (JNICALL *CallStaticByteMethod) (JNIEnv *, jclass, jmethodID, ...);
jbyte (JNICALL *CallStaticByteMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jbyte (JNICALL *CallStaticByteMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jchar (JNICALL *CallStaticCharMethod) (JNIEnv *, jclass, jmethodID, ...);
jchar (JNICALL *CallStaticCharMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jchar (JNICALL *CallStaticCharMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jshort (JNICALL *CallStaticShortMethod) (JNIEnv *, jclass, jmethodID, ...);
jshort (JNICALL *CallStaticShortMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jshort (JNICALL *CallStaticShortMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jint (JNICALL *CallStaticIntMethod) (JNIEnv *, jclass, jmethodID, ...);
jint (JNICALL *CallStaticIntMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jint (JNICALL *CallStaticIntMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jlong (JNICALL *CallStaticLongMethod) (JNIEnv *, jclass, jmethodID, ...);
jlong (JNICALL *CallStaticLongMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jlong (JNICALL *CallStaticLongMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jfloat (JNICALL *CallStaticFloatMethod) (JNIEnv *, jclass, jmethodID, ...);
jfloat (JNICALL *CallStaticFloatMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jfloat (JNICALL *CallStaticFloatMethodA) (JNIEnv *, jclass, jmethodID,jvalue *);
jdouble (JNICALL *CallStaticDoubleMethod) (JNIEnv *, jclass, jmethodID, ...);
jdouble (JNICALL *CallStaticDoubleMethodV) (JNIEnv *, jclass, jmethodID, va_list);
jdouble (JNICALL *CallStaticDoubleMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
void (JNICALL *CallStaticVoidMethod) (JNIEnv *, jclass, jmethodID, ...);
void (JNICALL *CallStaticVoidMethodV) (JNIEnv *, jclass, jmethodID, va_list);
void (JNICALL *CallStaticVoidMethodA) (JNIEnv *, jclass, jmethodID, jvalue *);
jfieldID (JNICALL *GetStaticFieldID) (JNIEnv *, jclass, const char *, const char *);
jobject (JNICALL *GetStaticObjectField) (JNIEnv *, jclass, jfieldID);
jboolean (JNICALL *GetStaticBooleanField) (JNIEnv *, jclass, jfieldID);
jbyte (JNICALL *GetStaticByteField) (JNIEnv *, jclass, jfieldID);
jchar (JNICALL *GetStaticCharField) (JNIEnv *, jclass, jfieldID);
jshort (JNICALL *GetStaticShortField) (JNIEnv *, jclass, jfieldID);
jint (JNICALL *GetStaticIntField) (JNIEnv *, jclass, jfieldID);
jlong (JNICALL *GetStaticLongField) (JNIEnv *, jclass, jfieldID);
jfloat (JNICALL *GetStaticFloatField) (JNIEnv *, jclass, jfieldID);
jdouble (JNICALL *GetStaticDoubleField) (JNIEnv *, jclass, jfieldID);
void (JNICALL *SetStaticObjectField) (JNIEnv *, jclass,jfieldID, jobject);
void (JNICALL *SetStaticBooleanField) (JNIEnv *, jclass,jfieldID, jboolean);
void (JNICALL *SetStaticByteField) (JNIEnv *, jclass,jfieldID, jbyte);
void (JNICALL *SetStaticCharField) (JNIEnv *, jclass,jfieldID, jchar);
void (JNICALL *SetStaticShortField) (JNIEnv *, jclass,jfieldID, jshort);
void (JNICALL *SetStaticIntField) (JNIEnv *, jclass,jfieldID, jint);
void (JNICALL *SetStaticLongField) (JNIEnv *, jclass, jfieldID, jlong);
void (JNICALL *SetStaticFloatField) (JNIEnv *, jclass, jfieldID, jfloat);
void (JNICALL *SetStaticDoubleField) (JNIEnv *, jclass, jfieldID, jdouble);
jstring (JNICALL *NewString) (JNIEnv *, const jchar *, jsize);
jsize (JNICALL *GetStringLength) (JNIEnv *, jstring);
const jchar * (JNICALL *GetStringChars) (JNIEnv *, jstring, jboolean *);
void (JNICALL *ReleaseStringChars) (JNIEnv *, jstring, const jchar *);
jstring (JNICALL *NewStringUTF) (JNIEnv *, const char *);
jsize (JNICALL *GetStringUTFLength) (JNIEnv *, jstring);
const char * (JNICALL *GetStringUTFChars) (JNIEnv *, jstring, jboolean *);
void (JNICALL *ReleaseStringUTFChars) (JNIEnv *, jstring, const char *);
jsize (JNICALL *GetArrayLength) (JNIEnv *, jarray);
jobjectArray (JNICALL *NewObjectArray) (JNIEnv *, jsize, jclass, jobject);
jobject (JNICALL *GetObjectArrayElement) (JNIEnv *, jobjectArray, jsize);
void (JNICALL *SetObjectArrayElement) (JNIEnv *, jobjectArray, jsize,
jobject);
jbooleanArray (JNICALL *NewBooleanArray) (JNIEnv *, jsize);
jbyteArray (JNICALL *NewByteArray) (JNIEnv *, jsize);
jcharArray (JNICALL *NewCharArray) (JNIEnv *, jsize);
jshortArray (JNICALL *NewShortArray) (JNIEnv *, jsize);
jintArray (JNICALL *NewIntArray) (JNIEnv *, jsize);
jlongArray (JNICALL *NewLongArray) (JNIEnv *, jsize);
jfloatArray (JNICALL *NewFloatArray) (JNIEnv *, jsize);
jdoubleArray (JNICALL *NewDoubleArray) (JNIEnv *, jsize);
jboolean * (JNICALL *GetBooleanArrayElements) (JNIEnv *, jbooleanArray, jboolean *);
jbyte * (JNICALL *GetByteArrayElements) (JNIEnv *, jbyteArray, jboolean *);
jchar * (JNICALL *GetCharArrayElements) (JNIEnv *, jcharArray, jboolean *);
jshort * (JNICALL *GetShortArrayElements) (JNIEnv *, jshortArray, jboolean *);
jint * (JNICALL *GetIntArrayElements) (JNIEnv *, jintArray, jboolean *);
jlong * (JNICALL *GetLongArrayElements) (JNIEnv *, jlongArray, jboolean *);
jfloat * (JNICALL *GetFloatArrayElements) (JNIEnv *, jfloatArray, jboolean *);
jdouble * (JNICALL *GetDoubleArrayElements) (JNIEnv *, jdoubleArray,jboolean *);
void (JNICALL *ReleaseBooleanArrayElements) (JNIEnv *, jbooleanArray, jboolean *, jint);
void (JNICALL *ReleaseByteArrayElements) (JNIEnv *, jbyteArray, jbyte *, jint);
void (JNICALL *ReleaseCharArrayElements) (JNIEnv *, jcharArray, jchar *, jint);
void (JNICALL *ReleaseShortArrayElements) (JNIEnv *, jshortArray, jshort *, jint);
void (JNICALL *ReleaseIntArrayElements) (JNIEnv *, jintArray, jint *, jint);
void (JNICALL *ReleaseLongArrayElements) (JNIEnv *, jlongArray, jlong *, jint);
void (JNICALL *ReleaseFloatArrayElements) (JNIEnv *, jfloatArray, jfloat *, jint);
void (JNICALL *ReleaseDoubleArrayElements) (JNIEnv *, jdoubleArray, jdouble *, jint);
void (JNICALL *GetBooleanArrayRegion) (JNIEnv *, jbooleanArray, jsize, jsize, jboolean *);
void (JNICALL *GetByteArrayRegion) (JNIEnv *, jbyteArray, jsize, jsize, jbyte *);
void (JNICALL *GetCharArrayRegion) (JNIEnv *, jcharArray, jsize, jsize, jchar *);
void (JNICALL *GetShortArrayRegion) (JNIEnv *, jshortArray, jsize, jsize, jshort *);
void (JNICALL *GetIntArrayRegion) (JNIEnv *, jintArray, jsize, jsize, jint *);
void (JNICALL *GetLongArrayRegion) (JNIEnv *, jlongArray, jsize, jsize, jlong *);
void (JNICALL *GetFloatArrayRegion) (JNIEnv *, jfloatArray, jsize, jsize, jfloat *);
void (JNICALL *GetDoubleArrayRegion) (JNIEnv *, jdoubleArray, jsize, jsize, jdouble *);
void (JNICALL *SetBooleanArrayRegion) (JNIEnv *, jbooleanArray, jsize, jsize, jboolean *);
void (JNICALL *SetByteArrayRegion) (JNIEnv *, jbyteArray, jsize, jsize, jbyte *);
void (JNICALL *SetCharArrayRegion) (JNIEnv *, jcharArray, jsize, jsize, jchar *);
void (JNICALL *SetShortArrayRegion) (JNIEnv *, jshortArray, jsize, jsize, jshort *);
void (JNICALL *SetIntArrayRegion) (JNIEnv *, jintArray, jsize, jsize, jint *);
void (JNICALL *SetLongArrayRegion) (JNIEnv *, jlongArray, jsize, jsize, jlong *);
void (JNICALL *SetFloatArrayRegion) (JNIEnv *, jfloatArray, jsize, jsize, jfloat *);
void (JNICALL *SetDoubleArrayRegion) (JNIEnv *, jdoubleArray, jsize, jsize, jdouble *);
jint (JNICALL *RegisterNatives) (JNIEnv *, jclass, const JNINativeMethod *, jint);
jint (JNICALL *UnregisterNatives) (JNIEnv *, jclass);
jint (JNICALL *MonitorEnter) (JNIEnv *, jobject);
jint (JNICALL *MonitorExit) (JNIEnv *, jobject);
jint (JNICALL *GetJavaVM) (JNIEnv *, JavaVM **);
/* ---- JNI 1.2 functions ---- */
void (JNICALL *GetStringRegion) (JNIEnv *, jstring, jsize, jsize, jchar *);
void (JNICALL *GetStringUTFRegion) (JNIEnv *, jstring, jsize, jsize, char *);
void * (JNICALL *GetPrimitiveArrayCritical) (JNIEnv *, jarray, jboolean *);
void (JNICALL *ReleasePrimitiveArrayCritical) (JNIEnv *, jarray, void *, jint);
const jchar * (JNICALL *GetStringCritical) (JNIEnv *, jstring, jboolean *);
void (JNICALL *ReleaseStringCritical) (JNIEnv *, jstring, const jchar *);
jweak (JNICALL *NewWeakGlobalRef) (JNIEnv *, jobject);
void (JNICALL *DeleteWeakGlobalRef) (JNIEnv *, jweak);
jboolean (JNICALL *ExceptionCheck) (JNIEnv *);
jobject (JNICALL *NewDirectByteBuffer) (JNIEnv *, void *, jlong);
void * (JNICALL *GetDirectBufferAddress) (JNIEnv *, jobject);
jlong (JNICALL *GetDirectBufferCapacity) (JNIEnv *, jobject);
};
typedef struct _Jv_JNIEnv JNIEnv;
JNI使用了一种Java 和 C 函数的映射表数组,并在其中描述了函数的参数和返回值。这个数组的类型是JNINativeMethod,定义如下:
typedef struct {
const char* name;
const char* signature;
void* fnPtr;
} JNINativeMethod;
第一个变量name是Java中函数的名字。
第二个变量signature,用字符串是描述了函数的参数和返回值
第三个变量fnPtr是函数指针,指向C函数。
其中比较难以理解的是第二个参数,例如
"()V",表示函数无参数,返回类型为void
"(II)V"表示函数参数为int,int,返回类型为void
"(Ljava/lang/String;Ljava/lang/String;)V",表示函数参数是String,String,返回类型为void
实际上这些字符是与函数的参数类型一一对应的。
"()" 中的字符表示参数,后面的则代表返回值。
具体的每一个字符的对应关系如下
字符 Java类型 C类型
V void void
Z jboolean boolean
I jint int
J jlong long
D jdouble double
F jfloat float
B jbyte byte
C jchar char
S jshort short
数组则以"["开始,用两个字符表示
[I jintArray int[]
[F jfloatArray float[]
[B jbyteArray byte[]
[C jcharArray char[]
[S jshortArray short[]
[D jdoubleArray double[]
[J jlongArray long[]
[Z jbooleanArray boolean[]
上面的都是基本类型。如果Java函数的参数是class,则以"L"开头,以";"结尾中间是用"/" 隔开的包及类名。而其对应的C函数名的参数则为jobject. 一个例外是String类,其对应的类为jstring
Ljava/lang/String; String jstring
Ljava/net/Socket; Socket jobject
如果JAVA函数位于一个嵌入类,则用$作为类名间的分隔符。
例如 "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z"
阅读(2834) | 评论(0) | 转发(0) |