NDK(12)Jni常用函数

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.函数列表

  1 typedef const struct JNINativeInterface *JNIEnv; 
  2 const struct JNINativeInterface ... = {
  3 
  4     NULL,
  5     NULL,
  6     NULL,
  7     NULL,
  8     GetVersion,
  9 
 10     DefineClass,
 11     FindClass,
 12 
 13     FromReflectedMethod,
 14     FromReflectedField,
 15     ToReflectedMethod,
 16 
 17     GetSuperclass,
 18     IsAssignableFrom,
 19 
 20     ToReflectedField,
 21 
 22     Throw,
 23     ThrowNew,
 24     ExceptionOccurred,
 25     ExceptionDescribe,
 26     ExceptionClear,
 27     FatalError,
 28 
 29     PushLocalFrame,
 30     PopLocalFrame,
 31 
 32     NewGlobalRef,
 33     DeleteGlobalRef,
 34     DeleteLocalRef,
 35     IsSameObject,
 36     NewLocalRef,
 37     EnsureLocalCapacity,
 38 
 39     AllocObject,
 40     NewObject,
 41     NewObjectV,
 42     NewObjectA,
 43 
 44     GetObjectClass,
 45     IsInstanceOf,
 46 
 47     GetMethodID,
 48 
 49     CallObjectMethod,
 50     CallObjectMethodV,
 51     CallObjectMethodA,
 52     CallBooleanMethod,
 53     CallBooleanMethodV,
 54     CallBooleanMethodA,
 55     CallByteMethod,
 56     CallByteMethodV,
 57     CallByteMethodA,
 58     CallCharMethod,
 59     CallCharMethodV,
 60     CallCharMethodA,
 61     CallShortMethod,
 62     CallShortMethodV,
 63     CallShortMethodA,
 64     CallIntMethod,
 65     CallIntMethodV,
 66     CallIntMethodA,
 67     CallLongMethod,
 68     CallLongMethodV,
 69     CallLongMethodA,
 70     CallFloatMethod,
 71     CallFloatMethodV,
 72     CallFloatMethodA,
 73     CallDoubleMethod,
 74     CallDoubleMethodV,
 75     CallDoubleMethodA,
 76     CallVoidMethod,
 77     CallVoidMethodV,
 78     CallVoidMethodA,
 79 
 80     CallNonvirtualObjectMethod,
 81     CallNonvirtualObjectMethodV,
 82     CallNonvirtualObjectMethodA,
 83     CallNonvirtualBooleanMethod,
 84     CallNonvirtualBooleanMethodV,
 85     CallNonvirtualBooleanMethodA,
 86     CallNonvirtualByteMethod,
 87     CallNonvirtualByteMethodV,
 88     CallNonvirtualByteMethodA,
 89     CallNonvirtualCharMethod,
 90     CallNonvirtualCharMethodV,
 91     CallNonvirtualCharMethodA,
 92     CallNonvirtualShortMethod,
 93     CallNonvirtualShortMethodV,
 94     CallNonvirtualShortMethodA,
 95     CallNonvirtualIntMethod,
 96     CallNonvirtualIntMethodV,
 97     CallNonvirtualIntMethodA,
 98     CallNonvirtualLongMethod,
 99     CallNonvirtualLongMethodV,
100     CallNonvirtualLongMethodA,
101     CallNonvirtualFloatMethod,
102     CallNonvirtualFloatMethodV,
103     CallNonvirtualFloatMethodA,
104     CallNonvirtualDoubleMethod,
105     CallNonvirtualDoubleMethodV,
106     CallNonvirtualDoubleMethodA,
107     CallNonvirtualVoidMethod,
108     CallNonvirtualVoidMethodV,
109     CallNonvirtualVoidMethodA,
110 
111     GetFieldID,
112 
113     GetObjectField,
114     GetBooleanField,
115     GetByteField,
116     GetCharField,
117     GetShortField,
118     GetIntField,
119     GetLongField,
120     GetFloatField,
121     GetDoubleField,
122     SetObjectField,
123     SetBooleanField,
124     SetByteField,
125     SetCharField,
126     SetShortField,
127     SetIntField,
128     SetLongField,
129     SetFloatField,
130     SetDoubleField,
131 
132     GetStaticMethodID,
133 
134     CallStaticObjectMethod,
135     CallStaticObjectMethodV,
136     CallStaticObjectMethodA,
137     CallStaticBooleanMethod,
138     CallStaticBooleanMethodV,
139     CallStaticBooleanMethodA,
140     CallStaticByteMethod,
141     CallStaticByteMethodV,
142     CallStaticByteMethodA,
143     CallStaticCharMethod,
144     CallStaticCharMethodV,
145     CallStaticCharMethodA,
146     CallStaticShortMethod,
147     CallStaticShortMethodV,
148     CallStaticShortMethodA,
149     CallStaticIntMethod,
150     CallStaticIntMethodV,
151     CallStaticIntMethodA,
152     CallStaticLongMethod,
153     CallStaticLongMethodV,
154     CallStaticLongMethodA,
155     CallStaticFloatMethod,
156     CallStaticFloatMethodV,
157     CallStaticFloatMethodA,
158     CallStaticDoubleMethod,
159     CallStaticDoubleMethodV,
160     CallStaticDoubleMethodA,
161     CallStaticVoidMethod,
162     CallStaticVoidMethodV,
163     CallStaticVoidMethodA,
164 
165     GetStaticFieldID,
166 
167     GetStaticObjectField,
168     GetStaticBooleanField,
169     GetStaticByteField,
170     GetStaticCharField,
171     GetStaticShortField,
172     GetStaticIntField,
173     GetStaticLongField,
174     GetStaticFloatField,
175     GetStaticDoubleField,
176 
177     SetStaticObjectField,
178     SetStaticBooleanField,
179     SetStaticByteField,
180     SetStaticCharField,
181     SetStaticShortField,
182     SetStaticIntField,
183     SetStaticLongField,
184     SetStaticFloatField,
185     SetStaticDoubleField,
186 
187     NewString,
188 
189     GetStringLength,
190     GetStringChars,
191     ReleaseStringChars,
192 
193     NewStringUTF,
194     GetStringUTFLength,
195     GetStringUTFChars,
196     ReleaseStringUTFChars,
197 
198     GetArrayLength,
199 
200     NewObjectArray,
201     GetObjectArrayElement,
202     SetObjectArrayElement,
203 
204     NewBooleanArray,
205     NewByteArray,
206     NewCharArray,
207     NewShortArray,
208     NewIntArray,
209     NewLongArray,
210     NewFloatArray,
211     NewDoubleArray,
212 
213     GetBooleanArrayElements,
214     GetByteArrayElements,
215     GetCharArrayElements,
216     GetShortArrayElements,
217     GetIntArrayElements,
218     GetLongArrayElements,
219     GetFloatArrayElements,
220     GetDoubleArrayElements,
221 
222     ReleaseBooleanArrayElements,
223     ReleaseByteArrayElements,
224     ReleaseCharArrayElements,
225     ReleaseShortArrayElements,
226     ReleaseIntArrayElements,
227     ReleaseLongArrayElements,
228     ReleaseFloatArrayElements,
229     ReleaseDoubleArrayElements,
230 
231     GetBooleanArrayRegion,
232     GetByteArrayRegion,
233     GetCharArrayRegion,
234     GetShortArrayRegion,
235     GetIntArrayRegion,
236     GetLongArrayRegion,
237     GetFloatArrayRegion,
238     GetDoubleArrayRegion,
239     SetBooleanArrayRegion,
240     SetByteArrayRegion,
241     SetCharArrayRegion,
242     SetShortArrayRegion,
243     SetIntArrayRegion,
244     SetLongArrayRegion,
245     SetFloatArrayRegion,
246     SetDoubleArrayRegion,
247 
248     RegisterNatives,
249     UnregisterNatives,
250 
251     MonitorEnter,
252     MonitorExit,
253 
254     GetJavaVM,
255 
256     GetStringRegion,
257     GetStringUTFRegion,
258 
259     GetPrimitiveArrayCritical,
260     ReleasePrimitiveArrayCritical,
261 
262     GetStringCritical,
263     ReleaseStringCritical,
264 
265     NewWeakGlobalRef,
266     DeleteWeakGlobalRef,
267 
268     ExceptionCheck,
269 
270     NewDirectByteBuffer,
271     GetDirectBufferAddress,
272     GetDirectBufferCapacity,
273 
274     GetObjectRefType
275   };
View Code

 

