• 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 GRAPHICS_JNI_HELPERS
18 #define GRAPHICS_JNI_HELPERS
19 
20 #include <log/log.h>
21 #include <nativehelper/JNIPlatformHelp.h>
22 #include <nativehelper/scoped_local_ref.h>
23 #include <nativehelper/scoped_utf_chars.h>
24 #include <string>
25 
26 // Host targets (layoutlib) do not differentiate between regular and critical native methods,
27 // and they need all the JNI methods to have JNIEnv* and jclass/jobject as their first two arguments.
28 // The following macro allows to have those arguments when compiling for host while omitting them when
29 // compiling for Android.
30 #ifdef __ANDROID__
31 #define CRITICAL_JNI_PARAMS
32 #define CRITICAL_JNI_PARAMS_COMMA
33 #else
34 #define CRITICAL_JNI_PARAMS JNIEnv*, jclass
35 #define CRITICAL_JNI_PARAMS_COMMA JNIEnv*, jclass,
36 #endif
37 
38 namespace android {
39 
40 // Defines some helpful functions.
41 
FindClassOrDie(JNIEnv * env,const char * class_name)42 static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
43     jclass clazz = env->FindClass(class_name);
44     LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
45     return clazz;
46 }
47 
GetFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)48 static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
49                                        const char* field_signature) {
50     jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
51     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
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", method_name);
59     return res;
60 }
61 
GetStaticFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)62 static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
63                                              const char* field_signature) {
64     jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature);
65     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
66     return res;
67 }
68 
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)69 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
70                                                const char* method_signature) {
71     jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
72     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
73     return res;
74 }
75 
76 template <typename T>
MakeGlobalRefOrDie(JNIEnv * env,T in)77 static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
78     jobject res = env->NewGlobalRef(in);
79     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
80     return static_cast<T>(res);
81 }
82 
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)83 static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
84                                        const JNINativeMethod* gMethods, int numMethods) {
85     int res = jniRegisterNativeMethods(env, className, gMethods, numMethods);
86     LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
87     return res;
88 }
89 
90 /**
91  * Read the specified field from jobject, and convert to std::string.
92  * If the field cannot be obtained, return defaultValue.
93  */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)94 static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
95         const char* defaultValue) {
96     ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
97     if (strObj != nullptr) {
98         ScopedUtfChars chars(env, strObj.get());
99         return std::string(chars.c_str());
100     }
101     return std::string(defaultValue);
102 }
103 
104 }  // namespace android
105 
106 #endif  // GRAPHICS_JNI_HELPERS
107