blob: 80edb5245f58a2b57c0bea9e9472effa404da380 [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
21#include "logging.h"
22#include "ScopedLocalRef.h"
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070023#include "thread.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070024
25namespace art {
26
27jclass WellKnownClasses::com_android_dex_Dex;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028jclass WellKnownClasses::dalvik_system_PathClassLoader;
Elliott Hugheseac76672012-05-24 21:56:51 -070029jclass WellKnownClasses::java_lang_ClassLoader;
30jclass WellKnownClasses::java_lang_ClassNotFoundException;
31jclass WellKnownClasses::java_lang_Daemons;
32jclass WellKnownClasses::java_lang_Error;
Elliott Hugheseac76672012-05-24 21:56:51 -070033jclass WellKnownClasses::java_lang_reflect_InvocationHandler;
Mathieu Chartier66f19252012-09-18 08:57:04 -070034jclass WellKnownClasses::java_lang_reflect_AbstractMethod;
Elliott Hugheseac76672012-05-24 21:56:51 -070035jclass WellKnownClasses::java_lang_reflect_Proxy;
Elliott Hughesa4f94742012-05-29 16:28:38 -070036jclass WellKnownClasses::java_lang_RuntimeException;
Ian Rogers120f1c72012-09-28 17:17:10 -070037jclass WellKnownClasses::java_lang_StackOverflowError;
Elliott Hugheseac76672012-05-24 21:56:51 -070038jclass WellKnownClasses::java_lang_Thread;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070039jclass WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler;
40jclass WellKnownClasses::java_lang_ThreadGroup;
41jclass WellKnownClasses::java_lang_ThreadLock;
Elliott Hughesa4f94742012-05-29 16:28:38 -070042jclass WellKnownClasses::java_lang_Throwable;
Elliott Hugheseac76672012-05-24 21:56:51 -070043jclass WellKnownClasses::java_nio_ReadWriteDirectByteBuffer;
44jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
45jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
46
47jmethodID WellKnownClasses::com_android_dex_Dex_create;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
49jmethodID WellKnownClasses::java_lang_Byte_valueOf;
50jmethodID WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070051jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070053jmethodID WellKnownClasses::java_lang_Daemons_requestGC;
Elliott Hugheseac76672012-05-24 21:56:51 -070054jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
55jmethodID WellKnownClasses::java_lang_Daemons_start;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056jmethodID WellKnownClasses::java_lang_Double_valueOf;
57jmethodID WellKnownClasses::java_lang_Float_valueOf;
58jmethodID WellKnownClasses::java_lang_Integer_valueOf;
59jmethodID WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070060jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
61jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Elliott Hugheseac76672012-05-24 21:56:51 -070062jmethodID WellKnownClasses::java_lang_reflect_InvocationHandler_invoke;
Ian Rogersef28b142012-11-30 14:22:18 -080063jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064jmethodID WellKnownClasses::java_lang_Short_valueOf;
Elliott Hugheseac76672012-05-24 21:56:51 -070065jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070066jmethodID WellKnownClasses::java_lang_Thread_run;
67jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException;
68jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Elliott Hugheseac76672012-05-24 21:56:51 -070069jmethodID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init;
70jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
71jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
72
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070073jfieldID WellKnownClasses::java_lang_Thread_daemon;
74jfieldID WellKnownClasses::java_lang_Thread_group;
75jfieldID WellKnownClasses::java_lang_Thread_lock;
76jfieldID WellKnownClasses::java_lang_Thread_name;
77jfieldID WellKnownClasses::java_lang_Thread_priority;
78jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
79jfieldID WellKnownClasses::java_lang_Thread_vmData;
80jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
81jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
82jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
83jfieldID WellKnownClasses::java_lang_ThreadLock_thread;
Elliott Hugheseac76672012-05-24 21:56:51 -070084jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
85jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity;
86jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress;
87jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
88jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
89jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
90jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
91
92static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
93 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
94 if (c.get() == NULL) {
95 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
96 }
97 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
98}
99
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700100static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
101 jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) : env->GetFieldID(c, name, signature);
Elliott Hugheseac76672012-05-24 21:56:51 -0700102 if (fid == NULL) {
103 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\"";
104 }
105 return fid;
106}
107
108static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
109 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) : env->GetMethodID(c, name, signature);
110 if (mid == NULL) {
111 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\"";
112 }
113 return mid;
114}
115
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
117 ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
118 return CacheMethod(env, boxed_class.get(), true, "valueOf",
119 StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
120}
121
122void WellKnownClasses::InitClasses(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700123 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
Elliott Hugheseac76672012-05-24 21:56:51 -0700125 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
126 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
127 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
128 java_lang_Error = CacheClass(env, "java/lang/Error");
Elliott Hugheseac76672012-05-24 21:56:51 -0700129 java_lang_reflect_InvocationHandler = CacheClass(env, "java/lang/reflect/InvocationHandler");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700130 java_lang_reflect_AbstractMethod = CacheClass(env, "java/lang/reflect/AbstractMethod");
Elliott Hugheseac76672012-05-24 21:56:51 -0700131 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700132 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Ian Rogers120f1c72012-09-28 17:17:10 -0700133 java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
Elliott Hugheseac76672012-05-24 21:56:51 -0700134 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700135 java_lang_Thread$UncaughtExceptionHandler = CacheClass(env, "java/lang/Thread$UncaughtExceptionHandler");
136 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
137 java_lang_ThreadLock = CacheClass(env, "java/lang/ThreadLock");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700138 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Elliott Hugheseac76672012-05-24 21:56:51 -0700139 java_nio_ReadWriteDirectByteBuffer = CacheClass(env, "java/nio/ReadWriteDirectByteBuffer");
140 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
141 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142}
143
144void WellKnownClasses::Init(JNIEnv* env) {
145 InitClasses(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700146
147 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
148 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 -0700149 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700150
151 java_lang_Daemons_requestGC = CacheMethod(env, java_lang_Daemons, true, "requestGC", "()V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700152 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
153 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700154
155 ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
156 java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
157 ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
158 java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
159
Elliott Hugheseac76672012-05-24 21:56:51 -0700160 java_lang_reflect_InvocationHandler_invoke = CacheMethod(env, java_lang_reflect_InvocationHandler, false, "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
161 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 -0700162 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
163 java_lang_Thread$UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread$UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
164 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "removeThread", "(Ljava/lang/Thread;)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700165 java_nio_ReadWriteDirectByteBuffer_init = CacheMethod(env, java_nio_ReadWriteDirectByteBuffer, false, "<init>", "(II)V");
166 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
167 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;");
168
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700169 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
170 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
171 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/ThreadLock;");
172 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
173 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
174 java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
175 java_lang_Thread_vmData = CacheField(env, java_lang_Thread, false, "vmData", "I");
176 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
177 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
178 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
179 java_lang_ThreadLock_thread = CacheField(env, java_lang_ThreadLock, false, "thread", "Ljava/lang/Thread;");
180 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
181 java_nio_ReadWriteDirectByteBuffer_capacity = CacheField(env, java_nio_ReadWriteDirectByteBuffer, false, "capacity", "I");
182 java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_ReadWriteDirectByteBuffer, false, "effectiveDirectAddress", "I");
183 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
184 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
185 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
186 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 -0700187
188 java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
189 java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
190 java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
191 java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
192 java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
193 java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
194 java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
195 java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700196}
197
Ian Rogersef28b142012-11-30 14:22:18 -0800198void WellKnownClasses::LateInit(JNIEnv* env) {
199 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
200 java_lang_Runtime_nativeLoad = CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;");
201}
202
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700203Class* WellKnownClasses::ToClass(jclass global_jclass) {
204 return reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(global_jclass));
Elliott Hugheseac76672012-05-24 21:56:51 -0700205}
206
207} // namespace art