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/JNIPlatformHelp.h>
21 #include <nativehelper/scoped_local_ref.h>
22 #include <nativehelper/scoped_utf_chars.h>
23 #include <android_runtime/AndroidRuntime.h>
24 
25 #include "jni_wrappers.h"
26 
27 // Host targets (layoutlib) do not differentiate between regular and critical native methods,
28 // and they need all the JNI methods to have JNIEnv* and jclass/jobject as their first two arguments.
29 // The following macro allows to have those arguments when compiling for host while omitting them when
30 // compiling for Android.
31 #ifdef __ANDROID__
32 #define CRITICAL_JNI_PARAMS
33 #define CRITICAL_JNI_PARAMS_COMMA
34 #else
35 #define CRITICAL_JNI_PARAMS JNIEnv*, jclass
36 #define CRITICAL_JNI_PARAMS_COMMA JNIEnv*, jclass,
37 #endif
38 
39 namespace android {
40 
41 /**
42  * Returns the result of invoking java.lang.ref.Reference.get() on a Reference object.
43  */
44 jobject GetReferent(JNIEnv* env, jobject ref);
45 
46 /**
47  * Read the specified field from jobject, and convert to std::string.
48  * If the field cannot be obtained, return defaultValue.
49  */
getStringField(JNIEnv * env,jobject obj,jfieldID fieldId,const char * defaultValue)50 static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
51         const char* defaultValue) {
52     ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
53     if (strObj != nullptr) {
54         ScopedUtfChars chars(env, strObj.get());
55         return std::string(chars.c_str());
56     }
57     return std::string(defaultValue);
58 }
59 
60 static inline JNIEnv* GetJNIEnvironment(JavaVM* vm, jint version = JNI_VERSION_1_4) {
61     JNIEnv* env;
62     if (vm->GetEnv(reinterpret_cast<void**>(&env), version) != JNI_OK) {
63         return nullptr;
64     }
65     return env;
66 }
67 
68 static inline JNIEnv* GetOrAttachJNIEnvironment(JavaVM* jvm, jint version = JNI_VERSION_1_4) {
69     JNIEnv* env = GetJNIEnvironment(jvm, version);
70     if (!env) {
71         int result = jvm->AttachCurrentThread(&env, nullptr);
72         LOG_ALWAYS_FATAL_IF(result != JNI_OK, "JVM thread attach failed.");
73         struct VmDetacher {
VmDetacherVmDetacher74             VmDetacher(JavaVM* vm) : mVm(vm) {}
~VmDetacherVmDetacher75             ~VmDetacher() { mVm->DetachCurrentThread(); }
76 
77         private:
78             JavaVM* const mVm;
79         };
80         static thread_local VmDetacher detacher(jvm);
81     }
82     return env;
83 }
84 
DieIfException(JNIEnv * env,const char * message)85 static inline void DieIfException(JNIEnv* env, const char* message) {
86     if (env->ExceptionCheck()) {
87         jnihelp::ExpandableString summary;
88         jnihelp::ExpandableStringInitialize(&summary);
89         jnihelp::GetStackTraceOrSummary(env, nullptr, &summary);
90         LOG_ALWAYS_FATAL("%s\n%s", message, summary.data);
91     }
92 }
93 
94 }  // namespace android
95 
96 #endif  // CORE_JNI_HELPERS
97