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 "JNIHelp-priv.h"
21#include <nativehelper/JniConstants.h>
22#include <nativehelper/JniConstants-priv.h>
23#include <nativehelper/ScopedLocalRef.h>
24
25#include <stdlib.h>
26
27#include <atomic>
28#include <mutex>
29
30static std::atomic<bool> g_constants_initialized(false);
31static std::mutex g_constants_mutex;
32
33jclass JniConstants::booleanClass;
34jclass JniConstants::byteArrayClass;
35jclass JniConstants::calendarClass;
36jclass JniConstants::charsetICUClass;
37jclass JniConstants::doubleClass;
38jclass JniConstants::errnoExceptionClass;
39jclass JniConstants::fileDescriptorClass;
40jclass JniConstants::gaiExceptionClass;
41jclass JniConstants::inet6AddressClass;
42jclass JniConstants::inet6AddressHolderClass;
43jclass JniConstants::inetAddressClass;
44jclass JniConstants::inetAddressHolderClass;
45jclass JniConstants::inetSocketAddressClass;
46jclass JniConstants::inetSocketAddressHolderClass;
47jclass JniConstants::integerClass;
48jclass JniConstants::localeDataClass;
49jclass JniConstants::longClass;
50jclass JniConstants::netlinkSocketAddressClass;
51jclass JniConstants::packetSocketAddressClass;
52jclass JniConstants::patternSyntaxExceptionClass;
53jclass JniConstants::referenceClass;
54jclass JniConstants::socketTaggerClass;
55jclass JniConstants::stringClass;
56jclass JniConstants::structAddrinfoClass;
57jclass JniConstants::structFlockClass;
58jclass JniConstants::structGroupReqClass;
59jclass JniConstants::structIfaddrs;
60jclass JniConstants::structLingerClass;
61jclass JniConstants::structPasswdClass;
62jclass JniConstants::structPollfdClass;
63jclass JniConstants::structStatClass;
64jclass JniConstants::structStatVfsClass;
65jclass JniConstants::structTimevalClass;
66jclass JniConstants::structTimespecClass;
67jclass JniConstants::structUcredClass;
68jclass JniConstants::structUtsnameClass;
69jclass JniConstants::unixSocketAddressClass;
70jclass JniConstants::zipEntryClass;
71
72static jclass findClass(JNIEnv* env, const char* name) {
73    ScopedLocalRef<jclass> localClass(env, env->FindClass(name));
74    jclass result = reinterpret_cast<jclass>(env->NewGlobalRef(localClass.get()));
75    if (result == NULL) {
76        ALOGE("failed to find class '%s'", name);
77        abort();
78    }
79    return result;
80}
81
82void JniConstants::init(JNIEnv* env) {
83    // Fast check
84    if (g_constants_initialized) {
85      // already initialized
86      return;
87    }
88
89    // Slightly slower check
90    std::lock_guard<std::mutex> guard(g_constants_mutex);
91    if (g_constants_initialized) {
92      // already initialized
93      return;
94    }
95
96    booleanClass = findClass(env, "java/lang/Boolean");
97    byteArrayClass = findClass(env, "[B");
98    calendarClass = findClass(env, "java/util/Calendar");
99    charsetICUClass = findClass(env, "java/nio/charset/CharsetICU");
100    doubleClass = findClass(env, "java/lang/Double");
101    errnoExceptionClass = findClass(env, "android/system/ErrnoException");
102    fileDescriptorClass = findClass(env, "java/io/FileDescriptor");
103    gaiExceptionClass = findClass(env, "android/system/GaiException");
104    inet6AddressClass = findClass(env, "java/net/Inet6Address");
105    inet6AddressHolderClass = findClass(env, "java/net/Inet6Address$Inet6AddressHolder");
106    inetAddressClass = findClass(env, "java/net/InetAddress");
107    inetAddressHolderClass = findClass(env, "java/net/InetAddress$InetAddressHolder");
108    inetSocketAddressClass = findClass(env, "java/net/InetSocketAddress");
109    inetSocketAddressHolderClass = findClass(env, "java/net/InetSocketAddress$InetSocketAddressHolder");
110    integerClass = findClass(env, "java/lang/Integer");
111    localeDataClass = findClass(env, "libcore/icu/LocaleData");
112    longClass = findClass(env, "java/lang/Long");
113    netlinkSocketAddressClass = findClass(env, "android/system/NetlinkSocketAddress");
114    packetSocketAddressClass = findClass(env, "android/system/PacketSocketAddress");
115    patternSyntaxExceptionClass = findClass(env, "java/util/regex/PatternSyntaxException");
116    referenceClass = findClass(env, "java/lang/ref/Reference");
117    socketTaggerClass = findClass(env, "dalvik/system/SocketTagger");
118    stringClass = findClass(env, "java/lang/String");
119    structAddrinfoClass = findClass(env, "android/system/StructAddrinfo");
120    structFlockClass = findClass(env, "android/system/StructFlock");
121    structGroupReqClass = findClass(env, "android/system/StructGroupReq");
122    structIfaddrs = findClass(env, "android/system/StructIfaddrs");
123    structLingerClass = findClass(env, "android/system/StructLinger");
124    structPasswdClass = findClass(env, "android/system/StructPasswd");
125    structPollfdClass = findClass(env, "android/system/StructPollfd");
126    structStatClass = findClass(env, "android/system/StructStat");
127    structStatVfsClass = findClass(env, "android/system/StructStatVfs");
128    structTimevalClass = findClass(env, "android/system/StructTimeval");
129    structTimespecClass = findClass(env, "android/system/StructTimespec");
130    structUcredClass = findClass(env, "android/system/StructUcred");
131    structUtsnameClass = findClass(env, "android/system/StructUtsname");
132    unixSocketAddressClass = findClass(env, "android/system/UnixSocketAddress");
133    zipEntryClass = findClass(env, "java/util/zip/ZipEntry");
134
135    g_constants_initialized = true;
136}
137
138namespace android {
139
140void ClearJniConstantsCache() {
141    g_constants_initialized = false;
142    ClearJNIHelpLocalCache();
143}
144
145}
146