Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 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/utf8/unicodetext.h" |
| 24 | |
| 25 | namespace libtextclassifier3 { |
| 26 | |
| 27 | // A helper class to cache class and method pointers for calls from JNI to Java. |
| 28 | // (for implementations such as Java ICU that need to make calls from C++ to |
| 29 | // Java) |
| 30 | struct JniCache { |
| 31 | static std::unique_ptr<JniCache> Create(JNIEnv* env); |
| 32 | |
| 33 | JNIEnv* GetEnv() const; |
| 34 | bool ExceptionCheckAndClear() const; |
| 35 | |
| 36 | JavaVM* jvm = nullptr; |
| 37 | |
| 38 | // java.lang.String |
| 39 | ScopedGlobalRef<jclass> string_class; |
| 40 | jmethodID string_init_bytes_charset = nullptr; |
| 41 | jmethodID string_code_point_count = nullptr; |
| 42 | jmethodID string_length = nullptr; |
| 43 | ScopedGlobalRef<jstring> string_utf8; |
| 44 | |
| 45 | // java.util.regex.Pattern |
| 46 | ScopedGlobalRef<jclass> pattern_class; |
| 47 | jmethodID pattern_compile = nullptr; |
| 48 | jmethodID pattern_matcher = nullptr; |
| 49 | |
| 50 | // java.util.regex.Matcher |
| 51 | ScopedGlobalRef<jclass> matcher_class; |
| 52 | jmethodID matcher_matches = nullptr; |
| 53 | jmethodID matcher_find = nullptr; |
| 54 | jmethodID matcher_reset = nullptr; |
| 55 | jmethodID matcher_start_idx = nullptr; |
| 56 | jmethodID matcher_end_idx = nullptr; |
| 57 | jmethodID matcher_group = nullptr; |
| 58 | jmethodID matcher_group_idx = nullptr; |
| 59 | |
| 60 | // java.util.Locale |
| 61 | ScopedGlobalRef<jclass> locale_class; |
| 62 | ScopedGlobalRef<jobject> locale_us; |
| 63 | jmethodID locale_init_string = nullptr; |
| 64 | jmethodID locale_for_language_tag = nullptr; |
| 65 | |
| 66 | // java.text.BreakIterator |
| 67 | ScopedGlobalRef<jclass> breakiterator_class; |
| 68 | jmethodID breakiterator_getwordinstance = nullptr; |
| 69 | jmethodID breakiterator_settext = nullptr; |
| 70 | jmethodID breakiterator_next = nullptr; |
| 71 | |
| 72 | // java.lang.Integer |
| 73 | ScopedGlobalRef<jclass> integer_class; |
| 74 | jmethodID integer_parse_int = nullptr; |
| 75 | |
| 76 | // java.util.Calendar |
| 77 | ScopedGlobalRef<jclass> calendar_class; |
| 78 | jmethodID calendar_get_instance = nullptr; |
| 79 | jmethodID calendar_get_first_day_of_week = nullptr; |
| 80 | jmethodID calendar_get_time_in_millis = nullptr; |
| 81 | jmethodID calendar_set_time_in_millis = nullptr; |
| 82 | jmethodID calendar_add = nullptr; |
| 83 | jmethodID calendar_get = nullptr; |
| 84 | jmethodID calendar_set = nullptr; |
| 85 | jint calendar_zone_offset; |
| 86 | jint calendar_dst_offset; |
| 87 | jint calendar_year; |
| 88 | jint calendar_month; |
| 89 | jint calendar_day_of_year; |
| 90 | jint calendar_day_of_month; |
| 91 | jint calendar_day_of_week; |
| 92 | jint calendar_hour_of_day; |
| 93 | jint calendar_minute; |
| 94 | jint calendar_second; |
| 95 | jint calendar_millisecond; |
| 96 | jint calendar_sunday; |
| 97 | jint calendar_monday; |
| 98 | jint calendar_tuesday; |
| 99 | jint calendar_wednesday; |
| 100 | jint calendar_thursday; |
| 101 | jint calendar_friday; |
| 102 | jint calendar_saturday; |
| 103 | |
| 104 | // java.util.TimeZone |
| 105 | ScopedGlobalRef<jclass> timezone_class; |
| 106 | jmethodID timezone_get_timezone = nullptr; |
| 107 | |
| 108 | // Helper to convert lib3 UnicodeText to Java strings. |
| 109 | ScopedLocalRef<jstring> ConvertToJavaString(const UnicodeText& text) const; |
| 110 | |
| 111 | private: |
| 112 | explicit JniCache(JavaVM* jvm); |
| 113 | }; |
| 114 | |
| 115 | } // namespace libtextclassifier3 |
| 116 | |
| 117 | #endif // LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_CACHE_H_ |