1 /* 2 * Copyright (C) 2012 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 #include "well_known_classes.h" 18 19 #include <stdlib.h> 20 21 #include "base/logging.h" 22 #include "mirror/class.h" 23 #include "ScopedLocalRef.h" 24 #include "thread.h" 25 26 namespace art { 27 28 jclass WellKnownClasses::com_android_dex_Dex; 29 jclass WellKnownClasses::dalvik_system_PathClassLoader; 30 jclass WellKnownClasses::java_lang_ClassLoader; 31 jclass WellKnownClasses::java_lang_ClassNotFoundException; 32 jclass WellKnownClasses::java_lang_Daemons; 33 jclass WellKnownClasses::java_lang_Error; 34 jclass WellKnownClasses::java_lang_Object; 35 jclass WellKnownClasses::java_lang_reflect_AbstractMethod; 36 jclass WellKnownClasses::java_lang_reflect_ArtMethod; 37 jclass WellKnownClasses::java_lang_reflect_Constructor; 38 jclass WellKnownClasses::java_lang_reflect_Field; 39 jclass WellKnownClasses::java_lang_reflect_Method; 40 jclass WellKnownClasses::java_lang_reflect_Proxy; 41 jclass WellKnownClasses::java_lang_RuntimeException; 42 jclass WellKnownClasses::java_lang_StackOverflowError; 43 jclass WellKnownClasses::java_lang_System; 44 jclass WellKnownClasses::java_lang_Thread; 45 jclass WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler; 46 jclass WellKnownClasses::java_lang_ThreadGroup; 47 jclass WellKnownClasses::java_lang_Throwable; 48 jclass WellKnownClasses::java_nio_DirectByteBuffer; 49 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk; 50 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer; 51 52 jmethodID WellKnownClasses::com_android_dex_Dex_create; 53 jmethodID WellKnownClasses::java_lang_Boolean_valueOf; 54 jmethodID WellKnownClasses::java_lang_Byte_valueOf; 55 jmethodID WellKnownClasses::java_lang_Character_valueOf; 56 jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass; 57 jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init; 58 jmethodID WellKnownClasses::java_lang_Daemons_requestGC; 59 jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim; 60 jmethodID WellKnownClasses::java_lang_Daemons_start; 61 jmethodID WellKnownClasses::java_lang_Double_valueOf; 62 jmethodID WellKnownClasses::java_lang_Float_valueOf; 63 jmethodID WellKnownClasses::java_lang_Integer_valueOf; 64 jmethodID WellKnownClasses::java_lang_Long_valueOf; 65 jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add; 66 jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add; 67 jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke; 68 jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad; 69 jmethodID WellKnownClasses::java_lang_Short_valueOf; 70 jmethodID WellKnownClasses::java_lang_System_runFinalization = NULL; 71 jmethodID WellKnownClasses::java_lang_Thread_init; 72 jmethodID WellKnownClasses::java_lang_Thread_run; 73 jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException; 74 jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread; 75 jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init; 76 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast; 77 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch; 78 79 jfieldID WellKnownClasses::java_lang_Thread_daemon; 80 jfieldID WellKnownClasses::java_lang_Thread_group; 81 jfieldID WellKnownClasses::java_lang_Thread_lock; 82 jfieldID WellKnownClasses::java_lang_Thread_name; 83 jfieldID WellKnownClasses::java_lang_Thread_priority; 84 jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler; 85 jfieldID WellKnownClasses::java_lang_Thread_nativePeer; 86 jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup; 87 jfieldID WellKnownClasses::java_lang_ThreadGroup_name; 88 jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup; 89 jfieldID WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod; 90 jfieldID WellKnownClasses::java_lang_reflect_Field_artField; 91 jfieldID WellKnownClasses::java_lang_reflect_Proxy_h; 92 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity; 93 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress; 94 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data; 95 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length; 96 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset; 97 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type; 98 99 static jclass CacheClass(JNIEnv* env, const char* jni_class_name) { 100 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name)); 101 if (c.get() == NULL) { 102 LOG(FATAL) << "Couldn't find class: " << jni_class_name; 103 } 104 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get())); 105 } 106 107 static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) { 108 jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) : env->GetFieldID(c, name, signature); 109 if (fid == NULL) { 110 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\""; 111 } 112 return fid; 113 } 114 115 jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) { 116 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) : env->GetMethodID(c, name, signature); 117 if (mid == NULL) { 118 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\""; 119 } 120 return mid; 121 } 122 123 static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) { 124 ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name)); 125 return CacheMethod(env, boxed_class.get(), true, "valueOf", 126 StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str()); 127 } 128 129 void WellKnownClasses::Init(JNIEnv* env) { 130 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex"); 131 dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader"); 132 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader"); 133 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException"); 134 java_lang_Daemons = CacheClass(env, "java/lang/Daemons"); 135 java_lang_Object = CacheClass(env, "java/lang/Object"); 136 java_lang_Error = CacheClass(env, "java/lang/Error"); 137 java_lang_reflect_AbstractMethod = CacheClass(env, "java/lang/reflect/AbstractMethod"); 138 java_lang_reflect_ArtMethod = CacheClass(env, "java/lang/reflect/ArtMethod"); 139 java_lang_reflect_Constructor = CacheClass(env, "java/lang/reflect/Constructor"); 140 java_lang_reflect_Field = CacheClass(env, "java/lang/reflect/Field"); 141 java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method"); 142 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy"); 143 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException"); 144 java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError"); 145 java_lang_System = CacheClass(env, "java/lang/System"); 146 java_lang_Thread = CacheClass(env, "java/lang/Thread"); 147 java_lang_Thread$UncaughtExceptionHandler = CacheClass(env, "java/lang/Thread$UncaughtExceptionHandler"); 148 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup"); 149 java_lang_Throwable = CacheClass(env, "java/lang/Throwable"); 150 java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer"); 151 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk"); 152 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer"); 153 154 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;"); 155 java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); 156 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); 157 158 java_lang_Daemons_requestGC = CacheMethod(env, java_lang_Daemons, true, "requestGC", "()V"); 159 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V"); 160 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V"); 161 162 ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference")); 163 java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V"); 164 ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue")); 165 java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V"); 166 167 java_lang_reflect_Proxy_invoke = CacheMethod(env, java_lang_reflect_Proxy, true, "invoke", "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/ArtMethod;[Ljava/lang/Object;)Ljava/lang/Object;"); 168 java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 169 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V"); 170 java_lang_Thread$UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread$UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 171 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "removeThread", "(Ljava/lang/Thread;)V"); 172 java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V"); 173 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V"); 174 org_apache_harmony_dalvik_ddmc_DdmServer_dispatch = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "dispatch", "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;"); 175 176 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z"); 177 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;"); 178 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;"); 179 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;"); 180 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I"); 181 java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;"); 182 java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "I"); 183 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;"); 184 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;"); 185 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;"); 186 java_lang_reflect_AbstractMethod_artMethod = CacheField(env, java_lang_reflect_AbstractMethod, false, "artMethod", "Ljava/lang/reflect/ArtMethod;"); 187 java_lang_reflect_Field_artField = CacheField(env, java_lang_reflect_Field, false, "artField", "Ljava/lang/reflect/ArtField;"); 188 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;"); 189 java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I"); 190 java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "effectiveDirectAddress", "J"); 191 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B"); 192 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I"); 193 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I"); 194 org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I"); 195 196 java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean"); 197 java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte"); 198 java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character"); 199 java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double"); 200 java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float"); 201 java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer"); 202 java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long"); 203 java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short"); 204 } 205 206 void WellKnownClasses::LateInit(JNIEnv* env) { 207 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime")); 208 java_lang_Runtime_nativeLoad = CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;"); 209 } 210 211 mirror::Class* WellKnownClasses::ToClass(jclass global_jclass) { 212 return reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(global_jclass)); 213 } 214 215 } // namespace art 216