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