1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <jni.h>
19 #include <log/log.h>
20 
21 // JNI helpers.
22 static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
23     jclass clazz = env->FindClass(class_name);
24     LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
25     return clazz;
26 }
27 
28 static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
29                                          const char* method_signature) {
30     jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
31     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
32     return res;
33 }
34 
35 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
36                                                const char* method_signature) {
37     jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
38     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
39     return res;
40 }
41 
42 static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
43                                        const char* field_signature) {
44     jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
45     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
46     return res;
47 }
48 
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 
56 static inline jint GetStaticIntFieldValueOrDie(JNIEnv* env, jclass clazz, const char* fieldName) {
57     jfieldID res = env->GetStaticFieldID(clazz, fieldName, "I");
58     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", fieldName);
59     return env->GetStaticIntField(clazz, res);
60 }
61 
62 static inline JNIEnv* GetJNIEnvironment(JavaVM* vm) {
63     JNIEnv* env;
64     if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
65         return 0;
66     }
67     return env;
68 }
69 
70 static inline JNIEnv* GetOrAttachJNIEnvironment(JavaVM* jvm) {
71     JNIEnv* env = GetJNIEnvironment(jvm);
72     if (!env) {
73         int result = jvm->AttachCurrentThread(&env, nullptr);
74         CHECK_EQ(result, JNI_OK) << "thread attach failed";
75         struct VmDetacher {
76             VmDetacher(JavaVM* vm) : mVm(vm) {}
77             ~VmDetacher() { mVm->DetachCurrentThread(); }
78 
79         private:
80             JavaVM* const mVm;
81         };
82         static thread_local VmDetacher detacher(jvm);
83     }
84     return env;
85 }
86