xref: /aosp_15_r20/frameworks/base/core/jni/jni_wrappers.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 // JNI wrappers for better logging
20 
21 #include <jni.h>
22 #include <log/log.h>
23 #include <nativehelper/JNIHelp.h>
24 
25 namespace android {
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 field %s with signature %s", field_name,
37                         field_signature);
38     return res;
39 }
40 
GetMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)41 static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
42                                          const char* method_signature) {
43     jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
44     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s with signature %s", method_name,
45                         method_signature);
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 with signature %s", field_name,
53                         field_signature);
54     return res;
55 }
56 
GetStaticMethodIDOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)57 static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
58                                                const char* method_signature) {
59     jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
60     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s with signature %s",
61                         method_name, method_signature);
62     return res;
63 }
64 
65 template <typename T>
MakeGlobalRefOrDie(JNIEnv * env,T in)66 static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
67     jobject res = env->NewGlobalRef(in);
68     LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
69     return static_cast<T>(res);
70 }
71 
72 //  Inline variable that specifies the method binding format.
73 //  The expected format is 'XX${method}XX', where ${method} represents the original method name.
74 //  This variable is shared across all translation units. This is treated as a global variable as
75 //  per C++ 17.
76 inline std::string jniMethodFormat;
77 
setJniMethodFormat(std::string value)78 inline static void setJniMethodFormat(std::string value) {
79     jniMethodFormat = value;
80 }
81 
82 // Register the native methods, potenially applying the jniMethodFormat if it has been set.
jniRegisterMaybeRenamedNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)83 static inline int jniRegisterMaybeRenamedNativeMethods(JNIEnv* env, const char* className,
84                                                        const JNINativeMethod* gMethods,
85                                                        int numMethods) {
86     if (jniMethodFormat.empty()) {
87         return jniRegisterNativeMethods(env, className, gMethods, numMethods);
88     }
89 
90     // Make a copy of gMethods with reformatted method names.
91     JNINativeMethod* modifiedMethods = new JNINativeMethod[numMethods];
92     LOG_ALWAYS_FATAL_IF(!modifiedMethods, "Failed to allocate a copy of the JNI methods");
93 
94     size_t methodNamePos = jniMethodFormat.find("${method}");
95     LOG_ALWAYS_FATAL_IF(methodNamePos == std::string::npos,
96                         "Invalid jniMethodFormat: could not find '${method}' in pattern");
97 
98     for (int i = 0; i < numMethods; i++) {
99         modifiedMethods[i] = gMethods[i];
100         std::string modifiedName = jniMethodFormat;
101         modifiedName.replace(methodNamePos, 9, gMethods[i].name);
102         char* modifiedNameChars = new char[modifiedName.length() + 1];
103         LOG_ALWAYS_FATAL_IF(!modifiedNameChars, "Failed to allocate the new method name");
104         std::strcpy(modifiedNameChars, modifiedName.c_str());
105         modifiedMethods[i].name = modifiedNameChars;
106     }
107     int res = jniRegisterNativeMethods(env, className, modifiedMethods, numMethods);
108     for (int i = 0; i < numMethods; i++) {
109         delete[] modifiedMethods[i].name;
110     }
111     delete[] modifiedMethods;
112     return res;
113 }
114 
RegisterMethodsOrDie(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)115 static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
116                                        const JNINativeMethod* gMethods, int numMethods) {
117     int res = jniRegisterMaybeRenamedNativeMethods(env, className, gMethods, numMethods);
118     LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
119     return res;
120 }
121 
122 } // namespace android
123