*
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "JniContext.h"
|
||||
#include <optional>
|
||||
|
||||
class JBindings {
|
||||
protected:
|
||||
JBindings(const char* className) {
|
||||
JNIEnv* env = JniContext::getJniEnv();
|
||||
_class = findClass(env, className);
|
||||
}
|
||||
|
||||
std::optional<jfieldID> getStaticFieldID(const char* name, const char* signature) {
|
||||
try{
|
||||
auto result = JniContext::getJniEnv()->GetStaticFieldID(_class, name, signature);
|
||||
JniContext::checkException();
|
||||
return result;
|
||||
}catch(...){
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
//
|
||||
|
||||
static jclass findClass(JNIEnv* env, const char* className) {
|
||||
auto classLocalRef = env->FindClass(className);
|
||||
if(classLocalRef == nullptr){
|
||||
return nullptr;
|
||||
}
|
||||
auto globalRef = (jclass)env->NewGlobalRef(classLocalRef);
|
||||
env->DeleteLocalRef(classLocalRef);
|
||||
return globalRef;
|
||||
}
|
||||
|
||||
jclass _class;
|
||||
};
|
||||
Reference in New Issue
Block a user