1.参考官方文档
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html
3.函数列表
typedef const struct JNINativeInterface *JNIEnv;
const struct JNINativeInterface ... = { NULL,
NULL,
NULL,
NULL,
GetVersion, DefineClass,
FindClass, FromReflectedMethod,
FromReflectedField,
ToReflectedMethod, GetSuperclass,
IsAssignableFrom, ToReflectedField, Throw,
ThrowNew,
ExceptionOccurred,
ExceptionDescribe,
ExceptionClear,
FatalError, PushLocalFrame,
PopLocalFrame, NewGlobalRef,
DeleteGlobalRef,
DeleteLocalRef,
IsSameObject,
NewLocalRef,
EnsureLocalCapacity, AllocObject,
NewObject,
NewObjectV,
NewObjectA, GetObjectClass,
IsInstanceOf, GetMethodID, CallObjectMethod,
CallObjectMethodV,
CallObjectMethodA,
CallBooleanMethod,
CallBooleanMethodV,
CallBooleanMethodA,
CallByteMethod,
CallByteMethodV,
CallByteMethodA,
CallCharMethod,
CallCharMethodV,
CallCharMethodA,
CallShortMethod,
CallShortMethodV,
CallShortMethodA,
CallIntMethod,
CallIntMethodV,
CallIntMethodA,
CallLongMethod,
CallLongMethodV,
CallLongMethodA,
CallFloatMethod,
CallFloatMethodV,
CallFloatMethodA,
CallDoubleMethod,
CallDoubleMethodV,
CallDoubleMethodA,
CallVoidMethod,
CallVoidMethodV,
CallVoidMethodA, CallNonvirtualObjectMethod,
CallNonvirtualObjectMethodV,
CallNonvirtualObjectMethodA,
CallNonvirtualBooleanMethod,
CallNonvirtualBooleanMethodV,
CallNonvirtualBooleanMethodA,
CallNonvirtualByteMethod,
CallNonvirtualByteMethodV,
CallNonvirtualByteMethodA,
CallNonvirtualCharMethod,
CallNonvirtualCharMethodV,
CallNonvirtualCharMethodA,
CallNonvirtualShortMethod,
CallNonvirtualShortMethodV,
CallNonvirtualShortMethodA,
CallNonvirtualIntMethod,
CallNonvirtualIntMethodV,
CallNonvirtualIntMethodA,
CallNonvirtualLongMethod,
CallNonvirtualLongMethodV,
CallNonvirtualLongMethodA,
CallNonvirtualFloatMethod,
CallNonvirtualFloatMethodV,
CallNonvirtualFloatMethodA,
CallNonvirtualDoubleMethod,
CallNonvirtualDoubleMethodV,
CallNonvirtualDoubleMethodA,
CallNonvirtualVoidMethod,
CallNonvirtualVoidMethodV,
CallNonvirtualVoidMethodA, GetFieldID, GetObjectField,
GetBooleanField,
GetByteField,
GetCharField,
GetShortField,
GetIntField,
GetLongField,
GetFloatField,
GetDoubleField,
SetObjectField,
SetBooleanField,
SetByteField,
SetCharField,
SetShortField,
SetIntField,
SetLongField,
SetFloatField,
SetDoubleField, GetStaticMethodID, CallStaticObjectMethod,
CallStaticObjectMethodV,
CallStaticObjectMethodA,
CallStaticBooleanMethod,
CallStaticBooleanMethodV,
CallStaticBooleanMethodA,
CallStaticByteMethod,
CallStaticByteMethodV,
CallStaticByteMethodA,
CallStaticCharMethod,
CallStaticCharMethodV,
CallStaticCharMethodA,
CallStaticShortMethod,
CallStaticShortMethodV,
CallStaticShortMethodA,
CallStaticIntMethod,
CallStaticIntMethodV,
CallStaticIntMethodA,
CallStaticLongMethod,
CallStaticLongMethodV,
CallStaticLongMethodA,
CallStaticFloatMethod,
CallStaticFloatMethodV,
CallStaticFloatMethodA,
CallStaticDoubleMethod,
CallStaticDoubleMethodV,
CallStaticDoubleMethodA,
CallStaticVoidMethod,
CallStaticVoidMethodV,
CallStaticVoidMethodA, GetStaticFieldID, GetStaticObjectField,
GetStaticBooleanField,
GetStaticByteField,
GetStaticCharField,
GetStaticShortField,
GetStaticIntField,
GetStaticLongField,
GetStaticFloatField,
GetStaticDoubleField, SetStaticObjectField,
SetStaticBooleanField,
SetStaticByteField,
SetStaticCharField,
SetStaticShortField,
SetStaticIntField,
SetStaticLongField,
SetStaticFloatField,
SetStaticDoubleField, NewString, GetStringLength,
GetStringChars,
ReleaseStringChars, NewStringUTF,
GetStringUTFLength,
GetStringUTFChars,
ReleaseStringUTFChars, GetArrayLength, NewObjectArray,
GetObjectArrayElement,
SetObjectArrayElement, NewBooleanArray,
NewByteArray,
NewCharArray,
NewShortArray,
NewIntArray,
NewLongArray,
NewFloatArray,
NewDoubleArray, GetBooleanArrayElements,
GetByteArrayElements,
GetCharArrayElements,
GetShortArrayElements,
GetIntArrayElements,
GetLongArrayElements,
GetFloatArrayElements,
GetDoubleArrayElements, ReleaseBooleanArrayElements,
ReleaseByteArrayElements,
ReleaseCharArrayElements,
ReleaseShortArrayElements,
ReleaseIntArrayElements,
ReleaseLongArrayElements,
ReleaseFloatArrayElements,
ReleaseDoubleArrayElements, GetBooleanArrayRegion,
GetByteArrayRegion,
GetCharArrayRegion,
GetShortArrayRegion,
GetIntArrayRegion,
GetLongArrayRegion,
GetFloatArrayRegion,
GetDoubleArrayRegion,
SetBooleanArrayRegion,
SetByteArrayRegion,
SetCharArrayRegion,
SetShortArrayRegion,
SetIntArrayRegion,
SetLongArrayRegion,
SetFloatArrayRegion,
SetDoubleArrayRegion, RegisterNatives,
UnregisterNatives, MonitorEnter,
MonitorExit, GetJavaVM, GetStringRegion,
GetStringUTFRegion, GetPrimitiveArrayCritical,
ReleasePrimitiveArrayCritical, GetStringCritical,
ReleaseStringCritical, NewWeakGlobalRef,
DeleteWeakGlobalRef, ExceptionCheck, NewDirectByteBuffer,
GetDirectBufferAddress,
GetDirectBufferCapacity, GetObjectRefType
};
4.常用函数表
对象(全局和局部)的生成,删除. | |||||||||
jobject NewGlobalRef(JNIEnv *env, jobject obj); |
Creates a new global reference to the object referred to by the The of by calling |
||||||||
void DeleteGlobalRef(JNIEnv *env, jobject globalRef); | Deletes the global reference pointed to by globalRef . |
||||||||
jobject NewLocalRef(JNIEnv *env, jobject ref); |
Creates a new local reference that refers to the same object as local reference. Returns |
||||||||
void DeleteLocalRef(JNIEnv *env, jobject localRef); | Deletes the local reference pointed to by localRef . |
||||||||
类的定义,声明,查找等 | |||||||||
jclass FindClass(JNIEnv *env, const char *name); |
The result of for applications,and is able to locate classes listed in the The For example, the fully-qualified class name for the "java/lang/String" 如 com.txt.Stu 则定成com/txt/Stu ,把包名中的.改成/ The array type signature of the array class "[Ljava/lang/Object;" |
||||||||
jclass GetSuperclass(JNIEnv *env, jclass clazz); |
If the superclass of the class specified by this function returns |
||||||||
jclass DefineClass(JNIEnv *env, const char *name, jobject loader,const jbyte *buf, jsize bufLen); |
Loads a class from a buffer of raw class data. The buffer containing the raw class data is not referenced by the VM after the DefineClass call returns, and it may be discarded if desired. |
||||||||
jobject AllocObject(JNIEnv *env, jclass clazz); |
Allocates a new Java object without invoking any of the constructors for the object. Returns a reference to the object. The clazz argument must not refer to an array class. |
||||||||
|
Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling ( The |
||||||||
数组相关 | |||||||||
ArrayType NewXXXArray |
申请数组 A family of operations used to construct a new primitive array object. Table 4-8 describes the specific primitive array constructors.You should replace NewXXXArray with one of the actual primitive array constructor routine names from the following table, and replace ArrayType with the corresponding array type for that routine. |
||||||||
NativeType
返回数组所有元素 |
A family of functions that returns the body of the primitive array. The result is valid until the corresponding Release Java array, changes made to the returned array will not necessarily be reflected in the original
If or it is set to |
||||||||
void ReleaseXXXArrayElements(JNIEnv *env, ArrayType array, NativeType *elems, jint mode);
|
A family of functions that informs the VM that the native code no longer needs access to is a pointer derived from copies back all changes made to buffer should be released. following impact, as shown in the following table: Table 4-10 Primitive Array Release Modes
In most cases, programmers pass “0” to the The other options give the programmer more control over memory management and should be used with extreme care. |
||||||||
jsize GetArrayLength(JNIEnv *env, jarray array); | Returns the number of elements in the array. | ||||||||
const NativeType |
A family of functions that copies back a region of a primitive array from a buffer.The following table describes the specific primitive array element accessors. You should make the following replacements: |
||||||||
void GetXXXArrayRegion
|
A family of functions that copies a region of a primitive array into a buffer. | ||||||||
获取对象属性,成员系列 | |||||||||
|
Returns the class of an object. | ||||||||
GetMethodID与GetStaticMethodID系列 jmethodID GetMethodID(JNIEnv *env, const char *name, const char *sig); |
Returns the method ID for an instance (nonstatic) method of a class or interface. The method may be defined in one of the
To obtain the method ID of a constructor, supply |
||||||||
GetFieldID与GetFieldID系列 |
Returns the field ID for an instance (nonstatic) field of a class. The field is specified by its name and signature. The Get<type>Field and Set<type>Field families of accessor functions use field IDs to retrieve object fields.
|
||||||||
调用系列 | |||||||||
NativeType CallXXXMethod
|
使用obj,class,等调用值为methoID的函数.注意要用 jobject,不是jclass |
||||||||
NativeType CallStaticXXXMethod
|
This family of operations invokes a static method on a Java object, according to the specified method ID. The The method ID must be derived from |
||||||||
字符串相关 | |||||||||
jstring NewStringUTF(JNIEnv *env, const char *bytes); |
Constructs a new |
||||||||
const char * GetStringUTFChars(JNIEnv *env, jstring string,jboolean *isCopy); |
Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by If |
||||||||
环境相关 | |||||||||
jint GetJavaVM(JNIEnv *env, JavaVM **vm); |
Returns the Java VM interface (used in the Invocation API) associated with the current thread. The result is placed at the location pointed to by the second argument, |
||||||||
jboolean ExceptionCheck(JNIEnv *env); |
Returns |
||||||||
等等 | ... |