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 "ALog-priv.h"
20 #include "JniConstants.h"
21 #include "ScopedLocalRef.h"
22
23 #include <stdlib.h>
24
25 #include <atomic>
26 #include <mutex>
27
28 static std::atomic<bool> g_constants_initialized(false);
29 static std::mutex g_constants_mutex;
30
31 jclass JniConstants::bigDecimalClass;
32 jclass JniConstants::booleanClass;
33 jclass JniConstants::byteArrayClass;
34 jclass JniConstants::byteClass;
35 jclass JniConstants::calendarClass;
36 jclass JniConstants::characterClass;
37 jclass JniConstants::charsetICUClass;
38 jclass JniConstants::constructorClass;
39 jclass JniConstants::deflaterClass;
40 jclass JniConstants::doubleClass;
41 jclass JniConstants::errnoExceptionClass;
42 jclass JniConstants::fieldClass;
43 jclass JniConstants::fileDescriptorClass;
44 jclass JniConstants::floatClass;
45 jclass JniConstants::gaiExceptionClass;
46 jclass JniConstants::inet6AddressClass;
47 jclass JniConstants::inetAddressClass;
48 jclass JniConstants::inetAddressHolderClass;
49 jclass JniConstants::inetSocketAddressClass;
50 jclass JniConstants::inetSocketAddressHolderClass;
51 jclass JniConstants::inflaterClass;
52 jclass JniConstants::inputStreamClass;
53 jclass JniConstants::integerClass;
54 jclass JniConstants::localeDataClass;
55 jclass JniConstants::longClass;
56 jclass JniConstants::methodClass;
57 jclass JniConstants::mutableIntClass;
58 jclass JniConstants::mutableLongClass;
59 jclass JniConstants::netlinkSocketAddressClass;
60 jclass JniConstants::objectClass;
61 jclass JniConstants::objectArrayClass;
62 jclass JniConstants::outputStreamClass;
63 jclass JniConstants::packetSocketAddressClass;
64 jclass JniConstants::parsePositionClass;
65 jclass JniConstants::patternSyntaxExceptionClass;
66 jclass JniConstants::referenceClass;
67 jclass JniConstants::shortClass;
68 jclass JniConstants::socketClass;
69 jclass JniConstants::socketImplClass;
70 jclass JniConstants::socketTaggerClass;
71 jclass JniConstants::stringClass;
72 jclass JniConstants::structAddrinfoClass;
73 jclass JniConstants::structFlockClass;
74 jclass JniConstants::structGroupReqClass;
75 jclass JniConstants::structGroupSourceReqClass;
76 jclass JniConstants::structLingerClass;
77 jclass JniConstants::structPasswdClass;
78 jclass JniConstants::structPollfdClass;
79 jclass JniConstants::structStatClass;
80 jclass JniConstants::structStatVfsClass;
81 jclass JniConstants::structTimevalClass;
82 jclass JniConstants::structUcredClass;
83 jclass JniConstants::structUtsnameClass;
84 jclass JniConstants::unixSocketAddressClass;
85 jclass JniConstants::zipEntryClass;
86
findClass(JNIEnv * env,const char * name)87 static jclass findClass(JNIEnv* env, const char* name) {
88 ScopedLocalRef<jclass> localClass(env, env->FindClass(name));
89 jclass result = reinterpret_cast<jclass>(env->NewGlobalRef(localClass.get()));
90 if (result == NULL) {
91 ALOGE("failed to find class '%s'", name);
92 abort();
93 }
94 return result;
95 }
96
init(JNIEnv * env)97 void JniConstants::init(JNIEnv* env) {
98 // Fast check
99 if (g_constants_initialized) {
100 // already initialized
101 return;
102 }
103
104 // Slightly slower check
105 std::lock_guard<std::mutex> guard(g_constants_mutex);
106 if (g_constants_initialized) {
107 // already initialized
108 return;
109 }
110
111 bigDecimalClass = findClass(env, "java/math/BigDecimal");
112 booleanClass = findClass(env, "java/lang/Boolean");
113 byteClass = findClass(env, "java/lang/Byte");
114 byteArrayClass = findClass(env, "[B");
115 calendarClass = findClass(env, "java/util/Calendar");
116 characterClass = findClass(env, "java/lang/Character");
117 charsetICUClass = findClass(env, "java/nio/charset/CharsetICU");
118 constructorClass = findClass(env, "java/lang/reflect/Constructor");
119 floatClass = findClass(env, "java/lang/Float");
120 deflaterClass = findClass(env, "java/util/zip/Deflater");
121 doubleClass = findClass(env, "java/lang/Double");
122 errnoExceptionClass = findClass(env, "android/system/ErrnoException");
123 fieldClass = findClass(env, "java/lang/reflect/Field");
124 fileDescriptorClass = findClass(env, "java/io/FileDescriptor");
125 gaiExceptionClass = findClass(env, "android/system/GaiException");
126 inet6AddressClass = findClass(env, "java/net/Inet6Address");
127 inetAddressClass = findClass(env, "java/net/InetAddress");
128 inetAddressHolderClass = findClass(env, "java/net/InetAddress$InetAddressHolder");
129 inetSocketAddressClass = findClass(env, "java/net/InetSocketAddress");
130 inetSocketAddressHolderClass = findClass(env, "java/net/InetSocketAddress$InetSocketAddressHolder");
131 inflaterClass = findClass(env, "java/util/zip/Inflater");
132 inputStreamClass = findClass(env, "java/io/InputStream");
133 integerClass = findClass(env, "java/lang/Integer");
134 localeDataClass = findClass(env, "libcore/icu/LocaleData");
135 longClass = findClass(env, "java/lang/Long");
136 methodClass = findClass(env, "java/lang/reflect/Method");
137 mutableIntClass = findClass(env, "android/util/MutableInt");
138 mutableLongClass = findClass(env, "android/util/MutableLong");
139 netlinkSocketAddressClass = findClass(env, "android/system/NetlinkSocketAddress");
140 objectClass = findClass(env, "java/lang/Object");
141 objectArrayClass = findClass(env, "[Ljava/lang/Object;");
142 outputStreamClass = findClass(env, "java/io/OutputStream");
143 packetSocketAddressClass = findClass(env, "android/system/PacketSocketAddress");
144 parsePositionClass = findClass(env, "java/text/ParsePosition");
145 patternSyntaxExceptionClass = findClass(env, "java/util/regex/PatternSyntaxException");
146 referenceClass = findClass(env, "java/lang/ref/Reference");
147 shortClass = findClass(env, "java/lang/Short");
148 socketClass = findClass(env, "java/net/Socket");
149 socketTaggerClass = findClass(env, "dalvik/system/SocketTagger");
150 socketImplClass = findClass(env, "java/net/SocketImpl");
151 stringClass = findClass(env, "java/lang/String");
152 structAddrinfoClass = findClass(env, "android/system/StructAddrinfo");
153 structFlockClass = findClass(env, "android/system/StructFlock");
154 structGroupReqClass = findClass(env, "android/system/StructGroupReq");
155 structGroupSourceReqClass = findClass(env, "android/system/StructGroupSourceReq");
156 structLingerClass = findClass(env, "android/system/StructLinger");
157 structPasswdClass = findClass(env, "android/system/StructPasswd");
158 structPollfdClass = findClass(env, "android/system/StructPollfd");
159 structStatClass = findClass(env, "android/system/StructStat");
160 structStatVfsClass = findClass(env, "android/system/StructStatVfs");
161 structTimevalClass = findClass(env, "android/system/StructTimeval");
162 structUcredClass = findClass(env, "android/system/StructUcred");
163 structUtsnameClass = findClass(env, "android/system/StructUtsname");
164 unixSocketAddressClass = findClass(env, "android/system/UnixSocketAddress");
165 zipEntryClass = findClass(env, "java/util/zip/ZipEntry");
166
167 g_constants_initialized = true;
168 }
169