JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_giveArray
(JNIEnv *env, jobject jobj, jintArray arr) {
int compare(jint *a, jint *b);
jint *elemts = (*env)->GetIntArrayElements(env, arr, NULL);
if (elemts == NULL) {
return;
}
int len = (*env)->GetArrayLength(env, arr);
qsort(elemts, len, sizeof(jint), compare);
(*env)->ReleaseIntArrayElements(env, arr, elemts, JNI_COMMIT);
}
int compare(jint *a, jint *b) {
return *a - *b;
}
JNIEXPORT jobjectArray JNICALL Java_com_mei_test_jni_JniTest_initStringArray
(JNIEnv *env, jobject jobj, jint size ) {
jobjectArray result;
jclass jclz;
jint i;
jclz = (*env)->FindClass(env, "java/lang/String");
if (jclz == NULL) {
return NULL;
}
result = (*env)->NewObjectArray(env, size, jclz, jobj);
if (result == NULL) {
return NULL;
}
for ( i = 0; i < size; i++)
{
char * c_str = (char*)malloc(256);
memset(c_str, 0, 256);
sprintf(c_str, "hello num:%d\n", i);
jstring str = (*env)->NewStringUTF(env, c_str);
if (str == NULL) {
return NULL;
}
(*env)->SetObjectArrayElement(env, result, i, str);
free(c_str);
c_str = NULL;
}
return result;
}
JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_localRef
(JNIEnv *env, jobject jobj) {
int i = 0;
for ( i = 0; i < 5; i++)
{
jclass cls = (*env)->FindClass(env, "java/util/Date");//这个也是一个局部引用
jmethodID jmid = (*env)->GetMethodID(env, cls, "<init>", "()V");
jobject obj = (*env)->NewObject(env, cls, jmid);
(*env)->DeleteLocalRef(env, cls);
(*env)->DeleteLocalRef(env, obj);
}
}
jstring global_str;
JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_createGlobalRef
(JNIEnv *env, jobject jobj) {
jobject obj = (*env)->NewStringUTF(env, "JNI is intersting");
global_str = (*env)->NewGlobalRef(env, obj);
}
JNIEXPORT jstring JNICALL Java_com_mei_test_jni_JniTest_getGlobalRef
(JNIEnv *env, jobject jobj) {
return global_str;
}
JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_deleteGlobalRef
(JNIEnv *env, jobject jobj) {
(*env)->DeleteGlobalRef(env, global_str);
}
jclass g_weak_cls;
JNIEXPORT jstring JNICALL Java_com_mei_test_jni_JniTest_createWeakRef
(JNIEnv *env, jobject jobj){
jclass cls_string = (*env)->FindClass(env, "java/lang/String");
g_weak_cls = (*env)->NewWeakGlobalRef(env, cls_string);
return g_weak_cls;
}
JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_exception
(JNIEnv *env, jobject jobj) {
jclass cls = (*env)->GetObjectClass(env, jobj);
jfieldID fid = (*env)->GetFieldID(env, cls, "key", "Ljava/lang/String;");
jthrowable exception = (*env)->ExceptionOccurred(env);
if (exception !=NULL)
{
jclass newExc;
(*env)->ExceptionClear(env);
newExc = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
if (newExc ==NULL)
{
printf("exception\n");
return;
}
(*env)->ThrowNew(env, newExc, "Throw exception from JNI: GetFieldID faild ");
}
printf("exception\n");
}
JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_cached
(JNIEnv *env, jobject jobj) {
jclass cls = (*env)->GetObjectClass(env, jobj);
static jfieldID fid;
if (fid == NULL) {
fid = (*env)->GetFieldID(env, cls, "mKey", "Ljava/lang/String;");
printf("GetFieldID\n");
}
}
static jfieldID fid_glb;
JNIEXPORT void JNICALL Java_com_mei_test_jni_JniTest_cachedGlobal
(JNIEnv *env, jobject jobj){
jclass jclz = (*env)->GetObjectClass(env, jobj);
if (fid_glb == NULL) {
fid_glb = (*env)->GetFieldID(env, jclz, "mKey", "Ljava/lang/String;");
printf("fid_glb init\n");
}
}
JNIEXPORT jstring JNICALL Java_com_mei_test_jni_JniTest_accessCacheNewString
(JNIEnv *env, jobject jobj) {
static jclass cls_string = NULL;//当内存不足的时候,在方法调用结束之后,JVM就会释放所有的局部变量,
if (cls_string == NULL)
{
printf("alvin in Java_JniMain_AcessCache_newString out: \n");
cls_string = (*env)->FindClass(env, "com/mei/test/jni/Refrence");
}
jmethodID jmid = (*env)->GetMethodID(env, cls_string, "getRef", "(I)I");
jthrowable ex = (*env)->ExceptionOccurred(env);
if (ex != NULL)
{
jclass newExc;
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
printf("C exceptions happend\n");
}
printf("alvin out Java_JniMain_AcessCache_newString\n");
return NULL;
}
JNIEXPORT jstring JNICALL Java_com_mei_test_jni_JniTest_accessCF
(JNIEnv *env, jobject jobj) {
return Java_com_mei_test_jni_JniTest_accessCacheNewString(env, jobj);
}