Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
Make JClass more useful and use it in ntcore JNI.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Dec 15, 2016
1 parent 378a145 commit ce76115
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 47 deletions.
68 changes: 24 additions & 44 deletions java/lib/NetworkTablesJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
8 changes: 5 additions & 3 deletions wpiutil/include/support/jni_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ namespace java {
// to potential shutdown issues with doing so.
class JClass {
public:
JClass(JNIEnv* env, const char* name) : m_cls(nullptr) {
JClass() = default;

JClass(JNIEnv* env, const char* name) {
jclass local = env->FindClass(name);
if (!local) return;
m_cls = static_cast<jclass>(env->NewGlobalRef(local));
env->DeleteLocalRef(local);
}

void free(JNIEnv *env) {
env->DeleteGlobalRef(m_cls);
if (m_cls) env->DeleteGlobalRef(m_cls);
m_cls = nullptr;
}

Expand All @@ -49,7 +51,7 @@ class JClass {
operator jclass() const { return m_cls; }

private:
jclass m_cls;
jclass m_cls = nullptr;
};

// Container class for cleaning up Java local references.
Expand Down

0 comments on commit ce76115

Please sign in to comment.