liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
/*
 * 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_CACHE_H_
#define LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_CACHE_H_
 
#include <jni.h>
#include "utils/java/scoped_global_ref.h"
#include "utils/java/scoped_local_ref.h"
#include "utils/strings/stringpiece.h"
#include "utils/utf8/unicodetext.h"
 
namespace libtextclassifier3 {
 
// A helper class to cache class and method pointers for calls from JNI to Java.
// (for implementations such as Java ICU that need to make calls from C++ to
// Java)
struct JniCache {
  static std::unique_ptr<JniCache> Create(JNIEnv* env);
 
  JNIEnv* GetEnv() const;
  bool ExceptionCheckAndClear() const;
 
  JavaVM* jvm = nullptr;
 
  // java.lang.String
  ScopedGlobalRef<jclass> string_class;
  jmethodID string_init_bytes_charset = nullptr;
  jmethodID string_code_point_count = nullptr;
  jmethodID string_length = nullptr;
  ScopedGlobalRef<jstring> string_utf8;
 
  // java.util.regex.Pattern
  ScopedGlobalRef<jclass> pattern_class;
  jmethodID pattern_compile = nullptr;
  jmethodID pattern_matcher = nullptr;
 
  // java.util.regex.Matcher
  ScopedGlobalRef<jclass> matcher_class;
  jmethodID matcher_matches = nullptr;
  jmethodID matcher_find = nullptr;
  jmethodID matcher_reset = nullptr;
  jmethodID matcher_start_idx = nullptr;
  jmethodID matcher_end_idx = nullptr;
  jmethodID matcher_group = nullptr;
  jmethodID matcher_group_idx = nullptr;
 
  // java.util.Locale
  ScopedGlobalRef<jclass> locale_class;
  ScopedGlobalRef<jobject> locale_us;
  jmethodID locale_init_string = nullptr;
  jmethodID locale_for_language_tag = nullptr;
 
  // java.text.BreakIterator
  ScopedGlobalRef<jclass> breakiterator_class;
  jmethodID breakiterator_getwordinstance = nullptr;
  jmethodID breakiterator_settext = nullptr;
  jmethodID breakiterator_next = nullptr;
 
  // java.lang.Integer
  ScopedGlobalRef<jclass> integer_class;
  jmethodID integer_parse_int = nullptr;
 
  // java.util.Calendar
  ScopedGlobalRef<jclass> calendar_class;
  jmethodID calendar_get_instance = nullptr;
  jmethodID calendar_get_first_day_of_week = nullptr;
  jmethodID calendar_get_time_in_millis = nullptr;
  jmethodID calendar_set_time_in_millis = nullptr;
  jmethodID calendar_add = nullptr;
  jmethodID calendar_get = nullptr;
  jmethodID calendar_set = nullptr;
  jint calendar_zone_offset;
  jint calendar_dst_offset;
  jint calendar_year;
  jint calendar_month;
  jint calendar_day_of_year;
  jint calendar_day_of_month;
  jint calendar_day_of_week;
  jint calendar_hour_of_day;
  jint calendar_minute;
  jint calendar_second;
  jint calendar_millisecond;
  jint calendar_sunday;
  jint calendar_monday;
  jint calendar_tuesday;
  jint calendar_wednesday;
  jint calendar_thursday;
  jint calendar_friday;
  jint calendar_saturday;
 
  // java.util.TimeZone
  ScopedGlobalRef<jclass> timezone_class;
  jmethodID timezone_get_timezone = nullptr;
 
  // java.net.URLEncoder
  ScopedGlobalRef<jclass> urlencoder_class;
  jmethodID urlencoder_encode = nullptr;
 
  // android.content.Context
  ScopedGlobalRef<jclass> context_class;
  jmethodID context_get_package_name = nullptr;
  jmethodID context_get_system_service = nullptr;
 
  // android.net.Uri
  ScopedGlobalRef<jclass> uri_class;
  jmethodID uri_parse = nullptr;
  jmethodID uri_get_scheme = nullptr;
  jmethodID uri_get_host = nullptr;
 
  // android.os.UserManager
  ScopedGlobalRef<jclass> usermanager_class;
  jmethodID usermanager_get_user_restrictions = nullptr;
 
  // android.os.Bundle
  ScopedGlobalRef<jclass> bundle_class;
  jmethodID bundle_get_boolean = nullptr;
 
  // android.content.res.Resources
  ScopedGlobalRef<jclass> resources_class;
  jmethodID resources_get_system = nullptr;
  jmethodID resources_get_identifier = nullptr;
  jmethodID resources_get_string = nullptr;
 
  // Helper to convert lib3 UnicodeText to Java strings.
  ScopedLocalRef<jstring> ConvertToJavaString(
      const char* utf8_text, const int utf8_text_size_bytes) const;
  ScopedLocalRef<jstring> ConvertToJavaString(StringPiece utf8_text) const;
  ScopedLocalRef<jstring> ConvertToJavaString(const UnicodeText& text) const;
 
 private:
  explicit JniCache(JavaVM* jvm);
};
 
}  // namespace libtextclassifier3
 
#endif  // LIBTEXTCLASSIFIER_UTILS_JAVA_JNI_CACHE_H_