• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "android-base/stringprintf.h"
24 
25 #include "base/logging.h"
26 #include "entrypoints/quick/quick_entrypoints_enum.h"
27 #include "jni_internal.h"
28 #include "mirror/class.h"
29 #include "mirror/throwable.h"
30 #include "nativehelper/ScopedLocalRef.h"
31 #include "obj_ptr-inl.h"
32 #include "scoped_thread_state_change-inl.h"
33 #include "thread-current-inl.h"
34 
35 namespace art {
36 
37 jclass WellKnownClasses::dalvik_annotation_optimization_CriticalNative;
38 jclass WellKnownClasses::dalvik_annotation_optimization_FastNative;
39 jclass WellKnownClasses::dalvik_system_BaseDexClassLoader;
40 jclass WellKnownClasses::dalvik_system_DelegateLastClassLoader;
41 jclass WellKnownClasses::dalvik_system_DexClassLoader;
42 jclass WellKnownClasses::dalvik_system_DexFile;
43 jclass WellKnownClasses::dalvik_system_DexPathList;
44 jclass WellKnownClasses::dalvik_system_DexPathList__Element;
45 jclass WellKnownClasses::dalvik_system_EmulatedStackFrame;
46 jclass WellKnownClasses::dalvik_system_PathClassLoader;
47 jclass WellKnownClasses::dalvik_system_VMRuntime;
48 jclass WellKnownClasses::java_lang_annotation_Annotation__array;
49 jclass WellKnownClasses::java_lang_BootClassLoader;
50 jclass WellKnownClasses::java_lang_ClassLoader;
51 jclass WellKnownClasses::java_lang_ClassNotFoundException;
52 jclass WellKnownClasses::java_lang_Daemons;
53 jclass WellKnownClasses::java_lang_Error;
54 jclass WellKnownClasses::java_lang_invoke_MethodHandle;
55 jclass WellKnownClasses::java_lang_IllegalAccessError;
56 jclass WellKnownClasses::java_lang_NoClassDefFoundError;
57 jclass WellKnownClasses::java_lang_Object;
58 jclass WellKnownClasses::java_lang_OutOfMemoryError;
59 jclass WellKnownClasses::java_lang_reflect_Constructor;
60 jclass WellKnownClasses::java_lang_reflect_Executable;
61 jclass WellKnownClasses::java_lang_reflect_Field;
62 jclass WellKnownClasses::java_lang_reflect_Method;
63 jclass WellKnownClasses::java_lang_reflect_Parameter;
64 jclass WellKnownClasses::java_lang_reflect_Parameter__array;
65 jclass WellKnownClasses::java_lang_reflect_Proxy;
66 jclass WellKnownClasses::java_lang_RuntimeException;
67 jclass WellKnownClasses::java_lang_StackOverflowError;
68 jclass WellKnownClasses::java_lang_String;
69 jclass WellKnownClasses::java_lang_StringFactory;
70 jclass WellKnownClasses::java_lang_System;
71 jclass WellKnownClasses::java_lang_Thread;
72 jclass WellKnownClasses::java_lang_ThreadGroup;
73 jclass WellKnownClasses::java_lang_Throwable;
74 jclass WellKnownClasses::java_nio_DirectByteBuffer;
75 jclass WellKnownClasses::java_util_ArrayList;
76 jclass WellKnownClasses::java_util_Collections;
77 jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
78 jclass WellKnownClasses::libcore_reflect_AnnotationMember;
79 jclass WellKnownClasses::libcore_util_EmptyArray;
80 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
81 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
82 
83 jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
84 jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
85 jmethodID WellKnownClasses::java_lang_Byte_valueOf;
86 jmethodID WellKnownClasses::java_lang_Character_valueOf;
87 jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
88 jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
89 jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
90 jmethodID WellKnownClasses::java_lang_Daemons_start;
91 jmethodID WellKnownClasses::java_lang_Daemons_stop;
92 jmethodID WellKnownClasses::java_lang_Double_valueOf;
93 jmethodID WellKnownClasses::java_lang_Float_valueOf;
94 jmethodID WellKnownClasses::java_lang_Integer_valueOf;
95 jmethodID WellKnownClasses::java_lang_invoke_MethodHandle_invoke;
96 jmethodID WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact;
97 jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_lookup;
98 jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor;
99 jmethodID WellKnownClasses::java_lang_Long_valueOf;
100 jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
101 jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
102 jmethodID WellKnownClasses::java_lang_reflect_Parameter_init;
103 jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
104 jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
105 jmethodID WellKnownClasses::java_lang_Short_valueOf;
106 jmethodID WellKnownClasses::java_lang_String_charAt;
107 jmethodID WellKnownClasses::java_lang_System_runFinalization = nullptr;
108 jmethodID WellKnownClasses::java_lang_Thread_dispatchUncaughtException;
109 jmethodID WellKnownClasses::java_lang_Thread_init;
110 jmethodID WellKnownClasses::java_lang_Thread_run;
111 jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
112 jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
113 jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
114 jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
115 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
116 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
117 
118 jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
119 jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
120 jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList;
121 jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
122 jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
123 jfieldID WellKnownClasses::java_lang_Thread_daemon;
124 jfieldID WellKnownClasses::java_lang_Thread_group;
125 jfieldID WellKnownClasses::java_lang_Thread_lock;
126 jfieldID WellKnownClasses::java_lang_Thread_name;
127 jfieldID WellKnownClasses::java_lang_Thread_priority;
128 jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
129 jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
130 jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
131 jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
132 jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
133 jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
134 jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
135 jfieldID WellKnownClasses::java_lang_Throwable_cause;
136 jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
137 jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
138 jfieldID WellKnownClasses::java_lang_Throwable_stackState;
139 jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
140 jfieldID WellKnownClasses::java_lang_reflect_Executable_artMethod;
141 jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
142 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
143 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
144 jfieldID WellKnownClasses::java_util_ArrayList_array;
145 jfieldID WellKnownClasses::java_util_ArrayList_size;
146 jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
147 jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
148 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
149 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
150 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
151 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
152 
CacheClass(JNIEnv * env,const char * jni_class_name)153 static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
154   ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
155   if (c.get() == nullptr) {
156     LOG(FATAL) << "Couldn't find class: " << jni_class_name;
157   }
158   return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
159 }
160 
CacheField(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)161 static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static,
162                            const char* name, const char* signature) {
163   jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) :
164       env->GetFieldID(c, name, signature);
165   if (fid == nullptr) {
166     ScopedObjectAccess soa(env);
167     if (soa.Self()->IsExceptionPending()) {
168       LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
169     }
170     std::ostringstream os;
171     WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
172     LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\": "
173                << os.str();
174   }
175   return fid;
176 }
177 
CacheMethod(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)178 static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static,
179                              const char* name, const char* signature) {
180   jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) :
181       env->GetMethodID(c, name, signature);
182   if (mid == nullptr) {
183     ScopedObjectAccess soa(env);
184     if (soa.Self()->IsExceptionPending()) {
185       LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
186     }
187     std::ostringstream os;
188     WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
189     LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\": "
190                << os.str();
191   }
192   return mid;
193 }
194 
CacheMethod(JNIEnv * env,const char * klass,bool is_static,const char * name,const char * signature)195 static jmethodID CacheMethod(JNIEnv* env, const char* klass, bool is_static,
196                       const char* name, const char* signature) {
197   ScopedLocalRef<jclass> java_class(env, env->FindClass(klass));
198   return CacheMethod(env, java_class.get(), is_static, name, signature);
199 }
200 
CachePrimitiveBoxingMethod(JNIEnv * env,char prim_name,const char * boxed_name)201 static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
202   ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
203   return CacheMethod(env, boxed_class.get(), true, "valueOf",
204                      android::base::StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
205 }
206 
207 #define STRING_INIT_LIST(V) \
208   V(java_lang_String_init, "()V", newEmptyString, "newEmptyString", "()Ljava/lang/String;", NewEmptyString) \
209   V(java_lang_String_init_B, "([B)V", newStringFromBytes_B, "newStringFromBytes", "([B)Ljava/lang/String;", NewStringFromBytes_B) \
210   V(java_lang_String_init_BI, "([BI)V", newStringFromBytes_BI, "newStringFromBytes", "([BI)Ljava/lang/String;", NewStringFromBytes_BI) \
211   V(java_lang_String_init_BII, "([BII)V", newStringFromBytes_BII, "newStringFromBytes", "([BII)Ljava/lang/String;", NewStringFromBytes_BII) \
212   V(java_lang_String_init_BIII, "([BIII)V", newStringFromBytes_BIII, "newStringFromBytes", "([BIII)Ljava/lang/String;", NewStringFromBytes_BIII) \
213   V(java_lang_String_init_BIIString, "([BIILjava/lang/String;)V", newStringFromBytes_BIIString, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BIIString) \
214   V(java_lang_String_init_BString, "([BLjava/lang/String;)V", newStringFromBytes_BString, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BString) \
215   V(java_lang_String_init_BIICharset, "([BIILjava/nio/charset/Charset;)V", newStringFromBytes_BIICharset, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BIICharset) \
216   V(java_lang_String_init_BCharset, "([BLjava/nio/charset/Charset;)V", newStringFromBytes_BCharset, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BCharset) \
217   V(java_lang_String_init_C, "([C)V", newStringFromChars_C, "newStringFromChars", "([C)Ljava/lang/String;", NewStringFromChars_C) \
218   V(java_lang_String_init_CII, "([CII)V", newStringFromChars_CII, "newStringFromChars", "([CII)Ljava/lang/String;", NewStringFromChars_CII) \
219   V(java_lang_String_init_IIC, "(II[C)V", newStringFromChars_IIC, "newStringFromChars", "(II[C)Ljava/lang/String;", NewStringFromChars_IIC) \
220   V(java_lang_String_init_String, "(Ljava/lang/String;)V", newStringFromString, "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;", NewStringFromString) \
221   V(java_lang_String_init_StringBuffer, "(Ljava/lang/StringBuffer;)V", newStringFromStringBuffer, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;", NewStringFromStringBuffer) \
222   V(java_lang_String_init_III, "([III)V", newStringFromCodePoints, "newStringFromCodePoints", "([III)Ljava/lang/String;", NewStringFromCodePoints) \
223   V(java_lang_String_init_StringBuilder, "(Ljava/lang/StringBuilder;)V", newStringFromStringBuilder, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;", NewStringFromStringBuilder) \
224 
225 #define STATIC_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, ...) \
226     static ArtMethod* init_runtime_name; \
227     static ArtMethod* new_runtime_name;
STRING_INIT_LIST(STATIC_STRING_INIT)228     STRING_INIT_LIST(STATIC_STRING_INIT)
229 #undef STATIC_STRING_INIT
230 
231 void WellKnownClasses::InitStringInit(JNIEnv* env) {
232   ScopedObjectAccess soa(Thread::Current());
233   #define LOAD_STRING_INIT(init_runtime_name, init_signature, new_runtime_name,             \
234                            new_java_name, new_signature, ...)                               \
235       init_runtime_name = jni::DecodeArtMethod(                                             \
236           CacheMethod(env, java_lang_String, false, "<init>", init_signature));             \
237       new_runtime_name = jni::DecodeArtMethod(                                              \
238           CacheMethod(env, java_lang_StringFactory, true, new_java_name, new_signature));
239       STRING_INIT_LIST(LOAD_STRING_INIT)
240   #undef LOAD_STRING_INIT
241 }
242 
InitStringEntryPoints()243 void Thread::InitStringEntryPoints() {
244   QuickEntryPoints* qpoints = &tlsPtr_.quick_entrypoints;
245   #define SET_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name,              \
246                           new_java_name, new_signature, entry_point_name)                   \
247       qpoints->p ## entry_point_name = reinterpret_cast<void(*)()>(new_runtime_name);
248       STRING_INIT_LIST(SET_ENTRY_POINT)
249   #undef SET_ENTRY_POINT
250 }
251 
StringInitToStringFactory(ArtMethod * string_init)252 ArtMethod* WellKnownClasses::StringInitToStringFactory(ArtMethod* string_init) {
253   #define TO_STRING_FACTORY(init_runtime_name, init_signature, new_runtime_name,            \
254                             new_java_name, new_signature, entry_point_name)                 \
255       if (string_init == (init_runtime_name)) {                                             \
256         return (new_runtime_name);                                                          \
257       }
258       STRING_INIT_LIST(TO_STRING_FACTORY)
259   #undef TO_STRING_FACTORY
260   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
261   return nullptr;
262 }
263 
StringInitToEntryPoint(ArtMethod * string_init)264 uint32_t WellKnownClasses::StringInitToEntryPoint(ArtMethod* string_init) {
265   #define TO_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name,               \
266                          new_java_name, new_signature, entry_point_name)                    \
267       if (string_init == (init_runtime_name)) {                                             \
268         return kQuick ## entry_point_name;                                                  \
269       }
270       STRING_INIT_LIST(TO_ENTRY_POINT)
271   #undef TO_STRING_FACTORY
272   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
273   return 0;
274 }
275 #undef STRING_INIT_LIST
276 
Init(JNIEnv * env)277 void WellKnownClasses::Init(JNIEnv* env) {
278   dalvik_annotation_optimization_CriticalNative =
279       CacheClass(env, "dalvik/annotation/optimization/CriticalNative");
280   dalvik_annotation_optimization_FastNative = CacheClass(env, "dalvik/annotation/optimization/FastNative");
281   dalvik_system_BaseDexClassLoader = CacheClass(env, "dalvik/system/BaseDexClassLoader");
282   dalvik_system_DelegateLastClassLoader = CacheClass(env, "dalvik/system/DelegateLastClassLoader");
283   dalvik_system_DexClassLoader = CacheClass(env, "dalvik/system/DexClassLoader");
284   dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
285   dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
286   dalvik_system_DexPathList__Element = CacheClass(env, "dalvik/system/DexPathList$Element");
287   dalvik_system_EmulatedStackFrame = CacheClass(env, "dalvik/system/EmulatedStackFrame");
288   dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
289   dalvik_system_VMRuntime = CacheClass(env, "dalvik/system/VMRuntime");
290 
291   java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
292   java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
293   java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
294   java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
295   java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
296   java_lang_Object = CacheClass(env, "java/lang/Object");
297   java_lang_OutOfMemoryError = CacheClass(env, "java/lang/OutOfMemoryError");
298   java_lang_Error = CacheClass(env, "java/lang/Error");
299   java_lang_IllegalAccessError = CacheClass(env, "java/lang/IllegalAccessError");
300   java_lang_invoke_MethodHandle = CacheClass(env, "java/lang/invoke/MethodHandle");
301   java_lang_NoClassDefFoundError = CacheClass(env, "java/lang/NoClassDefFoundError");
302   java_lang_reflect_Constructor = CacheClass(env, "java/lang/reflect/Constructor");
303   java_lang_reflect_Executable = CacheClass(env, "java/lang/reflect/Executable");
304   java_lang_reflect_Field = CacheClass(env, "java/lang/reflect/Field");
305   java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
306   java_lang_reflect_Parameter = CacheClass(env, "java/lang/reflect/Parameter");
307   java_lang_reflect_Parameter__array = CacheClass(env, "[Ljava/lang/reflect/Parameter;");
308   java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
309   java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
310   java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
311   java_lang_String = CacheClass(env, "java/lang/String");
312   java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
313   java_lang_System = CacheClass(env, "java/lang/System");
314   java_lang_Thread = CacheClass(env, "java/lang/Thread");
315   java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
316   java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
317   java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
318   java_util_ArrayList = CacheClass(env, "java/util/ArrayList");
319   java_util_Collections = CacheClass(env, "java/util/Collections");
320   libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
321   libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
322   libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
323   org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
324   org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
325 
326   dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
327   java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
328   java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
329 
330   java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
331   java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
332   java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
333   java_lang_invoke_MethodHandle_invoke = CacheMethod(env, java_lang_invoke_MethodHandle, false, "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;");
334   java_lang_invoke_MethodHandle_invokeExact = CacheMethod(env, java_lang_invoke_MethodHandle, false, "invokeExact", "([Ljava/lang/Object;)Ljava/lang/Object;");
335   java_lang_invoke_MethodHandles_lookup = CacheMethod(env, "java/lang/invoke/MethodHandles", true, "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;");
336   java_lang_invoke_MethodHandles_Lookup_findConstructor = CacheMethod(env, "java/lang/invoke/MethodHandles$Lookup", false, "findConstructor", "(Ljava/lang/Class;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;");
337   java_lang_ref_FinalizerReference_add = CacheMethod(env, "java/lang/ref/FinalizerReference", true, "add", "(Ljava/lang/Object;)V");
338   java_lang_ref_ReferenceQueue_add = CacheMethod(env, "java/lang/ref/ReferenceQueue", true, "add", "(Ljava/lang/ref/Reference;)V");
339 
340   java_lang_reflect_Parameter_init = CacheMethod(env, java_lang_reflect_Parameter, false, "<init>", "(Ljava/lang/String;ILjava/lang/reflect/Executable;I)V");
341   java_lang_String_charAt = CacheMethod(env, java_lang_String, false, "charAt", "(I)C");
342   java_lang_Thread_dispatchUncaughtException = CacheMethod(env, java_lang_Thread, false, "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
343   java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
344   java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
345   java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
346   java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
347   libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
348   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");
349   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
350   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;");
351 
352   dalvik_system_BaseDexClassLoader_pathList = CacheField(env, dalvik_system_BaseDexClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
353   dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
354   dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
355   dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
356   dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
357   java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
358   java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
359   java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
360   java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
361   java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
362   java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
363   java_lang_ThreadGroup_groups = CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
364   java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
365   java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
366   java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
367   java_lang_ThreadGroup_parent = CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
368   java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
369   java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
370   java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
371   java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
372   java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
373   java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
374   java_lang_reflect_Executable_artMethod = CacheField(env, java_lang_reflect_Executable, false, "artMethod", "J");
375   java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
376   java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "address", "J");
377   java_util_ArrayList_array = CacheField(env, java_util_ArrayList, false, "elementData", "[Ljava/lang/Object;");
378   java_util_ArrayList_size = CacheField(env, java_util_ArrayList, false, "size", "I");
379   java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
380   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
381   org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
382   org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
383   org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
384   org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
385 
386   java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
387   java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
388   java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
389   java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
390   java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
391   java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
392   java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
393   java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
394 
395   InitStringInit(env);
396   Thread::Current()->InitStringEntryPoints();
397 }
398 
LateInit(JNIEnv * env)399 void WellKnownClasses::LateInit(JNIEnv* env) {
400   ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
401   // CacheField and CacheMethod will initialize their classes. Classes below
402   // have clinit sections that call JNI methods. Late init is required
403   // to make sure these JNI methods are available.
404   java_lang_Runtime_nativeLoad =
405       CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
406                   "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)"
407                       "Ljava/lang/String;");
408   java_lang_reflect_Proxy_invoke =
409     CacheMethod(env, java_lang_reflect_Proxy, true, "invoke",
410                 "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;"
411                     "[Ljava/lang/Object;)Ljava/lang/Object;");
412   java_lang_reflect_Proxy_h =
413     CacheField(env, java_lang_reflect_Proxy, false, "h",
414                "Ljava/lang/reflect/InvocationHandler;");
415 }
416 
ToClass(jclass global_jclass)417 ObjPtr<mirror::Class> WellKnownClasses::ToClass(jclass global_jclass) {
418   auto ret = ObjPtr<mirror::Class>::DownCast(Thread::Current()->DecodeJObject(global_jclass));
419   DCHECK(!ret.IsNull());
420   return ret;
421 }
422 
423 }  // namespace art
424