blob: 16ef753c0cd00bd5b729c009c06b7c88134a53a2 [file] [log] [blame]
Andreas Gampe987f79f2014-11-18 17:29:46 -08001/*
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
Steven Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
Siarhei Vishniakoue890f592018-10-15 18:53:06 -070021#include <nativehelper/scoped_local_ref.h>
22#include <nativehelper/scoped_utf_chars.h>
Andreas Gampe987f79f2014-11-18 17:29:46 -080023#include <android_runtime/AndroidRuntime.h>
24
25namespace android {
26
27// Defines some helpful functions.
28
29static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
30 jclass clazz = env->FindClass(class_name);
31 LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
32 return clazz;
33}
34
35static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
36 const char* field_signature) {
37 jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
38 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
39 return res;
40}
41
42static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
43 const char* method_signature) {
44 jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
45 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
46 return res;
47}
48
49static 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
56static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
57 const char* method_signature) {
58 jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
59 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
60 return res;
61}
62
63template <typename T>
64static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
65 jobject res = env->NewGlobalRef(in);
66 LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
67 return static_cast<T>(res);
68}
69
70static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
71 const JNINativeMethod* gMethods, int numMethods) {
72 int res = AndroidRuntime::registerNativeMethods(env, className, gMethods, numMethods);
73 LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
74 return res;
75}
76
Siarhei Vishniakoue890f592018-10-15 18:53:06 -070077/**
78 * Read the specified field from jobject, and convert to std::string.
79 * If the field cannot be obtained, return defaultValue.
80 */
81static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
82 const char* defaultValue) {
83 ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
84 if (strObj != nullptr) {
85 ScopedUtfChars chars(env, strObj.get());
86 return std::string(chars.c_str());
87 }
88 return std::string(defaultValue);
89}
90
Andreas Gampe987f79f2014-11-18 17:29:46 -080091} // namespace android
92
93#endif // CORE_JNI_HELPERS