Make JClass more useful and use it in ntcore JNI.

This commit is contained in:
Peter Johnson
2016-12-14 23:16:09 -08:00
parent 378a145cf7
commit ce7611562f
2 changed files with 29 additions and 47 deletions

View File

@@ -24,13 +24,13 @@ using namespace wpi::java;
// Used for callback.
static JavaVM *jvm = nullptr;
static jclass booleanCls = nullptr;
static jclass doubleCls = nullptr;
static jclass connectionInfoCls = nullptr;
static jclass entryInfoCls = nullptr;
static jclass keyNotDefinedEx = nullptr;
static jclass persistentEx = nullptr;
static jclass illegalArgEx = nullptr;
static JClass booleanCls;
static JClass doubleCls;
static JClass connectionInfoCls;
static JClass entryInfoCls;
static JClass keyNotDefinedEx;
static JClass persistentEx;
static JClass illegalArgEx;
// Thread-attached environment for listener callbacks.
static JNIEnv *listenerEnv = nullptr;
@@ -64,49 +64,29 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
// Cache references to classes
jclass local;
local = env->FindClass("java/lang/Boolean");
if (!local) return JNI_ERR;
booleanCls = static_cast<jclass>(env->NewGlobalRef(local));
booleanCls = JClass(env, "java/lang/Boolean");
if (!booleanCls) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("java/lang/Double");
if (!local) return JNI_ERR;
doubleCls = static_cast<jclass>(env->NewGlobalRef(local));
doubleCls = JClass(env, "java/lang/Double");
if (!doubleCls) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("edu/wpi/first/wpilibj/networktables/ConnectionInfo");
if (!local) return JNI_ERR;
connectionInfoCls = static_cast<jclass>(env->NewGlobalRef(local));
connectionInfoCls =
JClass(env, "edu/wpi/first/wpilibj/networktables/ConnectionInfo");
if (!connectionInfoCls) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("edu/wpi/first/wpilibj/networktables/EntryInfo");
if (!local) return JNI_ERR;
entryInfoCls = static_cast<jclass>(env->NewGlobalRef(local));
entryInfoCls = JClass(env, "edu/wpi/first/wpilibj/networktables/EntryInfo");
if (!entryInfoCls) return JNI_ERR;
env->DeleteLocalRef(local);
local =
env->FindClass("edu/wpi/first/wpilibj/networktables/NetworkTableKeyNotDefined");
keyNotDefinedEx = static_cast<jclass>(env->NewGlobalRef(local));
keyNotDefinedEx = JClass(
env, "edu/wpi/first/wpilibj/networktables/NetworkTableKeyNotDefined");
if (!keyNotDefinedEx) return JNI_ERR;
env->DeleteLocalRef(local);
local =
env->FindClass("edu/wpi/first/wpilibj/networktables/PersistentException");
persistentEx = static_cast<jclass>(env->NewGlobalRef(local));
persistentEx =
JClass(env, "edu/wpi/first/wpilibj/networktables/PersistentException");
if (!persistentEx) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("java/lang/IllegalArgumentException");
if (!local) return JNI_ERR;
illegalArgEx = static_cast<jclass>(env->NewGlobalRef(local));
illegalArgEx = JClass(env, "java/lang/IllegalArgumentException");
if (!illegalArgEx) return JNI_ERR;
env->DeleteLocalRef(local);
// Initial configuration of listener start/exit
nt::SetListenerOnStart(ListenerOnStart);
@@ -120,13 +100,13 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK)
return;
// Delete global references
if (booleanCls) env->DeleteGlobalRef(booleanCls);
if (doubleCls) env->DeleteGlobalRef(doubleCls);
if (connectionInfoCls) env->DeleteGlobalRef(connectionInfoCls);
if (entryInfoCls) env->DeleteGlobalRef(entryInfoCls);
if (keyNotDefinedEx) env->DeleteGlobalRef(keyNotDefinedEx);
if (persistentEx) env->DeleteGlobalRef(persistentEx);
if (illegalArgEx) env->DeleteGlobalRef(illegalArgEx);
booleanCls.free(env);
doubleCls.free(env);
connectionInfoCls.free(env);
entryInfoCls.free(env);
keyNotDefinedEx.free(env);
persistentEx.free(env);
illegalArgEx.free(env);
jvm = nullptr;
}