1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef ICING_TEXT_CLASSIFIER_LIB3_UTILS_JAVA_JNI_BASE_H_
16 #define ICING_TEXT_CLASSIFIER_LIB3_UTILS_JAVA_JNI_BASE_H_
17
18 #include <jni.h>
19
20 #include <string>
21
22 #include "icing/text_classifier/lib3/utils/base/statusor.h"
23
24 // When we use a macro as an argument for a macro, an additional level of
25 // indirection is needed, if the macro argument is used with # or ##.
26 #define TC3_ADD_QUOTES_HELPER(TOKEN) #TOKEN
27 #define TC3_ADD_QUOTES(TOKEN) TC3_ADD_QUOTES_HELPER(TOKEN)
28
29 #ifndef TC3_PACKAGE_NAME
30 #define TC3_PACKAGE_NAME com_google_knowledge_cerebra_sense_textclassifier_lib3
31 #endif
32
33 #ifndef TC3_PACKAGE_PATH
34 #define TC3_PACKAGE_PATH \
35 "com/google/knowledge/cerebra/sense/textclassifier/lib3/"
36 #endif
37
38 #define TC3_JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name) \
39 Java_##package_name##_##class_name##_##method_name
40
41 #define TC3_JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, \
42 method_name) \
43 JNIEXPORT return_type JNICALL TC3_JNI_METHOD_NAME_INTERNAL( \
44 package_name, class_name, method_name)
45
46 // The indirection is needed to correctly expand the TC3_PACKAGE_NAME macro.
47 // See the explanation near TC3_ADD_QUOTES macro.
48 #define TC3_JNI_METHOD2(return_type, package_name, class_name, method_name) \
49 TC3_JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, method_name)
50
51 #define TC3_JNI_METHOD(return_type, class_name, method_name) \
52 TC3_JNI_METHOD2(return_type, TC3_PACKAGE_NAME, class_name, method_name)
53
54 #define TC3_JNI_METHOD_NAME2(package_name, class_name, method_name) \
55 TC3_JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name)
56
57 #define TC3_JNI_METHOD_NAME(class_name, method_name) \
58 TC3_JNI_METHOD_NAME2(TC3_PACKAGE_NAME, class_name, method_name)
59
60 namespace libtextclassifier3 {
61
62 // Returns true if the requested capacity is available.
63 bool EnsureLocalCapacity(JNIEnv* env, int capacity);
64
65 // Returns true if there was an exception. Also it clears the exception.
66 bool JniExceptionCheckAndClear(JNIEnv* env,
67 bool print_exception_on_error = true);
68
69 // A deleter to be used with std::unique_ptr to delete JNI global references.
70 class GlobalRefDeleter {
71 public:
GlobalRefDeleter(JavaVM * jvm)72 explicit GlobalRefDeleter(JavaVM* jvm) : jvm_(jvm) {}
73
74 GlobalRefDeleter(const GlobalRefDeleter& orig) = default;
75
76 // Copy assignment to allow move semantics in ScopedGlobalRef.
77 GlobalRefDeleter& operator=(const GlobalRefDeleter& rhs) {
78 TC3_CHECK_EQ(jvm_, rhs.jvm_);
79 return *this;
80 }
81
82 // The delete operator.
operator()83 void operator()(jobject object) const {
84 JNIEnv* env;
85 if (object != nullptr && jvm_ != nullptr &&
86 JNI_OK ==
87 jvm_->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) {
88 env->DeleteGlobalRef(object);
89 }
90 }
91
92 private:
93 // The jvm_ stashed to use for deletion.
94 JavaVM* const jvm_;
95 };
96
97 // A deleter to be used with std::unique_ptr to delete JNI local references.
98 class LocalRefDeleter {
99 public:
LocalRefDeleter(JNIEnv * env)100 explicit LocalRefDeleter(JNIEnv* env)
101 : env_(env) {} // NOLINT(runtime/explicit)
102
103 LocalRefDeleter(const LocalRefDeleter& orig) = default;
104
105 // Copy assignment to allow move semantics in ScopedLocalRef.
106 LocalRefDeleter& operator=(const LocalRefDeleter& rhs) {
107 env_ = rhs.env_;
108 return *this;
109 }
110
111 // The delete operator.
operator()112 void operator()(jobject object) const {
113 if (env_) {
114 env_->DeleteLocalRef(object);
115 }
116 }
117
118 private:
119 // The env_ stashed to use for deletion. Thread-local, don't share!
120 JNIEnv* env_;
121 };
122
123 // A smart pointer that deletes a reference when it goes out of scope.
124 //
125 // Note that this class is not thread-safe since it caches JNIEnv in
126 // the deleter. Do not use the same jobject across different threads.
127 template <typename T, typename Env, typename Deleter>
128 class ScopedRef {
129 public:
ScopedRef()130 ScopedRef() : ptr_(nullptr, Deleter(nullptr)) {}
ScopedRef(T value,Env * env)131 ScopedRef(T value, Env* env) : ptr_(value, Deleter(env)) {}
132
get()133 T get() const { return ptr_.get(); }
134
release()135 T release() { return ptr_.release(); }
136
137 bool operator!() const { return !ptr_; }
138
139 bool operator==(void* value) const { return ptr_.get() == value; }
140
141 explicit operator bool() const { return ptr_ != nullptr; }
142
reset(T value,Env * env)143 void reset(T value, Env* env) {
144 ptr_.reset(value);
145 ptr_.get_deleter() = Deleter(env);
146 }
147
148 private:
149 std::unique_ptr<typename std::remove_pointer<T>::type, Deleter> ptr_;
150 };
151
152 template <typename T, typename U, typename Env, typename Deleter>
153 inline bool operator==(const ScopedRef<T, Env, Deleter>& x,
154 const ScopedRef<U, Env, Deleter>& y) {
155 return x.get() == y.get();
156 }
157
158 template <typename T, typename Env, typename Deleter>
159 inline bool operator==(const ScopedRef<T, Env, Deleter>& x, std::nullptr_t) {
160 return x.get() == nullptr;
161 }
162
163 template <typename T, typename Env, typename Deleter>
164 inline bool operator==(std::nullptr_t, const ScopedRef<T, Env, Deleter>& x) {
165 return nullptr == x.get();
166 }
167
168 template <typename T, typename U, typename Env, typename Deleter>
169 inline bool operator!=(const ScopedRef<T, Env, Deleter>& x,
170 const ScopedRef<U, Env, Deleter>& y) {
171 return x.get() != y.get();
172 }
173
174 template <typename T, typename Env, typename Deleter>
175 inline bool operator!=(const ScopedRef<T, Env, Deleter>& x, std::nullptr_t) {
176 return x.get() != nullptr;
177 }
178
179 template <typename T, typename Env, typename Deleter>
180 inline bool operator!=(std::nullptr_t, const ScopedRef<T, Env, Deleter>& x) {
181 return nullptr != x.get();
182 }
183
184 template <typename T, typename U, typename Env, typename Deleter>
185 inline bool operator<(const ScopedRef<T, Env, Deleter>& x,
186 const ScopedRef<U, Env, Deleter>& y) {
187 return x.get() < y.get();
188 }
189
190 template <typename T, typename U, typename Env, typename Deleter>
191 inline bool operator>(const ScopedRef<T, Env, Deleter>& x,
192 const ScopedRef<U, Env, Deleter>& y) {
193 return x.get() > y.get();
194 }
195
196 // A smart pointer that deletes a JNI global reference when it goes out
197 // of scope. Usage is:
198 // ScopedGlobalRef<jobject> scoped_global(env->JniFunction(), jvm);
199 template <typename T>
200 using ScopedGlobalRef = ScopedRef<T, JavaVM, GlobalRefDeleter>;
201
202 // Ditto, but usage is:
203 // ScopedLocalRef<jobject> scoped_local(env->JniFunction(), env);
204 template <typename T>
205 using ScopedLocalRef = ScopedRef<T, JNIEnv, LocalRefDeleter>;
206
207 // A helper to create global references.
208 template <typename T>
MakeGlobalRef(T object,JNIEnv * env,JavaVM * jvm)209 ScopedGlobalRef<T> MakeGlobalRef(T object, JNIEnv* env, JavaVM* jvm) {
210 const jobject global_object = env->NewGlobalRef(object);
211 return ScopedGlobalRef<T>(reinterpret_cast<T>(global_object), jvm);
212 }
213
214 } // namespace libtextclassifier3
215
216 #endif // ICING_TEXT_CLASSIFIER_LIB3_UTILS_JAVA_JNI_BASE_H_
217