4.常用函数表 

对象(全局和局部)的生成,删除.
jobject NewGlobalRef(JNIEnv *env, jobject obj);

Creates a new global reference to the object referred to by the obj argument.

The obj argument may be a global or local reference. Global references must be explicitly disposed

of by calling DeleteGlobalRef().

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 ref. The given ref may be a global or

local reference. Returns NULL if ref refers to null.

void DeleteLocalRef(JNIEnv *env, jobject localRef); Deletes the local reference pointed to by localRef.
类的定义,声明,查找等
jclass FindClass(JNIEnv *env, const char *name);

The result of ClassLoader.getSystemClassLoader is used. This is the class loader the virtual machine creates

for applications,and is able to locate classes listed in the java.class.path property.

The name argument is a fully-qualified class name or an array type signature .

For example, the fully-qualified class name for the java.lang.String class is:

           "java/lang/String"  

如 com.txt.Stu 则定成com/txt/Stu ,把包名中的.改成/

The array type signature of the array class java.lang.Object[] is:                   

        "[Ljava/lang/Object;"

jclass GetSuperclass(JNIEnv *env, jclass clazz);

 

If clazz represents any class other than the class Object, then this function returns the object that represents

the superclass of the class specified by clazz.If clazz specifies the class Object, or clazz represents an interface,

