blob: d288943528a96f1dfa7f76d65a79bb3b3ef0a157 [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
Ian Rogersc7dd2952014-10-21 23:31:19 -070021#include <sstream>
22
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/class.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "mirror/throwable.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "ScopedLocalRef.h"
Brian Carlstrom3c821c62014-09-10 10:08:37 -070027#include "scoped_thread_state_change.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080028#include "thread-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029
30namespace art {
31
32jclass WellKnownClasses::com_android_dex_Dex;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -070033jclass WellKnownClasses::dalvik_system_DexFile;
34jclass WellKnownClasses::dalvik_system_DexPathList;
Andreas Gampec8ccf682014-09-29 20:07:43 -070035jclass WellKnownClasses::dalvik_system_DexPathList__Element;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036jclass WellKnownClasses::dalvik_system_PathClassLoader;
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -070037jclass WellKnownClasses::dalvik_system_VMRuntime;
Jeff Hao13e748b2015-08-25 20:44:19 +000038jclass WellKnownClasses::java_lang_annotation_Annotation__array;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -070039jclass WellKnownClasses::java_lang_BootClassLoader;
Elliott Hugheseac76672012-05-24 21:56:51 -070040jclass WellKnownClasses::java_lang_ClassLoader;
41jclass WellKnownClasses::java_lang_ClassNotFoundException;
42jclass WellKnownClasses::java_lang_Daemons;
43jclass WellKnownClasses::java_lang_Error;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080044jclass WellKnownClasses::java_lang_Object;
Mathieu Chartiera61894d2015-04-23 16:32:54 -070045jclass WellKnownClasses::java_lang_OutOfMemoryError;
Mathieu Chartier66f19252012-09-18 08:57:04 -070046jclass WellKnownClasses::java_lang_reflect_AbstractMethod;
Brian Carlstromea46f952013-07-30 01:26:50 -070047jclass WellKnownClasses::java_lang_reflect_Constructor;
48jclass WellKnownClasses::java_lang_reflect_Field;
49jclass WellKnownClasses::java_lang_reflect_Method;
Elliott Hugheseac76672012-05-24 21:56:51 -070050jclass WellKnownClasses::java_lang_reflect_Proxy;
Elliott Hughesa4f94742012-05-29 16:28:38 -070051jclass WellKnownClasses::java_lang_RuntimeException;
Ian Rogers120f1c72012-09-28 17:17:10 -070052jclass WellKnownClasses::java_lang_StackOverflowError;
Ian Rogersdd157d72014-05-15 14:47:50 -070053jclass WellKnownClasses::java_lang_String;
Jeff Hao848f70a2014-01-15 13:49:50 -080054jclass WellKnownClasses::java_lang_StringFactory;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070055jclass WellKnownClasses::java_lang_System;
Elliott Hugheseac76672012-05-24 21:56:51 -070056jclass WellKnownClasses::java_lang_Thread;
Andreas Gampec8ccf682014-09-29 20:07:43 -070057jclass WellKnownClasses::java_lang_Thread__UncaughtExceptionHandler;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070058jclass WellKnownClasses::java_lang_ThreadGroup;
Elliott Hughesa4f94742012-05-29 16:28:38 -070059jclass WellKnownClasses::java_lang_Throwable;
Elliott Hughesaecb5f32013-03-28 08:27:38 -070060jclass WellKnownClasses::java_nio_DirectByteBuffer;
Sebastien Hertze49e1952014-10-13 11:27:13 +020061jclass WellKnownClasses::java_util_ArrayList;
Andreas Gampe7ea6f792014-07-14 16:21:44 -070062jclass WellKnownClasses::java_util_Collections;
Jeff Hao13e748b2015-08-25 20:44:19 +000063jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
64jclass WellKnownClasses::libcore_reflect_AnnotationMember;
Andreas Gampe7ea6f792014-07-14 16:21:44 -070065jclass WellKnownClasses::libcore_util_EmptyArray;
Elliott Hugheseac76672012-05-24 21:56:51 -070066jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
67jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
68
69jmethodID WellKnownClasses::com_android_dex_Dex_create;
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -070070jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
72jmethodID WellKnownClasses::java_lang_Byte_valueOf;
73jmethodID WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070074jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
Elliott Hugheseac76672012-05-24 21:56:51 -070076jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
77jmethodID WellKnownClasses::java_lang_Daemons_start;
Mathieu Chartiercef50f02014-12-09 17:38:52 -080078jmethodID WellKnownClasses::java_lang_Daemons_stop;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079jmethodID WellKnownClasses::java_lang_Double_valueOf;
80jmethodID WellKnownClasses::java_lang_Float_valueOf;
81jmethodID WellKnownClasses::java_lang_Integer_valueOf;
82jmethodID WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070083jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
84jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Brian Carlstromea46f952013-07-30 01:26:50 -070085jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
Ian Rogersef28b142012-11-30 14:22:18 -080086jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087jmethodID WellKnownClasses::java_lang_Short_valueOf;
Jeff Hao848f70a2014-01-15 13:49:50 -080088jmethodID WellKnownClasses::java_lang_String_init;
89jmethodID WellKnownClasses::java_lang_String_init_B;
90jmethodID WellKnownClasses::java_lang_String_init_BI;
91jmethodID WellKnownClasses::java_lang_String_init_BII;
92jmethodID WellKnownClasses::java_lang_String_init_BIII;
93jmethodID WellKnownClasses::java_lang_String_init_BIIString;
94jmethodID WellKnownClasses::java_lang_String_init_BString;
95jmethodID WellKnownClasses::java_lang_String_init_BIICharset;
96jmethodID WellKnownClasses::java_lang_String_init_BCharset;
97jmethodID WellKnownClasses::java_lang_String_init_C;
98jmethodID WellKnownClasses::java_lang_String_init_CII;
99jmethodID WellKnownClasses::java_lang_String_init_IIC;
100jmethodID WellKnownClasses::java_lang_String_init_String;
101jmethodID WellKnownClasses::java_lang_String_init_StringBuffer;
102jmethodID WellKnownClasses::java_lang_String_init_III;
103jmethodID WellKnownClasses::java_lang_String_init_StringBuilder;
104jmethodID WellKnownClasses::java_lang_StringFactory_newEmptyString;
105jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_B;
106jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BI;
107jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BII;
108jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BIII;
109jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BIIString;
110jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BString;
111jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BIICharset;
112jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BCharset;
113jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromChars_C;
114jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromChars_CII;
115jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromChars_IIC;
116jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromString;
117jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromStringBuffer;
118jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromCodePoints;
119jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromStringBuilder;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120jmethodID WellKnownClasses::java_lang_System_runFinalization = nullptr;
Elliott Hugheseac76672012-05-24 21:56:51 -0700121jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700122jmethodID WellKnownClasses::java_lang_Thread_run;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700123jmethodID WellKnownClasses::java_lang_Thread__UncaughtExceptionHandler_uncaughtException;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700124jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700125jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
Jeff Hao13e748b2015-08-25 20:44:19 +0000126jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
127jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
Elliott Hugheseac76672012-05-24 21:56:51 -0700128jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
129jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
130
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700131jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800132jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700133jfieldID WellKnownClasses::dalvik_system_PathClassLoader_pathList;
134jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700135jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700136jfieldID WellKnownClasses::java_lang_Thread_daemon;
137jfieldID WellKnownClasses::java_lang_Thread_group;
138jfieldID WellKnownClasses::java_lang_Thread_lock;
139jfieldID WellKnownClasses::java_lang_Thread_name;
140jfieldID WellKnownClasses::java_lang_Thread_priority;
141jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
Anwar Ghuloum3c50a4b2013-06-21 13:05:23 -0700142jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200143jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +0000144jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700145jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
146jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200147jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700148jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
Brian Carlstrom34375312014-09-10 23:10:47 -0700149jfieldID WellKnownClasses::java_lang_Throwable_cause;
150jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
151jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
152jfieldID WellKnownClasses::java_lang_Throwable_stackState;
153jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
Brian Carlstromea46f952013-07-30 01:26:50 -0700154jfieldID WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod;
Elliott Hugheseac76672012-05-24 21:56:51 -0700155jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700156jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
157jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200158jfieldID WellKnownClasses::java_util_ArrayList_array;
159jfieldID WellKnownClasses::java_util_ArrayList_size;
Brian Carlstrom34375312014-09-10 23:10:47 -0700160jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
161jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
Elliott Hugheseac76672012-05-24 21:56:51 -0700162jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
163jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
164jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
165jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
166
167static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
168 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 if (c.get() == nullptr) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700170 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
171 }
172 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
173}
174
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700175static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static,
176 const char* name, const char* signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700177 jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) :
178 env->GetFieldID(c, name, signature);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700179 if (fid == nullptr) {
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700180 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700181 if (soa.Self()->IsExceptionPending()) {
182 LOG(INTERNAL_FATAL) << soa.Self()->GetException()->Dump() << '\n';
183 }
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700184 std::ostringstream os;
185 WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
186 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\": "
187 << os.str();
Elliott Hugheseac76672012-05-24 21:56:51 -0700188 }
189 return fid;
190}
191
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700192jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static,
193 const char* name, const char* signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) :
195 env->GetMethodID(c, name, signature);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700196 if (mid == nullptr) {
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700197 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198 if (soa.Self()->IsExceptionPending()) {
199 LOG(INTERNAL_FATAL) << soa.Self()->GetException()->Dump() << '\n';
200 }
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700201 std::ostringstream os;
202 WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
203 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\": "
204 << os.str();
Elliott Hugheseac76672012-05-24 21:56:51 -0700205 }
206 return mid;
207}
208
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
210 ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
211 return CacheMethod(env, boxed_class.get(), true, "valueOf",
212 StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
213}
214
Brian Carlstromea46f952013-07-30 01:26:50 -0700215void WellKnownClasses::Init(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700216 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700217 dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
218 dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700219 dalvik_system_DexPathList__Element = CacheClass(env, "dalvik/system/DexPathList$Element");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -0700221 dalvik_system_VMRuntime = CacheClass(env, "dalvik/system/VMRuntime");
222
Jeff Hao13e748b2015-08-25 20:44:19 +0000223 java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700224 java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
Elliott Hugheseac76672012-05-24 21:56:51 -0700225 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
226 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
227 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800228 java_lang_Object = CacheClass(env, "java/lang/Object");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700229 java_lang_OutOfMemoryError = CacheClass(env, "java/lang/OutOfMemoryError");
Elliott Hugheseac76672012-05-24 21:56:51 -0700230 java_lang_Error = CacheClass(env, "java/lang/Error");
Mathieu Chartier66f19252012-09-18 08:57:04 -0700231 java_lang_reflect_AbstractMethod = CacheClass(env, "java/lang/reflect/AbstractMethod");
Brian Carlstromea46f952013-07-30 01:26:50 -0700232 java_lang_reflect_Constructor = CacheClass(env, "java/lang/reflect/Constructor");
233 java_lang_reflect_Field = CacheClass(env, "java/lang/reflect/Field");
234 java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
Elliott Hugheseac76672012-05-24 21:56:51 -0700235 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700236 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Ian Rogers120f1c72012-09-28 17:17:10 -0700237 java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
Ian Rogersdd157d72014-05-15 14:47:50 -0700238 java_lang_String = CacheClass(env, "java/lang/String");
Jeff Hao848f70a2014-01-15 13:49:50 -0800239 java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700240 java_lang_System = CacheClass(env, "java/lang/System");
Elliott Hugheseac76672012-05-24 21:56:51 -0700241 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700242 java_lang_Thread__UncaughtExceptionHandler = CacheClass(env,
243 "java/lang/Thread$UncaughtExceptionHandler");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700244 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700245 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700246 java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
Sebastien Hertze49e1952014-10-13 11:27:13 +0200247 java_util_ArrayList = CacheClass(env, "java/util/ArrayList");
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700248 java_util_Collections = CacheClass(env, "java/util/Collections");
Jeff Hao13e748b2015-08-25 20:44:19 +0000249 libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
250 libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700251 libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
Elliott Hugheseac76672012-05-24 21:56:51 -0700252 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
253 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
254
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -0700255 dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700256 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
257 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 -0700258 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700259
Elliott Hugheseac76672012-05-24 21:56:51 -0700260 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
261 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
Mathieu Chartiercef50f02014-12-09 17:38:52 -0800262 java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700263
264 ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
265 java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
266 ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
267 java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
268
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700269 java_lang_reflect_Proxy_invoke = CacheMethod(env, java_lang_reflect_Proxy, true, "invoke", "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
Elliott Hugheseac76672012-05-24 21:56:51 -0700270 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 -0700271 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700272 java_lang_Thread__UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread__UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
Piotr Jastrzebskie6cd3b52015-05-07 09:41:00 +0100273 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
Elliott Hughesb5681212013-03-29 17:29:22 -0700274 java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
Jeff Hao13e748b2015-08-25 20:44:19 +0000275 libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
276 libcore_reflect_AnnotationMember_init = CacheMethod(env, libcore_reflect_AnnotationMember, false, "<init>", "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/reflect/Method;)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700277 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
278 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;");
279
Jeff Hao848f70a2014-01-15 13:49:50 -0800280 java_lang_String_init = CacheMethod(env, java_lang_String, false, "<init>", "()V");
281 java_lang_String_init_B = CacheMethod(env, java_lang_String, false, "<init>", "([B)V");
282 java_lang_String_init_BI = CacheMethod(env, java_lang_String, false, "<init>", "([BI)V");
283 java_lang_String_init_BII = CacheMethod(env, java_lang_String, false, "<init>", "([BII)V");
284 java_lang_String_init_BIII = CacheMethod(env, java_lang_String, false, "<init>", "([BIII)V");
285 java_lang_String_init_BIIString = CacheMethod(env, java_lang_String, false, "<init>",
286 "([BIILjava/lang/String;)V");
287 java_lang_String_init_BString = CacheMethod(env, java_lang_String, false, "<init>",
288 "([BLjava/lang/String;)V");
289 java_lang_String_init_BIICharset = CacheMethod(env, java_lang_String, false, "<init>",
290 "([BIILjava/nio/charset/Charset;)V");
291 java_lang_String_init_BCharset = CacheMethod(env, java_lang_String, false, "<init>",
292 "([BLjava/nio/charset/Charset;)V");
293 java_lang_String_init_C = CacheMethod(env, java_lang_String, false, "<init>", "([C)V");
294 java_lang_String_init_CII = CacheMethod(env, java_lang_String, false, "<init>", "([CII)V");
295 java_lang_String_init_IIC = CacheMethod(env, java_lang_String, false, "<init>", "(II[C)V");
296 java_lang_String_init_String = CacheMethod(env, java_lang_String, false, "<init>",
297 "(Ljava/lang/String;)V");
298 java_lang_String_init_StringBuffer = CacheMethod(env, java_lang_String, false, "<init>",
299 "(Ljava/lang/StringBuffer;)V");
300 java_lang_String_init_III = CacheMethod(env, java_lang_String, false, "<init>", "([III)V");
301 java_lang_String_init_StringBuilder = CacheMethod(env, java_lang_String, false, "<init>",
302 "(Ljava/lang/StringBuilder;)V");
303 java_lang_StringFactory_newEmptyString = CacheMethod(env, java_lang_StringFactory, true,
304 "newEmptyString", "()Ljava/lang/String;");
305 java_lang_StringFactory_newStringFromBytes_B = CacheMethod(env, java_lang_StringFactory, true,
306 "newStringFromBytes", "([B)Ljava/lang/String;");
307 java_lang_StringFactory_newStringFromBytes_BI = CacheMethod(env, java_lang_StringFactory, true,
308 "newStringFromBytes", "([BI)Ljava/lang/String;");
309 java_lang_StringFactory_newStringFromBytes_BII = CacheMethod(env, java_lang_StringFactory, true,
310 "newStringFromBytes", "([BII)Ljava/lang/String;");
311 java_lang_StringFactory_newStringFromBytes_BIII = CacheMethod(env, java_lang_StringFactory, true,
312 "newStringFromBytes", "([BIII)Ljava/lang/String;");
313 java_lang_StringFactory_newStringFromBytes_BIIString = CacheMethod(env, java_lang_StringFactory,
314 true, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;");
315 java_lang_StringFactory_newStringFromBytes_BString = CacheMethod(env, java_lang_StringFactory,
316 true, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;");
317 java_lang_StringFactory_newStringFromBytes_BIICharset = CacheMethod(env, java_lang_StringFactory,
318 true, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;");
319 java_lang_StringFactory_newStringFromBytes_BCharset = CacheMethod(env, java_lang_StringFactory,
320 true, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;");
321 java_lang_StringFactory_newStringFromChars_C = CacheMethod(env, java_lang_StringFactory, true,
322 "newStringFromChars", "([C)Ljava/lang/String;");
323 java_lang_StringFactory_newStringFromChars_CII = CacheMethod(env, java_lang_StringFactory, true,
324 "newStringFromChars", "([CII)Ljava/lang/String;");
325 java_lang_StringFactory_newStringFromChars_IIC = CacheMethod(env, java_lang_StringFactory, true,
326 "newStringFromChars", "(II[C)Ljava/lang/String;");
327 java_lang_StringFactory_newStringFromString = CacheMethod(env, java_lang_StringFactory, true,
328 "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;");
329 java_lang_StringFactory_newStringFromStringBuffer = CacheMethod(env, java_lang_StringFactory,
330 true, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;");
331 java_lang_StringFactory_newStringFromCodePoints = CacheMethod(env, java_lang_StringFactory,
332 true, "newStringFromCodePoints", "([III)Ljava/lang/String;");
333 java_lang_StringFactory_newStringFromStringBuilder = CacheMethod(env, java_lang_StringFactory,
334 true, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;");
335
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800336 dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800337 dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700338 dalvik_system_PathClassLoader_pathList = CacheField(env, dalvik_system_PathClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
339 dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700340 dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700341 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
342 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
Elliott Hughesc5dc2ff2013-01-09 13:44:30 -0800343 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700344 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
345 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
Piotr Jastrzebskie6cd3b52015-05-07 09:41:00 +0100346 java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtExceptionHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800347 java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000348 java_lang_ThreadGroup_groups = CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +0000349 java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700350 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
351 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
Sebastien Hertze49e1952014-10-13 11:27:13 +0200352 java_lang_ThreadGroup_parent = CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700353 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
Brian Carlstrom34375312014-09-10 23:10:47 -0700354 java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
355 java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
356 java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
Piotr Jastrzebskic16a50f2015-05-07 09:41:00 +0100357 java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
Brian Carlstrom34375312014-09-10 23:10:47 -0700358 java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 java_lang_reflect_AbstractMethod_artMethod = CacheField(env, java_lang_reflect_AbstractMethod, false, "artMethod", "J");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700360 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700361 java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
Piotr Jastrzebski8c145b92015-05-05 12:54:00 +0100362 java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "address", "J");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000363 java_util_ArrayList_array = CacheField(env, java_util_ArrayList, false, "elementData", "[Ljava/lang/Object;");
Sebastien Hertze49e1952014-10-13 11:27:13 +0200364 java_util_ArrayList_size = CacheField(env, java_util_ArrayList, false, "size", "I");
Brian Carlstrom34375312014-09-10 23:10:47 -0700365 java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
366 libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
Sebastien Hertzb30e0c62014-09-11 16:23:44 +0200367 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700368 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
369 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
370 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 -0700371
372 java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
373 java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
374 java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
375 java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
376 java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
377 java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
378 java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
379 java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
Jeff Hao848f70a2014-01-15 13:49:50 -0800380
381 Thread::Current()->InitStringEntryPoints();
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700382}
383
Ian Rogersef28b142012-11-30 14:22:18 -0800384void WellKnownClasses::LateInit(JNIEnv* env) {
385 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
Dmitriy Ivanov44409d32015-11-11 14:18:55 -0800386 java_lang_Runtime_nativeLoad =
387 CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800388 "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)"
Dmitriy Ivanov44409d32015-11-11 14:18:55 -0800389 "Ljava/lang/String;");
Ian Rogersef28b142012-11-30 14:22:18 -0800390}
391
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392mirror::Class* WellKnownClasses::ToClass(jclass global_jclass) {
393 return reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(global_jclass));
Elliott Hugheseac76672012-05-24 21:56:51 -0700394}
395
Jeff Hao848f70a2014-01-15 13:49:50 -0800396jmethodID WellKnownClasses::StringInitToStringFactoryMethodID(jmethodID string_init) {
397 // TODO: Prioritize ordering.
398 if (string_init == java_lang_String_init) {
399 return java_lang_StringFactory_newEmptyString;
400 } else if (string_init == java_lang_String_init_B) {
401 return java_lang_StringFactory_newStringFromBytes_B;
402 } else if (string_init == java_lang_String_init_BI) {
403 return java_lang_StringFactory_newStringFromBytes_BI;
404 } else if (string_init == java_lang_String_init_BII) {
405 return java_lang_StringFactory_newStringFromBytes_BII;
406 } else if (string_init == java_lang_String_init_BIII) {
407 return java_lang_StringFactory_newStringFromBytes_BIII;
408 } else if (string_init == java_lang_String_init_BIIString) {
409 return java_lang_StringFactory_newStringFromBytes_BIIString;
410 } else if (string_init == java_lang_String_init_BString) {
411 return java_lang_StringFactory_newStringFromBytes_BString;
412 } else if (string_init == java_lang_String_init_BIICharset) {
413 return java_lang_StringFactory_newStringFromBytes_BIICharset;
414 } else if (string_init == java_lang_String_init_BCharset) {
415 return java_lang_StringFactory_newStringFromBytes_BCharset;
416 } else if (string_init == java_lang_String_init_C) {
417 return java_lang_StringFactory_newStringFromChars_C;
418 } else if (string_init == java_lang_String_init_CII) {
419 return java_lang_StringFactory_newStringFromChars_CII;
420 } else if (string_init == java_lang_String_init_IIC) {
421 return java_lang_StringFactory_newStringFromChars_IIC;
422 } else if (string_init == java_lang_String_init_String) {
423 return java_lang_StringFactory_newStringFromString;
424 } else if (string_init == java_lang_String_init_StringBuffer) {
425 return java_lang_StringFactory_newStringFromStringBuffer;
426 } else if (string_init == java_lang_String_init_III) {
427 return java_lang_StringFactory_newStringFromCodePoints;
428 } else if (string_init == java_lang_String_init_StringBuilder) {
429 return java_lang_StringFactory_newStringFromStringBuilder;
430 }
431 LOG(FATAL) << "Could not find StringFactory method for String.<init>";
432 return nullptr;
433}
434
Elliott Hugheseac76672012-05-24 21:56:51 -0700435} // namespace art