blob: e3946f72092e8ee2db97925d5f58d349b0a982e1 [file] [log] [blame]
Elliott Hugheseac76672012-05-24 21:56:51 -07001/*
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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070023#include "ScopedLocalRef.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080024#include "thread-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070025
26namespace art {
27
28jclass WellKnownClasses::com_android_dex_Dex;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029jclass WellKnownClasses::dalvik_system_PathClassLoader;
Elliott Hugheseac76672012-05-24 21:56:51 -070030jclass WellKnownClasses::java_lang_ClassLoader;
31jclass WellKnownClasses::java_lang_ClassNotFoundException;
32jclass WellKnownClasses::java_lang_Daemons;
33jclass WellKnownClasses::java_lang_Error;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080034jclass WellKnownClasses::java_lang_Object;
Mathieu Chartier66f19252012-09-18 08:57:04 -070035jclass WellKnownClasses::java_lang_reflect_AbstractMethod;
Brian Carlstromea46f952013-07-30 01:26:50 -070036jclass WellKnownClasses::java_lang_reflect_ArtMethod;
37jclass WellKnownClasses::java_lang_reflect_Constructor;
38jclass WellKnownClasses::java_lang_reflect_Field;
39jclass WellKnownClasses::java_lang_reflect_Method;
Elliott Hugheseac76672012-05-24 21:56:51 -070040jclass WellKnownClasses::java_lang_reflect_Proxy;
Elliott Hughesa4f94742012-05-29 16:28:38 -070041jclass WellKnownClasses::java_lang_RuntimeException;
Ian Rogers120f1c72012-09-28 17:17:10 -070042jclass WellKnownClasses::java_lang_StackOverflowError;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070043jclass WellKnownClasses::java_lang_System;
Elliott Hugheseac76672012-05-24 21:56:51 -070044jclass WellKnownClasses::java_lang_Thread;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070045jclass WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler;
46jclass WellKnownClasses::java_lang_ThreadGroup;
Elliott Hughesa4f94742012-05-29 16:28:38 -070047jclass WellKnownClasses::java_lang_Throwable;
Elliott Hughesaecb5f32013-03-28 08:27:38 -070048jclass WellKnownClasses::java_nio_DirectByteBuffer;
Elliott Hugheseac76672012-05-24 21:56:51 -070049jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
50jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
51
52jmethodID WellKnownClasses::com_android_dex_Dex_create;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
54jmethodID WellKnownClasses::java_lang_Byte_valueOf;
55jmethodID WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070056jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070058jmethodID WellKnownClasses::java_lang_Daemons_requestGC;
Elliott Hugheseac76672012-05-24 21:56:51 -070059jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
60jmethodID WellKnownClasses::java_lang_Daemons_start;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061jmethodID WellKnownClasses::java_lang_Double_valueOf;
62jmethodID WellKnownClasses::java_lang_Float_valueOf;
63jmethodID WellKnownClasses::java_lang_Integer_valueOf;
64jmethodID WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070065jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
66jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Brian Carlstromea46f952013-07-30 01:26:50 -070067jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
Ian Rogersef28b142012-11-30 14:22:18 -080068jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069jmethodID WellKnownClasses::java_lang_Short_valueOf;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070070jmethodID WellKnownClasses::java_lang_System_runFinalization = NULL;
Elliott Hugheseac76672012-05-24 21:56:51 -070071jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070072jmethodID WellKnownClasses::java_lang_Thread_run;
73jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException;
74jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Elliott Hughesaecb5f32013-03-28 08:27:38 -070075jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
Elliott Hugheseac76672012-05-24 21:56:51 -070076jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
77jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
78
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070079jfieldID WellKnownClasses::java_lang_Thread_daemon;
80jfieldID WellKnownClasses::java_lang_Thread_group;
81jfieldID WellKnownClasses::java_lang_Thread_lock;
82jfieldID WellKnownClasses::java_lang_Thread_name;
83jfieldID WellKnownClasses::java_lang_Thread_priority;
84jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
Anwar Ghuloum3c50a4b2013-06-21 13:05:23 -070085jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070086jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
87jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
88jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
Brian Carlstromea46f952013-07-30 01:26:50 -070089jfieldID WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod;
90jfieldID WellKnownClasses::java_lang_reflect_Field_artField;
Elliott Hugheseac76672012-05-24 21:56:51 -070091jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
Elliott Hughesaecb5f32013-03-28 08:27:38 -070092jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
93jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
Elliott Hugheseac76672012-05-24 21:56:51 -070094jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
95jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
96jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
97jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
98
99static 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
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700107static 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);
Elliott Hugheseac76672012-05-24 21:56:51 -0700109 if (fid == NULL) {
110 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\"";
111 }
112 return fid;
113}
114
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700115jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700116 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123static 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700129void WellKnownClasses::Init(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700130 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
Elliott Hugheseac76672012-05-24 21:56:51 -0700132 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");
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800135 java_lang_Object = CacheClass(env, "java/lang/Object");
Elliott Hugheseac76672012-05-24 21:56:51 -0700136 java_lang_Error = CacheClass(env, "java/lang/Error");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700137 java_lang_reflect_AbstractMethod = CacheClass(env, "java/lang/reflect/AbstractMethod");
Brian Carlstromea46f952013-07-30 01:26:50 -0700138 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");
Elliott Hugheseac76672012-05-24 21:56:51 -0700142 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700143 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Ian Rogers120f1c72012-09-28 17:17:10 -0700144 java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700145 java_lang_System = CacheClass(env, "java/lang/System");
Elliott Hugheseac76672012-05-24 21:56:51 -0700146 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700147 java_lang_Thread$UncaughtExceptionHandler = CacheClass(env, "java/lang/Thread$UncaughtExceptionHandler");
148 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700149 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700150 java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
Elliott Hugheseac76672012-05-24 21:56:51 -0700151 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");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700156 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700157
158 java_lang_Daemons_requestGC = CacheMethod(env, java_lang_Daemons, true, "requestGC", "()V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700159 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");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700161
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
Brian Carlstromea46f952013-07-30 01:26:50 -0700167 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;");
Elliott Hugheseac76672012-05-24 21:56:51 -0700168 java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700169 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");
Elliott Hughesb5681212013-03-29 17:29:22 -0700172 java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700173 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
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700176 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;");
Elliott Hughesc5dc2ff2013-01-09 13:44:30 -0800178 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700179 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;");
Anwar Ghuloum3c50a4b2013-06-21 13:05:23 -0700182 java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "I");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700183 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;");
Brian Carlstromea46f952013-07-30 01:26:50 -0700186 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;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700188 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700189 java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
Elliott Hughes2e8a4fc2013-03-29 16:33:52 -0700190 java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "effectiveDirectAddress", "J");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700191 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");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195
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");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700204}
205
Ian Rogersef28b142012-11-30 14:22:18 -0800206void WellKnownClasses::LateInit(JNIEnv* env) {
207 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
Elliott Hughesc8fece32013-01-02 11:27:23 -0800208 java_lang_Runtime_nativeLoad = CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;");
Ian Rogersef28b142012-11-30 14:22:18 -0800209}
210
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211mirror::Class* WellKnownClasses::ToClass(jclass global_jclass) {
212 return reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(global_jclass));
Elliott Hugheseac76672012-05-24 21:56:51 -0700213}
214
215} // namespace art