this function returns NULL.

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.

jobject NewObject(JNIEnv *env,

jclass clazz,jmethodID methodID, ...);

有3个重载

Constructs a new Java object. The method ID indicates which constructor method to invoke.

This ID must be obtained by calling GetMethodID() with <init> as the method name and void 

(V) as the return type.

The clazz argument must not refer to an array class.

数组相关   

ArrayType NewXXXArray(JNIEnv *env,jsize length);

 

申请数组

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 *GetXXXArrayElements(JNIEnv *env,

ArrayType array, jboolean *isCopy);

返回数组所有元素

A family of functions that returns the body of the primitive array. The result is valid until the corresponding 

ReleaseXXXArrayElements() function is called. Since the returned array may be a copy of the

Java array, changes made to the returned array will not necessarily be reflected in the original array until 

ReleaseXXXArrayElements() is called.

If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made;

or it is set to JNI_FALSE if no copy is made.

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 elems. The elems argument

is a pointer derived from array using the corresponding GetXXXArrayElements() function. If necessary, this function

copies back all changes made to elems to the original array.The mode argument provides information on how the array

buffer should be released. mode has no effect if elems is not a copy of the elements in array. Otherwise, mode has the

following impact, as shown in the following table: Table 4-10 Primitive Array Release Modes



mode

actions
0
copy back the content and free the elems buffer
当isCopy是true时,用这个,说明jvm的是返回副本,
把副本的内容更新的源数组,并把副本释放.
JNI_COMMIT
copy back the content but do not free the elems buffer
当isCopy是false时,用这个,说明jvm返回的是引用,
这时不要释放数组.
JNI_ABORT
free the buffer without copying back the possible changes
这是只释放掉缓冲,不更新内容

 

In most cases, programmers pass “0” to the mode argument to ensure consistent behavior for both pinned and copied arrays.

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.

void SetXXXArrayRegion(JNIEnv *env, ArrayType array,

jsize start, jsize len, 

const NativeType *buf);

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(JNIEnv *env, ArrayType array,

jsize start, jsize len, NativeType *buf);

 A family of functions that copies a region of a primitive array into a buffer.
获取对象属性,成员系列

jclass GetObjectClass(JNIEnv *env, jobject obj);

Returns the class of an object. 

GetMethodID与GetStaticMethodID系列

jmethodID GetMethodID(JNIEnv *env,  jclass clazz,

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 clazz’s superclasses and inherited by clazz. The method is determined by its name and signature.

GetMethodID() causes an uninitialized class to be initialized.

To obtain the method ID of a constructor, supply <init> as the method name and void (V) as the return type.

GetFieldID与GetFieldID系列
jfieldID GetFieldID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);

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.

GetFieldID() causes an uninitialized class to be initialized.

GetFieldID() cannot be used to obtain the length field of an array. Use GetArrayLength() instead.

 

调用系列

NativeType CallXXXMethod(JNIEnv *env,

jobject obj,jmethodID methodID, ...);

 

使用obj,class,等调用值为methoID的函数.注意要用 jobject,不是jclass

NativeType CallStaticXXXMethod(JNIEnv *env,

jclass clazz,jmethodID methodID, ...); 

 

This family of operations invokes a static method on a Java object, according to the specified method ID.

The methodID argument must be obtained by calling GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.

字符串相关

 

jstring NewStringUTF(JNIEnv *env, const char *bytes);

Constructs a new java.lang.String object from an array of characters in modified UTF-8 encoding. 

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 ReleaseStringUTFChars().

If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.

环境相关

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, vm.

jboolean ExceptionCheck(JNIEnv *env);

Returns JNI_TRUE when there is a pending exception; otherwise, returns JNI_FALSE.

等等 ...
posted @ 2015-08-19 17:47  f9q  阅读(499)  评论(0编辑  收藏  举报