summaryrefslogtreecommitdiff
path: root/native/utils/java/jni-base.h
blob: 211000a412bf414fc38fa6d791990794cf3031ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_BASE_H_
#define LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_BASE_H_

#include <jni.h>

#include <string>

#include "utils/base/statusor.h"

// When we use a macro as an argument for a macro, an additional level of
// indirection is needed, if the macro argument is used with # or ##.
#define TC3_ADD_QUOTES_HELPER(TOKEN) #TOKEN
#define TC3_ADD_QUOTES(TOKEN) TC3_ADD_QUOTES_HELPER(TOKEN)

#ifndef TC3_PACKAGE_NAME
#define TC3_PACKAGE_NAME com_google_android_textclassifier
#endif

#ifndef TC3_PACKAGE_PATH
#define TC3_PACKAGE_PATH \
  "com/google/android/textclassifier/"
#endif

#define TC3_JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name) \
  Java_##package_name##_##class_name##_##method_name

#define TC3_JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, \
                                 method_name)                           \
  JNIEXPORT return_type JNICALL TC3_JNI_METHOD_NAME_INTERNAL(           \
      package_name, class_name, method_name)

// The indirection is needed to correctly expand the TC3_PACKAGE_NAME macro.
// See the explanation near TC3_ADD_QUOTES macro.
#define TC3_JNI_METHOD2(return_type, package_name, class_name, method_name) \
  TC3_JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, method_name)

#define TC3_JNI_METHOD(return_type, class_name, method_name) \
  TC3_JNI_METHOD2(return_type, TC3_PACKAGE_NAME, class_name, method_name)

#define TC3_JNI_METHOD_NAME2(package_name, class_name, method_name) \
  TC3_JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name)

#define TC3_JNI_METHOD_NAME(class_name, method_name) \
  TC3_JNI_METHOD_NAME2(TC3_PACKAGE_NAME, class_name, method_name)

namespace libtextclassifier3 {

// Returns true if the requested capacity is available.
bool EnsureLocalCapacity(JNIEnv* env, int capacity);

// Returns true if there was an exception. Also it clears the exception.
bool JniExceptionCheckAndClear(JNIEnv* env,
                               bool print_exception_on_error = true);

// A deleter to be used with std::unique_ptr to delete JNI global references.
class GlobalRefDeleter {
 public:
  explicit GlobalRefDeleter(JavaVM* jvm) : jvm_(jvm) {}

  GlobalRefDeleter(const GlobalRefDeleter& orig) = default;

  // Copy assignment to allow move semantics in ScopedGlobalRef.
  GlobalRefDeleter& operator=(const GlobalRefDeleter& rhs) {
    TC3_CHECK_EQ(jvm_, rhs.jvm_);
    return *this;
  }

  // The delete operator.
  void operator()(jobject object) const {
    JNIEnv* env;
    if (object != nullptr && jvm_ != nullptr &&
        JNI_OK ==
            jvm_->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) {
      env->DeleteGlobalRef(object);
    }
  }

 private:
  // The jvm_ stashed to use for deletion.
  JavaVM* const jvm_;
};

// A deleter to be used with std::unique_ptr to delete JNI local references.
class LocalRefDeleter {
 public:
  explicit LocalRefDeleter(JNIEnv* env)
      : env_(env) {}  // NOLINT(runtime/explicit)

  LocalRefDeleter(const LocalRefDeleter& orig) = default;

  // Copy assignment to allow move semantics in ScopedLocalRef.
  LocalRefDeleter& operator=(const LocalRefDeleter& rhs) {
    env_ = rhs.env_;
    return *this;
  }

  // The delete operator.
  void operator()(jobject object) const {
    if (env_) {
      env_->DeleteLocalRef(object);
    }
  }

 private:
  // The env_ stashed to use for deletion. Thread-local, don't share!
  JNIEnv* env_;
};

// A smart pointer that deletes a reference when it goes out of scope.
//
// Note that this class is not thread-safe since it caches JNIEnv in
// the deleter. Do not use the same jobject across different threads.
template <typename T, typename Env, typename Deleter>
class ScopedRef {
 public:
  ScopedRef() : ptr_(nullptr, Deleter(nullptr)) {}
  ScopedRef(T value, Env* env) : ptr_(value, Deleter(env)) {}

  T get() const { return ptr_.get(); }

  T release() { return ptr_.release(); }

  bool operator!() const { return !ptr_; }

  bool operator==(void* value) const { return ptr_.get() == value; }

  explicit operator bool() const { return ptr_ != nullptr; }

  void reset(T value, Env* env) {
    ptr_.reset(value);
    ptr_.get_deleter() = Deleter(env);
  }

 private:
  std::unique_ptr<typename std::remove_pointer<T>::type, Deleter> ptr_;
};

template <typename T, typename U, typename Env, typename Deleter>
inline bool operator==(const ScopedRef<T, Env, Deleter>& x,
                       const ScopedRef<U, Env, Deleter>& y) {
  return x.get() == y.get();
}

template <typename T, typename Env, typename Deleter>
inline bool operator==(const ScopedRef<T, Env, Deleter>& x, std::nullptr_t) {
  return x.get() == nullptr;
}

template <typename T, typename Env, typename Deleter>
inline bool operator==(std::nullptr_t, const ScopedRef<T, Env, Deleter>& x) {
  return nullptr == x.get();
}

template <typename T, typename U, typename Env, typename Deleter>
inline bool operator!=(const ScopedRef<T, Env, Deleter>& x,
                       const ScopedRef<U, Env, Deleter>& y) {
  return x.get() != y.get();
}

template <typename T, typename Env, typename Deleter>
inline bool operator!=(const ScopedRef<T, Env, Deleter>& x, std::nullptr_t) {
  return x.get() != nullptr;
}

template <typename T, typename Env, typename Deleter>
inline bool operator!=(std::nullptr_t, const ScopedRef<T, Env, Deleter>& x) {
  return nullptr != x.get();
}

template <typename T, typename U, typename Env, typename Deleter>
inline bool operator<(const ScopedRef<T, Env, Deleter>& x,
                      const ScopedRef<U, Env, Deleter>& y) {
  return x.get() < y.get();
}

template <typename T, typename U, typename Env, typename Deleter>
inline bool operator>(const ScopedRef<T, Env, Deleter>& x,
                      const ScopedRef<U, Env, Deleter>& y) {
  return x.get() > y.get();
}

// A smart pointer that deletes a JNI global reference when it goes out
// of scope. Usage is:
// ScopedGlobalRef<jobject> scoped_global(env->JniFunction(), jvm);
template <typename T>
using ScopedGlobalRef = ScopedRef<T, JavaVM, GlobalRefDeleter>;

// Ditto, but usage is:
// ScopedLocalRef<jobject> scoped_local(env->JniFunction(), env);
template <typename T>
using ScopedLocalRef = ScopedRef<T, JNIEnv, LocalRefDeleter>;

// A helper to create global references.
template <typename T>
ScopedGlobalRef<T> MakeGlobalRef(T object, JNIEnv* env, JavaVM* jvm) {
  const jobject global_object = env->NewGlobalRef(object);
  return ScopedGlobalRef<T>(reinterpret_cast<T>(global_object), jvm);
}

}  // namespace libtextclassifier3

#endif  // LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_BASE_H_