1 /*
2 * Copyright (C) 2010 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 #define LOG_TAG "JniConstants"
18
19 #include "JniConstants.h"
20
21 #include <atomic>
22 #include <mutex>
23 #include <stdlib.h>
24
25 #include <log/log.h>
26
27 namespace {
28
findClass(JNIEnv * env,const char * name)29 jclass findClass(JNIEnv* env, const char* name) {
30 jclass result = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass(name)));
31 if (result == NULL) {
32 ALOGE("failed to find class '%s'", name);
33 abort();
34 }
35 return result;
36 }
37
38 static std::mutex g_constants_mutex;
39
40 // Flag indicating whether cached constants are valid
41 static bool g_constants_valid = false;
42
43 jclass charsetICUClass;
44 jclass patternSyntaxExceptionClass;
45 jclass stringClass;
46
47 // EnsureJniConstantsInitialized initializes cached constants. It should be
48 // called before returning a heap object from the cache to ensure cache is
49 // initialized. This pattern is only necessary because if a process finishes one
50 // runtime and starts another then JNI_OnLoad may not be called.
EnsureJniConstantsInitialized(JNIEnv * env)51 void EnsureJniConstantsInitialized(JNIEnv* env) {
52 if (g_constants_valid) {
53 return;
54 }
55
56 std::lock_guard guard(g_constants_mutex);
57 if (g_constants_valid) {
58 return;
59 }
60
61 charsetICUClass = findClass(env, "com/android/icu/charset/CharsetICU");
62 patternSyntaxExceptionClass = findClass(env, "java/util/regex/PatternSyntaxException");
63 stringClass = findClass(env, "java/lang/String");
64
65 g_constants_valid = true;
66 }
67
68 } // namespace
69
Initialize(JNIEnv * env)70 void JniConstants::Initialize(JNIEnv* env) {
71 EnsureJniConstantsInitialized(env);
72 }
73
GetCharsetICUClass(JNIEnv * env)74 jclass JniConstants::GetCharsetICUClass(JNIEnv* env) {
75 EnsureJniConstantsInitialized(env);
76 return charsetICUClass;
77 }
78
GetPatternSyntaxExceptionClass(JNIEnv * env)79 jclass JniConstants::GetPatternSyntaxExceptionClass(JNIEnv* env) {
80 EnsureJniConstantsInitialized(env);
81 return patternSyntaxExceptionClass;
82 }
83
GetStringClass(JNIEnv * env)84 jclass JniConstants::GetStringClass(JNIEnv* env) {
85 EnsureJniConstantsInitialized(env);
86 return stringClass;
87 }
88
Invalidate()89 void JniConstants::Invalidate() {
90 // Clean shutdown would require calling DeleteGlobalRef() for each of the
91 // class references. However, JavaVM can't be used for cleanup during
92 // JNI_OnUnload because ART only calls this once all threads are
93 // unregistered.
94 std::lock_guard guard(g_constants_mutex);
95 g_constants_valid = false;
96 }