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