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.
FindClassOrDie(JNIEnv * env,const char * class_name)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
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)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
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)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
GetFieldIDOrDie(JNIEnv * env,jclass clazz,const char * field_name,const char * field_signature)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
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
GetStaticIntFieldValueOrDie(JNIEnv * env,jclass clazz,const char * fieldName)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
GetStringField(JNIEnv * env,jobject obj,jfieldID field)62 static inline jstring GetStringField(JNIEnv* env, jobject obj, jfieldID field) {
63 return reinterpret_cast<jstring>(env->GetObjectField(obj, field));
64 }
65
GetByteArrayField(JNIEnv * env,jobject obj,jfieldID field)66 static inline jbyteArray GetByteArrayField(JNIEnv* env, jobject obj, jfieldID field) {
67 return reinterpret_cast<jbyteArray>(env->GetObjectField(obj, field));
68 }
69
GetJNIEnvironment(JavaVM * vm)70 static inline JNIEnv* GetJNIEnvironment(JavaVM* vm) {
71 JNIEnv* env;
72 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
73 return 0;
74 }
75 return env;
76 }
77
GetOrAttachJNIEnvironment(JavaVM * jvm)78 static inline JNIEnv* GetOrAttachJNIEnvironment(JavaVM* jvm) {
79 JNIEnv* env = GetJNIEnvironment(jvm);
80 if (!env) {
81 int result = jvm->AttachCurrentThread(&env, nullptr);
82 CHECK_EQ(result, JNI_OK) << "thread attach failed";
83 struct VmDetacher {
84 VmDetacher(JavaVM* vm) : mVm(vm) {}
85 ~VmDetacher() { mVm->DetachCurrentThread(); }
86
87 private:
88 JavaVM* const mVm;
89 };
90 static thread_local VmDetacher detacher(jvm);
91 }
92 return env;
93 }
94