staticconstchar* jni_error_code(int ret) { switch(ret) { case JNI_OK: return"JNI_OK"; case JNI_ERR: return"JNI_ERR"; case JNI_EDETACHED: return"JNI_EDETACHED"; case JNI_EVERSION: return"JNI_EVERSION"; case JNI_ENOMEM: return"JNI_ENOMEM"; case JNI_EEXIST: return"JNI_EEXIST"; case JNI_EINVAL: return"JNI_EINVAL"; default: return"Invalid JNI error code";
}
}
staticvoid report(constchar* func, int ret_actual, int ret_expected) { constchar* ret = jni_error_code(ret_actual); if (ret_actual == ret_expected) {
printf("%s returned %s as expected\n", func, ret);
} else {
printf("Unexpected JNI return code %s from %s\n", ret, func);
}
}
staticint using_system_exit = 0; // Not System.exit by default
// We've saved the JavaVM from OnLoad time so we first try to // get a JNIEnv for the current thread.
JNIEnv *env;
jint res = (*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2);
report("GetEnv", res, JNI_EDETACHED); if (res == JNI_EDETACHED) {
// Test all of the Invocation API functions
res = (*jvm)->AttachCurrentThreadAsDaemon(jvm, (void **)&env, NULL);
report("AttachCurrentThreadAsDaemon", res, JNI_ERR);
res = (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
report("AttachCurrentThread", res, JNI_ERR);
res = (*jvm)->DetachCurrentThread(jvm);
report("DetachCurrentThread", res, JNI_ERR);
JavaVM* jvm_p[1]; int nVMs;
res = JNI_GetCreatedJavaVMs(jvm_p, 1, &nVMs);
report("JNI_GetCreatedJavaVMs", res, JNI_OK); // Whether nVMs is 0 or 1 depends on the termination path if (nVMs == 0 && !using_system_exit) {
printf("Found 0 created VMs as expected\n");
} elseif (nVMs == 1 && using_system_exit) {
printf("Found 1 created VM as expected\n");
} else {
printf("Unexpected number of created VMs: %d\n", nVMs);
}
res = (*jvm)->DestroyJavaVM(jvm);
report("DestroyJavaVM", res, JNI_ERR);
// Failure mode depends on the termination path
res = JNI_CreateJavaVM(jvm_p, (void**)&env, &args);
report("JNI_CreateJavaVM", res, using_system_exit ? JNI_EEXIST : JNI_ERR);
} // else test has already failed
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.