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 30 static std::atomic<bool> g_constants_initialized(false); 31 static std::mutex g_constants_mutex; 32 33 jclass JniConstants::booleanClass; 34 jclass JniConstants::byteArrayClass; 35 jclass JniConstants::calendarClass; 36 jclass JniConstants::charsetICUClass; 37 jclass JniConstants::doubleClass; 38 jclass JniConstants::errnoExceptionClass; 39 jclass JniConstants::fileDescriptorClass; 40 jclass JniConstants::gaiExceptionClass; 41 jclass JniConstants::inet6AddressClass; 42 jclass JniConstants::inet6AddressHolderClass; 43 jclass JniConstants::inetAddressClass; 44 jclass JniConstants::inetAddressHolderClass; 45 jclass JniConstants::inetSocketAddressClass; 46 jclass JniConstants::inetSocketAddressHolderClass; 47 jclass JniConstants::integerClass; 48 jclass JniConstants::localeDataClass; 49 jclass JniConstants::longClass; 50 jclass JniConstants::netlinkSocketAddressClass; 51 jclass JniConstants::packetSocketAddressClass; 52 jclass JniConstants::patternSyntaxExceptionClass; 53 jclass JniConstants::referenceClass; 54 jclass JniConstants::socketTaggerClass; 55 jclass JniConstants::stringClass; 56 jclass JniConstants::structAddrinfoClass; 57 jclass JniConstants::structFlockClass; 58 jclass JniConstants::structGroupReqClass; 59 jclass JniConstants::structIfaddrs; 60 jclass JniConstants::structLingerClass; 61 jclass JniConstants::structPasswdClass; 62 jclass JniConstants::structPollfdClass; 63 jclass JniConstants::structStatClass; 64 jclass JniConstants::structStatVfsClass; 65 jclass JniConstants::structTimevalClass; 66 jclass JniConstants::structTimespecClass; 67 jclass JniConstants::structUcredClass; 68 jclass JniConstants::structUtsnameClass; 69 jclass JniConstants::unixSocketAddressClass; 70 jclass JniConstants::zipEntryClass; 71 72 static 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 82 void 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 138 namespace android { 139 140 void ClearJniConstantsCache() { 141 g_constants_initialized = false; 142 ClearJNIHelpLocalCache(); 143 } 144 145 } 146