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 "JNIHelp.h"
21 #include <android_runtime/AndroidRuntime.h>
22
23 namespace android {
24
25 // Defines some helpful functions.
26
FindClassOrDie(JNIEnv * env,const char * class_name)27 static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
28 jclass clazz = env->FindClass(class_name);
29 LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
30 return clazz;
31 }
32
GetFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)33 static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
34 const char* field_signature) {
35 jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
36 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
37 return res;
38 }
39
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)40 static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
41 const char* method_signature) {
42 jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
43 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
44 return res;
45 }
46
GetStaticFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)47 static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
48 const char* field_signature) {
49 jfieldID res = env->GetStaticFieldID(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
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)54 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
55 const char* method_signature) {
56 jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
57 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
58 return res;
59 }
60
61 template <typename T>
MakeGlobalRefOrDie(JNIEnv * env,T in)62 static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
63 jobject res = env->NewGlobalRef(in);
64 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
65 return static_cast<T>(res);
66 }
67
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)68 static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
69 const JNINativeMethod* gMethods, int numMethods) {
70 int res = AndroidRuntime::registerNativeMethods(env, className, gMethods, numMethods);
71 LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
72 return res;
73 }
74
75 } // namespace android
76
77 #endif // CORE_JNI_HELPERS
78