• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JNI_JNI_CACHE_H_
16 #define ICING_JNI_JNI_CACHE_H_
17 
18 #ifndef ICING_REVERSE_JNI_SEGMENTATION
19 namespace icing {
20 namespace lib {
21 
22 class JniCache {};  // Declare an empty class definition for non-Android builds.
23 
24 }  // namespace lib
25 }  // namespace icing
26 #else  // ICING_REVERSE_JNI_SEGMENTATION
27 
28 #include <jni.h>
29 
30 #include "icing/text_classifier/lib3/utils/base/statusor.h"
31 #include "icing/text_classifier/lib3/utils/java/jni-base.h"
32 
33 namespace icing {
34 namespace lib {
35 
36 // A helper class to cache class and method pointers for calls from JNI to Java.
37 // (for implementations such as Java ICU that need to make calls from C++ to
38 // Java)
39 struct JniCache {
40   static libtextclassifier3::StatusOr<std::unique_ptr<JniCache>> Create(
41       JNIEnv* env);
42 
43   // Returns the correct JNIEnv of the current thread. This allows multiple
44   // threads, each accessing the same instance of JniCache, to retrieve their
45   // unique JNIEnv pointers.
46   JNIEnv* GetEnv() const;
47 
48   // Returns true if there are any pending exceptions from the execution of JNI
49   // calls. Also clears the exception if any existed.
50   bool ExceptionCheckAndClear() const;
51 
52   JavaVM* jvm = nullptr;
53 
54   // java.lang.String
55   libtextclassifier3::ScopedGlobalRef<jclass> string_class;
56   jmethodID string_constructor = nullptr;
57   jmethodID string_code_point_count = nullptr;
58   jmethodID string_length = nullptr;
59   libtextclassifier3::ScopedGlobalRef<jstring> string_utf8;
60 
61   // java.util.Locale
62   libtextclassifier3::ScopedGlobalRef<jclass> locale_class;
63   libtextclassifier3::ScopedGlobalRef<jobject> locale_us;
64   jmethodID locale_constructor = nullptr;
65   jmethodID locale_for_language_tag = nullptr;
66 
67   // BreakIteratorBatcher
68   libtextclassifier3::ScopedGlobalRef<jclass> breakiterator_class;
69   jmethodID breakiterator_constructor = nullptr;
70   jmethodID breakiterator_settext = nullptr;
71   jmethodID breakiterator_next = nullptr;
72   jmethodID breakiterator_first = nullptr;
73   jmethodID breakiterator_following = nullptr;
74   jmethodID breakiterator_preceding = nullptr;
75 
76   // Helper to convert lib3 UnicodeText to Java strings.
77   libtextclassifier3::StatusOr<libtextclassifier3::ScopedLocalRef<jstring>>
78   ConvertToJavaString(const char* utf8_text,
79                       const int utf8_text_size_bytes) const;
80 
81  private:
82   explicit JniCache(JavaVM* jvm);
83 };
84 
85 }  // namespace lib
86 }  // namespace icing
87 
88 #endif  // !ICING_REVERSE_JNI_SEGMENTATION
89 
90 #endif  // ICING_JNI_JNI_CACHE_H_
91