• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
jniGetReferent(JNIEnv * env,jobject ref)93 static inline jobject jniGetReferent(JNIEnv* env, jobject ref) {
94     jclass cls = FindClassOrDie(env, "java/lang/ref/Reference");
95     jmethodID get = GetMethodIDOrDie(env, cls, "get", "()Ljava/lang/Object;");
96     return env->CallObjectMethod(ref, get);
97 }
98 
99 /**
100  * Read the specified field from jobject, and convert to std::string.
101  * If the field cannot be obtained, return defaultValue.
102  */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)103 static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
104         const char* defaultValue) {
105     ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
106     if (strObj != nullptr) {
107         ScopedUtfChars chars(env, strObj.get());
108         return std::string(chars.c_str());
109     }
110     return std::string(defaultValue);
111 }
112 
113 static inline JNIEnv* GetJNIEnvironment(JavaVM* vm, jint version = JNI_VERSION_1_4) {
114     JNIEnv* env;
115     if (vm->GetEnv(reinterpret_cast<void**>(&env), version) != JNI_OK) {
116         return nullptr;
117     }
118     return env;
119 }
120 
121 static inline JNIEnv* GetOrAttachJNIEnvironment(JavaVM* jvm, jint version = JNI_VERSION_1_4) {
122     JNIEnv* env = GetJNIEnvironment(jvm, version);
123     if (!env) {
124         int result = jvm->AttachCurrentThread(&env, nullptr);
125         LOG_ALWAYS_FATAL_IF(result != JNI_OK, "JVM thread attach failed.");
126         struct VmDetacher {
VmDetacherVmDetacher127             VmDetacher(JavaVM* vm) : mVm(vm) {}
~VmDetacherVmDetacher128             ~VmDetacher() { mVm->DetachCurrentThread(); }
129 
130         private:
131             JavaVM* const mVm;
132         };
133         static thread_local VmDetacher detacher(jvm);
134     }
135     return env;
136 }
137 
138 }  // namespace android
139 
140 #endif  // CORE_JNI_HELPERS
141