• 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/JNIHelp.h>
21 #include <nativehelper/scoped_local_ref.h>
22 #include <nativehelper/scoped_utf_chars.h>
23 #include <android_runtime/AndroidRuntime.h>
24 
25 namespace android {
26 
27 // Defines some helpful functions.
28 
FindClassOrDie(JNIEnv * env,const char * class_name)29 static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
30     jclass clazz = env->FindClass(class_name);
31     LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
32     return clazz;
33 }
34 
GetFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)35 static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
36                                        const char* field_signature) {
37     jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
38     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
39     return res;
40 }
41 
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)42 static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
43                                          const char* method_signature) {
44     jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
45     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
46     return res;
47 }
48 
GetStaticFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)49 static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
50                                              const char* field_signature) {
51     jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature);
52     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
53     return res;
54 }
55 
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)56 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
57                                                const char* method_signature) {
58     jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
59     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
60     return res;
61 }
62 
63 template <typename T>
MakeGlobalRefOrDie(JNIEnv * env,T in)64 static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
65     jobject res = env->NewGlobalRef(in);
66     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
67     return static_cast<T>(res);
68 }
69 
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)70 static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
71                                        const JNINativeMethod* gMethods, int numMethods) {
72     int res = AndroidRuntime::registerNativeMethods(env, className, gMethods, numMethods);
73     LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
74     return res;
75 }
76 
77 /**
78  * Read the specified field from jobject, and convert to std::string.
79  * If the field cannot be obtained, return defaultValue.
80  */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)81 static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
82         const char* defaultValue) {
83     ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
84     if (strObj != nullptr) {
85         ScopedUtfChars chars(env, strObj.get());
86         return std::string(chars.c_str());
87     }
88     return std::string(defaultValue);
89 }
90 
91 }  // namespace android
92 
93 #endif  // CORE_JNI_HELPERS
94