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 28static std::atomic<bool> g_constants_initialized(false); 29static std::mutex g_constants_mutex; 30 31jclass JniConstants::booleanClass; 32jclass JniConstants::byteArrayClass; 33jclass JniConstants::calendarClass; 34jclass JniConstants::charsetICUClass; 35jclass JniConstants::doubleClass; 36jclass JniConstants::errnoExceptionClass; 37jclass JniConstants::fileDescriptorClass; 38jclass JniConstants::gaiExceptionClass; 39jclass JniConstants::inet6AddressClass; 40jclass JniConstants::inet6AddressHolderClass; 41jclass JniConstants::inetAddressClass; 42jclass JniConstants::inetAddressHolderClass; 43jclass JniConstants::inetSocketAddressClass; 44jclass JniConstants::inetSocketAddressHolderClass; 45jclass JniConstants::integerClass; 46jclass JniConstants::localeDataClass; 47jclass JniConstants::longClass; 48jclass JniConstants::mutableIntClass; 49jclass JniConstants::mutableLongClass; 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::structGroupSourceReqClass; 60jclass JniConstants::structIfaddrs; 61jclass JniConstants::structLingerClass; 62jclass JniConstants::structPasswdClass; 63jclass JniConstants::structPollfdClass; 64jclass JniConstants::structStatClass; 65jclass JniConstants::structStatVfsClass; 66jclass JniConstants::structTimevalClass; 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 mutableIntClass = findClass(env, "android/util/MutableInt"); 114 mutableLongClass = findClass(env, "android/util/MutableLong"); 115 netlinkSocketAddressClass = findClass(env, "android/system/NetlinkSocketAddress"); 116 packetSocketAddressClass = findClass(env, "android/system/PacketSocketAddress"); 117 patternSyntaxExceptionClass = findClass(env, "java/util/regex/PatternSyntaxException"); 118 referenceClass = findClass(env, "java/lang/ref/Reference"); 119 socketTaggerClass = findClass(env, "dalvik/system/SocketTagger"); 120 stringClass = findClass(env, "java/lang/String"); 121 structAddrinfoClass = findClass(env, "android/system/StructAddrinfo"); 122 structFlockClass = findClass(env, "android/system/StructFlock"); 123 structGroupReqClass = findClass(env, "android/system/StructGroupReq"); 124 structGroupSourceReqClass = findClass(env, "android/system/StructGroupSourceReq"); 125 structIfaddrs = findClass(env, "android/system/StructIfaddrs"); 126 structLingerClass = findClass(env, "android/system/StructLinger"); 127 structPasswdClass = findClass(env, "android/system/StructPasswd"); 128 structPollfdClass = findClass(env, "android/system/StructPollfd"); 129 structStatClass = findClass(env, "android/system/StructStat"); 130 structStatVfsClass = findClass(env, "android/system/StructStatVfs"); 131 structTimevalClass = findClass(env, "android/system/StructTimeval"); 132 structUcredClass = findClass(env, "android/system/StructUcred"); 133 structUtsnameClass = findClass(env, "android/system/StructUtsname"); 134 unixSocketAddressClass = findClass(env, "android/system/UnixSocketAddress"); 135 zipEntryClass = findClass(env, "java/util/zip/ZipEntry"); 136 137 g_constants_initialized = true; 138} 139