| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef CORE_JNI_HELPERS |
| 18 |
#define CORE_JNI_HELPERS |
| 19 |
|
| 20 |
#include <nativehelper/JNIPlatformHelp.h> |
| 21 |
#include <nativehelper/scoped_local_ref.h> |
| 22 |
#include <nativehelper/scoped_utf_chars.h> |
| 23 |
#include <android_runtime/AndroidRuntime.h> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifdef __ANDROID__ |
| 30 |
#define CRITICAL_JNI_PARAMS |
| 31 |
#define CRITICAL_JNI_PARAMS_COMMA |
| 32 |
#else |
| 33 |
#define CRITICAL_JNI_PARAMS JNIEnv*, jclass |
| 34 |
#define CRITICAL_JNI_PARAMS_COMMA JNIEnv*, jclass, |
| 35 |
#endif |
| 36 |
|
| 37 |
namespace android { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) { |
| 42 |
jclass clazz = env->FindClass(class_name); |
| 43 |
LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name); |
| 44 |
return clazz; |
| 45 |
} |
| 46 |
|
| 47 |
static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name, |
| 48 |
const char* field_signature) { |
| 49 |
jfieldID res = env->GetFieldID(clazz, field_name, field_signature); |
| 50 |
LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find field %s with signature %s", field_name, |
| 51 |
field_signature); |
| 52 |
return res; |
| 53 |
} |
| 54 |
|
| 55 |
static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name, |
| 56 |
const char* method_signature) { |
| 57 |
jmethodID res = env->GetMethodID(clazz, method_name, method_signature); |
| 58 |
LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s with signature %s", method_name, |
| 59 |
method_signature); |
| 60 |
return res; |
| 61 |
} |
| 62 |
|
| 63 |
static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name, |
| 64 |
const char* field_signature) { |
| 65 |
jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature); |
| 66 |
LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s with signature %s", field_name, |
| 67 |
field_signature); |
| 68 |
return res; |
| 69 |
} |
| 70 |
|
| 71 |
static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name, |
| 72 |
const char* method_signature) { |
| 73 |
jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature); |
| 74 |
LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s with signature %s", |
| 75 |
method_name, method_signature); |
| 76 |
return res; |
| 77 |
} |
| 78 |
|
| 79 |
template <typename T> |
| 80 |
static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) { |
| 81 |
jobject res = env->NewGlobalRef(in); |
| 82 |
LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference."); |
| 83 |
return static_cast<T>(res); |
| 84 |
} |
| 85 |
|
| 86 |
static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className, |
| 87 |
const JNINativeMethod* gMethods, int numMethods) { |
| 88 |
int res = AndroidRuntime::registerNativeMethods(env, className, gMethods, numMethods); |
| 89 |
LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods."); |
| 90 |
return res; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
jobject GetReferent(JNIEnv* env, jobject ref); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId, |
| 103 |
const char* defaultValue) { |
| 104 |
ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId))); |
| 105 |
if (strObj != nullptr) { |
| 106 |
ScopedUtfChars chars(env, strObj.get()); |
| 107 |
return std::string(chars.c_str()); |
| 108 |
} |
| 109 |
return std::string(defaultValue); |
| 110 |
} |
| 111 |
|
| 112 |
static inline JNIEnv* GetJNIEnvironment(JavaVM* vm, jint version = JNI_VERSION_1_4) { |
| 113 |
JNIEnv* env; |
| 114 |
if (vm->GetEnv(reinterpret_cast<void**>(&env), version) != JNI_OK) { |
| 115 |
return nullptr; |
| 116 |
} |
| 117 |
return env; |
| 118 |
} |
| 119 |
|
| 120 |
static inline JNIEnv* GetOrAttachJNIEnvironment(JavaVM* jvm, jint version = JNI_VERSION_1_4) { |
| 121 |
JNIEnv* env = GetJNIEnvironment(jvm, version); |
| 122 |
if (!env) { |
| 123 |
int result = jvm->AttachCurrentThread(&env, nullptr); |
| 124 |
LOG_ALWAYS_FATAL_IF(result != JNI_OK, "JVM thread attach failed."); |
| 125 |
struct VmDetacher { |
| 126 |
VmDetacher(JavaVM* vm) : mVm(vm) {} |
| 127 |
~VmDetacher() { mVm->DetachCurrentThread(); } |
| 128 |
|
| 129 |
private: |
| 130 |
JavaVM* const mVm; |
| 131 |
}; |
| 132 |
static thread_local VmDetacher detacher(jvm); |
| 133 |
} |
| 134 |
return env; |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
#endif |