1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 // Host targets (layoutlib) do not differentiate between regular and critical native methods,
26 // and they need all the JNI methods to have JNIEnv* and jclass/jobject as their first two arguments.
27 // The following macro allows to have those arguments when compiling for host while omitting them when
28 // compiling for Android.
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 // Defines some helpful functions.
40
FindClassOrDie(JNIEnv * env,const char * class_name)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
GetFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)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
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)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
GetStaticFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)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
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)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>
MakeGlobalRefOrDie(JNIEnv * env,T in)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
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)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 * Returns the result of invoking java.lang.ref.Reference.get() on a Reference object.
95 */
96 jobject GetReferent(JNIEnv* env, jobject ref);
97
98 /**
99 * Read the specified field from jobject, and convert to std::string.
100 * If the field cannot be obtained, return defaultValue.
101 */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)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 {
VmDetacherVmDetacher126 VmDetacher(JavaVM* vm) : mVm(vm) {}
~VmDetacherVmDetacher127 ~VmDetacher() { mVm->DetachCurrentThread(); }
128
129 private:
130 JavaVM* const mVm;
131 };
132 static thread_local VmDetacher detacher(jvm);
133 }
134 return env;
135 }
136
DieIfException(JNIEnv * env,const char * message)137 static inline void DieIfException(JNIEnv* env, const char* message) {
138 if (env->ExceptionCheck()) {
139 jnihelp::ExpandableString summary;
140 jnihelp::ExpandableStringInitialize(&summary);
141 jnihelp::GetStackTraceOrSummary(env, nullptr, &summary);
142 LOG_ALWAYS_FATAL("%s\n%s", message, summary.data);
143 }
144 }
145
146 } // namespace android
147
148 #endif // CORE_JNI_HELPERS
149