• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_CACHE_H_
18 #define LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_CACHE_H_
19 
20 #include <jni.h>
21 #include "utils/java/scoped_global_ref.h"
22 #include "utils/java/scoped_local_ref.h"
23 #include "utils/strings/stringpiece.h"
24 #include "utils/utf8/unicodetext.h"
25 
26 namespace libtextclassifier3 {
27 
28 // A helper class to cache class and method pointers for calls from JNI to Java.
29 // (for implementations such as Java ICU that need to make calls from C++ to
30 // Java)
31 struct JniCache {
32   static std::unique_ptr<JniCache> Create(JNIEnv* env);
33 
34   JNIEnv* GetEnv() const;
35   bool ExceptionCheckAndClear() const;
36 
37   JavaVM* jvm = nullptr;
38 
39   // java.lang.String
40   ScopedGlobalRef<jclass> string_class;
41   jmethodID string_init_bytes_charset = nullptr;
42   jmethodID string_code_point_count = nullptr;
43   jmethodID string_length = nullptr;
44   ScopedGlobalRef<jstring> string_utf8;
45 
46   // java.util.regex.Pattern
47   ScopedGlobalRef<jclass> pattern_class;
48   jmethodID pattern_compile = nullptr;
49   jmethodID pattern_matcher = nullptr;
50 
51   // java.util.regex.Matcher
52   ScopedGlobalRef<jclass> matcher_class;
53   jmethodID matcher_matches = nullptr;
54   jmethodID matcher_find = nullptr;
55   jmethodID matcher_reset = nullptr;
56   jmethodID matcher_start_idx = nullptr;
57   jmethodID matcher_end_idx = nullptr;
58   jmethodID matcher_group = nullptr;
59   jmethodID matcher_group_idx = nullptr;
60 
61   // java.util.Locale
62   ScopedGlobalRef<jclass> locale_class;
63   ScopedGlobalRef<jobject> locale_us;
64   jmethodID locale_init_string = nullptr;
65   jmethodID locale_for_language_tag = nullptr;
66 
67   // java.text.BreakIterator
68   ScopedGlobalRef<jclass> breakiterator_class;
69   jmethodID breakiterator_getwordinstance = nullptr;
70   jmethodID breakiterator_settext = nullptr;
71   jmethodID breakiterator_next = nullptr;
72 
73   // java.lang.Integer
74   ScopedGlobalRef<jclass> integer_class;
75   jmethodID integer_parse_int = nullptr;
76 
77   // java.util.Calendar
78   ScopedGlobalRef<jclass> calendar_class;
79   jmethodID calendar_get_instance = nullptr;
80   jmethodID calendar_get_first_day_of_week = nullptr;
81   jmethodID calendar_get_time_in_millis = nullptr;
82   jmethodID calendar_set_time_in_millis = nullptr;
83   jmethodID calendar_add = nullptr;
84   jmethodID calendar_get = nullptr;
85   jmethodID calendar_set = nullptr;
86   jint calendar_zone_offset;
87   jint calendar_dst_offset;
88   jint calendar_year;
89   jint calendar_month;
90   jint calendar_day_of_year;
91   jint calendar_day_of_month;
92   jint calendar_day_of_week;
93   jint calendar_hour_of_day;
94   jint calendar_minute;
95   jint calendar_second;
96   jint calendar_millisecond;
97   jint calendar_sunday;
98   jint calendar_monday;
99   jint calendar_tuesday;
100   jint calendar_wednesday;
101   jint calendar_thursday;
102   jint calendar_friday;
103   jint calendar_saturday;
104 
105   // java.util.TimeZone
106   ScopedGlobalRef<jclass> timezone_class;
107   jmethodID timezone_get_timezone = nullptr;
108 
109   // java.net.URLEncoder
110   ScopedGlobalRef<jclass> urlencoder_class;
111   jmethodID urlencoder_encode = nullptr;
112 
113   // android.content.Context
114   ScopedGlobalRef<jclass> context_class;
115   jmethodID context_get_package_name = nullptr;
116   jmethodID context_get_system_service = nullptr;
117 
118   // android.net.Uri
119   ScopedGlobalRef<jclass> uri_class;
120   jmethodID uri_parse = nullptr;
121   jmethodID uri_get_scheme = nullptr;
122   jmethodID uri_get_host = nullptr;
123 
124   // android.os.UserManager
125   ScopedGlobalRef<jclass> usermanager_class;
126   jmethodID usermanager_get_user_restrictions = nullptr;
127 
128   // android.os.Bundle
129   ScopedGlobalRef<jclass> bundle_class;
130   jmethodID bundle_get_boolean = nullptr;
131 
132   // android.content.res.Resources
133   ScopedGlobalRef<jclass> resources_class;
134   jmethodID resources_get_system = nullptr;
135   jmethodID resources_get_identifier = nullptr;
136   jmethodID resources_get_string = nullptr;
137 
138   // Helper to convert lib3 UnicodeText to Java strings.
139   ScopedLocalRef<jstring> ConvertToJavaString(
140       const char* utf8_text, const int utf8_text_size_bytes) const;
141   ScopedLocalRef<jstring> ConvertToJavaString(StringPiece utf8_text) const;
142   ScopedLocalRef<jstring> ConvertToJavaString(const UnicodeText& text) const;
143 
144  private:
145   explicit JniCache(JavaVM* jvm);
146 };
147 
148 }  // namespace libtextclassifier3
149 
150 #endif  // LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_CACHE_H_
151