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 <sstream>
22 
23 #include "base/logging.h"
24 #include "mirror/class.h"
25 #include "mirror/throwable.h"
26 #include "ScopedLocalRef.h"
27 #include "scoped_thread_state_change.h"
28 #include "thread-inl.h"
29 
30 namespace art {
31 
32 jclass WellKnownClasses::com_android_dex_Dex;
33 jclass WellKnownClasses::dalvik_system_DexFile;
34 jclass WellKnownClasses::dalvik_system_DexPathList;
35 jclass WellKnownClasses::dalvik_system_DexPathList__Element;
36 jclass WellKnownClasses::dalvik_system_PathClassLoader;
37 jclass WellKnownClasses::dalvik_system_VMRuntime;
38 jclass WellKnownClasses::java_lang_annotation_Annotation__array;
39 jclass WellKnownClasses::java_lang_BootClassLoader;
40 jclass WellKnownClasses::java_lang_ClassLoader;
41 jclass WellKnownClasses::java_lang_ClassNotFoundException;
42 jclass WellKnownClasses::java_lang_Daemons;
43 jclass WellKnownClasses::java_lang_Error;
44 jclass WellKnownClasses::java_lang_ExceptionInInitializerError;
45 jclass WellKnownClasses::java_lang_IllegalAccessError;
46 jclass WellKnownClasses::java_lang_NoClassDefFoundError;
47 jclass WellKnownClasses::java_lang_Object;
48 jclass WellKnownClasses::java_lang_OutOfMemoryError;
49 jclass WellKnownClasses::java_lang_reflect_AbstractMethod;
50 jclass WellKnownClasses::java_lang_reflect_Constructor;
51 jclass WellKnownClasses::java_lang_reflect_Field;
52 jclass WellKnownClasses::java_lang_reflect_Method;
53 jclass WellKnownClasses::java_lang_reflect_Proxy;
54 jclass WellKnownClasses::java_lang_RuntimeException;
55 jclass WellKnownClasses::java_lang_StackOverflowError;
56 jclass WellKnownClasses::java_lang_String;
57 jclass WellKnownClasses::java_lang_StringFactory;
58 jclass WellKnownClasses::java_lang_System;
59 jclass WellKnownClasses::java_lang_Thread;
60 jclass WellKnownClasses::java_lang_Thread__UncaughtExceptionHandler;
61 jclass WellKnownClasses::java_lang_ThreadGroup;
62 jclass WellKnownClasses::java_lang_Throwable;
63 jclass WellKnownClasses::java_nio_DirectByteBuffer;
64 jclass WellKnownClasses::java_util_ArrayList;
65 jclass WellKnownClasses::java_util_Collections;
66 jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
67 jclass WellKnownClasses::libcore_reflect_AnnotationMember;
68 jclass WellKnownClasses::libcore_util_EmptyArray;
69 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
70 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
71 
72 jmethodID WellKnownClasses::com_android_dex_Dex_create;
73 jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
74 jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
75 jmethodID WellKnownClasses::java_lang_Byte_valueOf;
76 jmethodID WellKnownClasses::java_lang_Character_valueOf;
77 jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
78 jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
79 jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
80 jmethodID WellKnownClasses::java_lang_Daemons_start;
81 jmethodID WellKnownClasses::java_lang_Daemons_stop;
82 jmethodID WellKnownClasses::java_lang_Double_valueOf;
83 jmethodID WellKnownClasses::java_lang_Float_valueOf;
84 jmethodID WellKnownClasses::java_lang_Integer_valueOf;
85 jmethodID WellKnownClasses::java_lang_Long_valueOf;
86 jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
87 jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
88 jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
89 jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
90 jmethodID WellKnownClasses::java_lang_Short_valueOf;
91 jmethodID WellKnownClasses::java_lang_String_init;
92 jmethodID WellKnownClasses::java_lang_String_init_B;
93 jmethodID WellKnownClasses::java_lang_String_init_BI;
94 jmethodID WellKnownClasses::java_lang_String_init_BII;
95 jmethodID WellKnownClasses::java_lang_String_init_BIII;
96 jmethodID WellKnownClasses::java_lang_String_init_BIIString;
97 jmethodID WellKnownClasses::java_lang_String_init_BString;
98 jmethodID WellKnownClasses::java_lang_String_init_BIICharset;
99 jmethodID WellKnownClasses::java_lang_String_init_BCharset;
100 jmethodID WellKnownClasses::java_lang_String_init_C;
101 jmethodID WellKnownClasses::java_lang_String_init_CII;
102 jmethodID WellKnownClasses::java_lang_String_init_IIC;
103 jmethodID WellKnownClasses::java_lang_String_init_String;
104 jmethodID WellKnownClasses::java_lang_String_init_StringBuffer;
105 jmethodID WellKnownClasses::java_lang_String_init_III;
106 jmethodID WellKnownClasses::java_lang_String_init_StringBuilder;
107 jmethodID WellKnownClasses::java_lang_StringFactory_newEmptyString;
108 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_B;
109 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BI;
110 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BII;
111 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BIII;
112 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BIIString;
113 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BString;
114 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BIICharset;
115 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromBytes_BCharset;
116 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromChars_C;
117 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromChars_CII;
118 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromChars_IIC;
119 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromString;
120 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromStringBuffer;
121 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromCodePoints;
122 jmethodID WellKnownClasses::java_lang_StringFactory_newStringFromStringBuilder;
123 jmethodID WellKnownClasses::java_lang_System_runFinalization = nullptr;
124 jmethodID WellKnownClasses::java_lang_Thread_init;
125 jmethodID WellKnownClasses::java_lang_Thread_run;
126 jmethodID WellKnownClasses::java_lang_Thread__UncaughtExceptionHandler_uncaughtException;
127 jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
128 jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
129 jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
130 jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
131 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
132 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
133 
134 jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
135 jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
136 jfieldID WellKnownClasses::dalvik_system_PathClassLoader_pathList;
137 jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
138 jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
139 jfieldID WellKnownClasses::java_lang_Thread_daemon;
140 jfieldID WellKnownClasses::java_lang_Thread_group;
141 jfieldID WellKnownClasses::java_lang_Thread_lock;
142 jfieldID WellKnownClasses::java_lang_Thread_name;
143 jfieldID WellKnownClasses::java_lang_Thread_priority;
144 jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
145 jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
146 jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
147 jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
148 jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
149 jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
150 jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
151 jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
152 jfieldID WellKnownClasses::java_lang_Throwable_cause;
153 jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
154 jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
155 jfieldID WellKnownClasses::java_lang_Throwable_stackState;
156 jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
157 jfieldID WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod;
158 jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
159 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
160 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
161 jfieldID WellKnownClasses::java_util_ArrayList_array;
162 jfieldID WellKnownClasses::java_util_ArrayList_size;
163 jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
164 jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
165 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
166 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
167 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
168 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
169 
CacheClass(JNIEnv * env,const char * jni_class_name)170 static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
171   ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
172   if (c.get() == nullptr) {
173     LOG(FATAL) << "Couldn't find class: " << jni_class_name;
174   }
175   return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
176 }
177 
CacheField(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)178 static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static,
179                            const char* name, const char* signature) {
180   jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) :
181       env->GetFieldID(c, name, signature);
182   if (fid == nullptr) {
183     ScopedObjectAccess soa(env);
184     if (soa.Self()->IsExceptionPending()) {
185       LOG(INTERNAL_FATAL) << soa.Self()->GetException()->Dump() << '\n';
186     }
187     std::ostringstream os;
188     WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
189     LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\": "
190                << os.str();
191   }
192   return fid;
193 }
194 
CacheMethod(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)195 jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static,
196                       const char* name, const char* signature) {
197   jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) :
198       env->GetMethodID(c, name, signature);
199   if (mid == nullptr) {
200     ScopedObjectAccess soa(env);
201     if (soa.Self()->IsExceptionPending()) {
202       LOG(INTERNAL_FATAL) << soa.Self()->GetException()->Dump() << '\n';
203     }
204     std::ostringstream os;
205     WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
206     LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\": "
207                << os.str();
208   }
209   return mid;
210 }
211 
CachePrimitiveBoxingMethod(JNIEnv * env,char prim_name,const char * boxed_name)212 static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
213   ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
214   return CacheMethod(env, boxed_class.get(), true, "valueOf",
215                      StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
216 }
217 
Init(JNIEnv * env)218 void WellKnownClasses::Init(JNIEnv* env) {
219   com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
220   dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
221   dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
222   dalvik_system_DexPathList__Element = CacheClass(env, "dalvik/system/DexPathList$Element");
223   dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
224   dalvik_system_VMRuntime = CacheClass(env, "dalvik/system/VMRuntime");
225 
226   java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
227   java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
228   java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
229   java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
230   java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
231   java_lang_Object = CacheClass(env, "java/lang/Object");
232   java_lang_OutOfMemoryError = CacheClass(env, "java/lang/OutOfMemoryError");
233   java_lang_Error = CacheClass(env, "java/lang/Error");
234   java_lang_ExceptionInInitializerError = CacheClass(env, "java/lang/ExceptionInInitializerError");
235   java_lang_IllegalAccessError = CacheClass(env, "java/lang/IllegalAccessError");
236   java_lang_NoClassDefFoundError = CacheClass(env, "java/lang/NoClassDefFoundError");
237   java_lang_reflect_AbstractMethod = CacheClass(env, "java/lang/reflect/AbstractMethod");
238   java_lang_reflect_Constructor = CacheClass(env, "java/lang/reflect/Constructor");
239   java_lang_reflect_Field = CacheClass(env, "java/lang/reflect/Field");
240   java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
241   java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
242   java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
243   java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
244   java_lang_String = CacheClass(env, "java/lang/String");
245   java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
246   java_lang_System = CacheClass(env, "java/lang/System");
247   java_lang_Thread = CacheClass(env, "java/lang/Thread");
248   java_lang_Thread__UncaughtExceptionHandler = CacheClass(env,
249       "java/lang/Thread$UncaughtExceptionHandler");
250   java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
251   java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
252   java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
253   java_util_ArrayList = CacheClass(env, "java/util/ArrayList");
254   java_util_Collections = CacheClass(env, "java/util/Collections");
255   libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
256   libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
257   libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
258   org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
259   org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
260 
261   dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
262   com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
263   java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
264   java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
265 
266   java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
267   java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
268   java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
269 
270   ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
271   java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
272   ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
273   java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
274 
275   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;");
276   java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
277   java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
278   java_lang_Thread__UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread__UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
279   java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
280   java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
281   libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
282   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");
283   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
284   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;");
285 
286   java_lang_String_init = CacheMethod(env, java_lang_String, false, "<init>", "()V");
287   java_lang_String_init_B = CacheMethod(env, java_lang_String, false, "<init>", "([B)V");
288   java_lang_String_init_BI = CacheMethod(env, java_lang_String, false, "<init>", "([BI)V");
289   java_lang_String_init_BII = CacheMethod(env, java_lang_String, false, "<init>", "([BII)V");
290   java_lang_String_init_BIII = CacheMethod(env, java_lang_String, false, "<init>", "([BIII)V");
291   java_lang_String_init_BIIString = CacheMethod(env, java_lang_String, false, "<init>",
292       "([BIILjava/lang/String;)V");
293   java_lang_String_init_BString = CacheMethod(env, java_lang_String, false, "<init>",
294       "([BLjava/lang/String;)V");
295   java_lang_String_init_BIICharset = CacheMethod(env, java_lang_String, false, "<init>",
296       "([BIILjava/nio/charset/Charset;)V");
297   java_lang_String_init_BCharset = CacheMethod(env, java_lang_String, false, "<init>",
298       "([BLjava/nio/charset/Charset;)V");
299   java_lang_String_init_C = CacheMethod(env, java_lang_String, false, "<init>", "([C)V");
300   java_lang_String_init_CII = CacheMethod(env, java_lang_String, false, "<init>", "([CII)V");
301   java_lang_String_init_IIC = CacheMethod(env, java_lang_String, false, "<init>", "(II[C)V");
302   java_lang_String_init_String = CacheMethod(env, java_lang_String, false, "<init>",
303       "(Ljava/lang/String;)V");
304   java_lang_String_init_StringBuffer = CacheMethod(env, java_lang_String, false, "<init>",
305       "(Ljava/lang/StringBuffer;)V");
306   java_lang_String_init_III = CacheMethod(env, java_lang_String, false, "<init>", "([III)V");
307   java_lang_String_init_StringBuilder = CacheMethod(env, java_lang_String, false, "<init>",
308        "(Ljava/lang/StringBuilder;)V");
309   java_lang_StringFactory_newEmptyString = CacheMethod(env, java_lang_StringFactory, true,
310        "newEmptyString", "()Ljava/lang/String;");
311   java_lang_StringFactory_newStringFromBytes_B = CacheMethod(env, java_lang_StringFactory, true,
312        "newStringFromBytes", "([B)Ljava/lang/String;");
313   java_lang_StringFactory_newStringFromBytes_BI = CacheMethod(env, java_lang_StringFactory, true,
314        "newStringFromBytes", "([BI)Ljava/lang/String;");
315   java_lang_StringFactory_newStringFromBytes_BII = CacheMethod(env, java_lang_StringFactory, true,
316        "newStringFromBytes", "([BII)Ljava/lang/String;");
317   java_lang_StringFactory_newStringFromBytes_BIII = CacheMethod(env, java_lang_StringFactory, true,
318        "newStringFromBytes", "([BIII)Ljava/lang/String;");
319   java_lang_StringFactory_newStringFromBytes_BIIString = CacheMethod(env, java_lang_StringFactory,
320        true, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;");
321   java_lang_StringFactory_newStringFromBytes_BString = CacheMethod(env, java_lang_StringFactory,
322        true, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;");
323   java_lang_StringFactory_newStringFromBytes_BIICharset = CacheMethod(env, java_lang_StringFactory,
324        true, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;");
325   java_lang_StringFactory_newStringFromBytes_BCharset = CacheMethod(env, java_lang_StringFactory,
326        true, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;");
327   java_lang_StringFactory_newStringFromChars_C = CacheMethod(env, java_lang_StringFactory, true,
328        "newStringFromChars", "([C)Ljava/lang/String;");
329   java_lang_StringFactory_newStringFromChars_CII = CacheMethod(env, java_lang_StringFactory, true,
330        "newStringFromChars", "([CII)Ljava/lang/String;");
331   java_lang_StringFactory_newStringFromChars_IIC = CacheMethod(env, java_lang_StringFactory, true,
332        "newStringFromChars", "(II[C)Ljava/lang/String;");
333   java_lang_StringFactory_newStringFromString = CacheMethod(env, java_lang_StringFactory, true,
334        "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;");
335   java_lang_StringFactory_newStringFromStringBuffer = CacheMethod(env, java_lang_StringFactory,
336        true, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;");
337   java_lang_StringFactory_newStringFromCodePoints = CacheMethod(env, java_lang_StringFactory,
338        true, "newStringFromCodePoints", "([III)Ljava/lang/String;");
339   java_lang_StringFactory_newStringFromStringBuilder = CacheMethod(env, java_lang_StringFactory,
340        true, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;");
341 
342   dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
343   dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
344   dalvik_system_PathClassLoader_pathList = CacheField(env, dalvik_system_PathClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
345   dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
346   dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
347   java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
348   java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
349   java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
350   java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
351   java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
352   java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtExceptionHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
353   java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
354   java_lang_ThreadGroup_groups = CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
355   java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
356   java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
357   java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
358   java_lang_ThreadGroup_parent = CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
359   java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
360   java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
361   java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
362   java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
363   java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
364   java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
365   java_lang_reflect_AbstractMethod_artMethod = CacheField(env, java_lang_reflect_AbstractMethod, false, "artMethod", "J");
366   java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
367   java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
368   java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "address", "J");
369   java_util_ArrayList_array = CacheField(env, java_util_ArrayList, false, "elementData", "[Ljava/lang/Object;");
370   java_util_ArrayList_size = CacheField(env, java_util_ArrayList, false, "size", "I");
371   java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
372   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
373   org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
374   org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
375   org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
376   org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
377 
378   java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
379   java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
380   java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
381   java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
382   java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
383   java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
384   java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
385   java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
386 
387   Thread::Current()->InitStringEntryPoints();
388 }
389 
LateInit(JNIEnv * env)390 void WellKnownClasses::LateInit(JNIEnv* env) {
391   ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
392   java_lang_Runtime_nativeLoad =
393       CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
394                   "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)"
395                       "Ljava/lang/String;");
396 }
397 
ToClass(jclass global_jclass)398 mirror::Class* WellKnownClasses::ToClass(jclass global_jclass) {
399   return reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(global_jclass));
400 }
401 
StringInitToStringFactoryMethodID(jmethodID string_init)402 jmethodID WellKnownClasses::StringInitToStringFactoryMethodID(jmethodID string_init) {
403   // TODO: Prioritize ordering.
404   if (string_init == java_lang_String_init) {
405     return java_lang_StringFactory_newEmptyString;
406   } else if (string_init == java_lang_String_init_B) {
407     return java_lang_StringFactory_newStringFromBytes_B;
408   } else if (string_init == java_lang_String_init_BI) {
409     return java_lang_StringFactory_newStringFromBytes_BI;
410   } else if (string_init == java_lang_String_init_BII) {
411     return java_lang_StringFactory_newStringFromBytes_BII;
412   } else if (string_init == java_lang_String_init_BIII) {
413     return java_lang_StringFactory_newStringFromBytes_BIII;
414   } else if (string_init == java_lang_String_init_BIIString) {
415     return java_lang_StringFactory_newStringFromBytes_BIIString;
416   } else if (string_init == java_lang_String_init_BString) {
417     return java_lang_StringFactory_newStringFromBytes_BString;
418   } else if (string_init == java_lang_String_init_BIICharset) {
419     return java_lang_StringFactory_newStringFromBytes_BIICharset;
420   } else if (string_init == java_lang_String_init_BCharset) {
421     return java_lang_StringFactory_newStringFromBytes_BCharset;
422   } else if (string_init == java_lang_String_init_C) {
423     return java_lang_StringFactory_newStringFromChars_C;
424   } else if (string_init == java_lang_String_init_CII) {
425     return java_lang_StringFactory_newStringFromChars_CII;
426   } else if (string_init == java_lang_String_init_IIC) {
427     return java_lang_StringFactory_newStringFromChars_IIC;
428   } else if (string_init == java_lang_String_init_String) {
429     return java_lang_StringFactory_newStringFromString;
430   } else if (string_init == java_lang_String_init_StringBuffer) {
431     return java_lang_StringFactory_newStringFromStringBuffer;
432   } else if (string_init == java_lang_String_init_III) {
433     return java_lang_StringFactory_newStringFromCodePoints;
434   } else if (string_init == java_lang_String_init_StringBuilder) {
435     return java_lang_StringFactory_newStringFromStringBuilder;
436   }
437   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
438   return nullptr;
439 }
440 
441 }  // namespace art
442