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 static field %s", field_name);
51 return res;
52 }
53
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)54 static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
55 const char* method_signature) {
56 jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
57 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
58 return res;
59 }
60
GetStaticFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)61 static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
62 const char* field_signature) {
63 jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature);
64 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
65 return res;
66 }
67
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)68 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
69 const char* method_signature) {
70 jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
71 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
72 return res;
73 }
74
75 template <typename T>
MakeGlobalRefOrDie(JNIEnv * env,T in)76 static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
77 jobject res = env->NewGlobalRef(in);
78 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
79 return static_cast<T>(res);
80 }
81
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)82 static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
83 const JNINativeMethod* gMethods, int numMethods) {
84 int res = AndroidRuntime::registerNativeMethods(env, className, gMethods, numMethods);
85 LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
86 return res;
87 }
88
89 /**
90 * Read the specified field from jobject, and convert to std::string.
91 * If the field cannot be obtained, return defaultValue.
92 */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)93 static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
94 const char* defaultValue) {
95 ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
96 if (strObj != nullptr) {
97 ScopedUtfChars chars(env, strObj.get());
98 return std::string(chars.c_str());
99 }
100 return std::string(defaultValue);
101 }
102
103 } // namespace android
104
105 #endif // CORE_JNI_HELPERS
106