blob: a0397fff211adc957edd8b642a068eafd47eae81 [file] [log] [blame]
Elliott Hugheseac76672012-05-24 21:56:51 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "well_known_classes.h"
18
19#include <stdlib.h>
20
21#include "logging.h"
22#include "ScopedLocalRef.h"
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070023#include "thread.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070024
25namespace art {
26
27jclass WellKnownClasses::com_android_dex_Dex;
28jclass WellKnownClasses::java_lang_ClassLoader;
29jclass WellKnownClasses::java_lang_ClassNotFoundException;
30jclass WellKnownClasses::java_lang_Daemons;
31jclass WellKnownClasses::java_lang_Error;
Elliott Hugheseac76672012-05-24 21:56:51 -070032jclass WellKnownClasses::java_lang_reflect_InvocationHandler;
33jclass WellKnownClasses::java_lang_reflect_Method;
34jclass WellKnownClasses::java_lang_reflect_Proxy;
Elliott Hughesa4f94742012-05-29 16:28:38 -070035jclass WellKnownClasses::java_lang_RuntimeException;
Elliott Hugheseac76672012-05-24 21:56:51 -070036jclass WellKnownClasses::java_lang_Thread;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070037jclass WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler;
38jclass WellKnownClasses::java_lang_ThreadGroup;
39jclass WellKnownClasses::java_lang_ThreadLock;
Elliott Hughesa4f94742012-05-29 16:28:38 -070040jclass WellKnownClasses::java_lang_Throwable;
Elliott Hugheseac76672012-05-24 21:56:51 -070041jclass WellKnownClasses::java_nio_ReadWriteDirectByteBuffer;
42jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
43jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
44
45jmethodID WellKnownClasses::com_android_dex_Dex_create;
46jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
Elliott Hughesa4f94742012-05-29 16:28:38 -070047jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070048jmethodID WellKnownClasses::java_lang_Daemons_requestGC;
Elliott Hugheseac76672012-05-24 21:56:51 -070049jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
50jmethodID WellKnownClasses::java_lang_Daemons_start;
Elliott Hughesa4f94742012-05-29 16:28:38 -070051jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
52jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Elliott Hugheseac76672012-05-24 21:56:51 -070053jmethodID WellKnownClasses::java_lang_reflect_InvocationHandler_invoke;
54jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070055jmethodID WellKnownClasses::java_lang_Thread_run;
56jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException;
57jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Elliott Hugheseac76672012-05-24 21:56:51 -070058jmethodID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init;
59jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
60jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
61
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070062jfieldID WellKnownClasses::java_lang_Thread_daemon;
63jfieldID WellKnownClasses::java_lang_Thread_group;
64jfieldID WellKnownClasses::java_lang_Thread_lock;
65jfieldID WellKnownClasses::java_lang_Thread_name;
66jfieldID WellKnownClasses::java_lang_Thread_priority;
67jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
68jfieldID WellKnownClasses::java_lang_Thread_vmData;
69jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
70jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
71jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
72jfieldID WellKnownClasses::java_lang_ThreadLock_thread;
Elliott Hugheseac76672012-05-24 21:56:51 -070073jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
74jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity;
75jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress;
76jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
77jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
78jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
79jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
80
81static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
82 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
83 if (c.get() == NULL) {
84 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
85 }
86 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
87}
88
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070089static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
90 jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) : env->GetFieldID(c, name, signature);
Elliott Hugheseac76672012-05-24 21:56:51 -070091 if (fid == NULL) {
92 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\"";
93 }
94 return fid;
95}
96
97static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
98 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) : env->GetMethodID(c, name, signature);
99 if (mid == NULL) {
100 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\"";
101 }
102 return mid;
103}
104
105void WellKnownClasses::Init(JNIEnv* env) {
106 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
107 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
108 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
109 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
110 java_lang_Error = CacheClass(env, "java/lang/Error");
Elliott Hugheseac76672012-05-24 21:56:51 -0700111 java_lang_reflect_InvocationHandler = CacheClass(env, "java/lang/reflect/InvocationHandler");
112 java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
113 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700114 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Elliott Hugheseac76672012-05-24 21:56:51 -0700115 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700116 java_lang_Thread$UncaughtExceptionHandler = CacheClass(env, "java/lang/Thread$UncaughtExceptionHandler");
117 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
118 java_lang_ThreadLock = CacheClass(env, "java/lang/ThreadLock");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700119 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Elliott Hugheseac76672012-05-24 21:56:51 -0700120 java_nio_ReadWriteDirectByteBuffer = CacheClass(env, "java/nio/ReadWriteDirectByteBuffer");
121 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
122 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
123
124 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
125 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 -0700126 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700127
128 java_lang_Daemons_requestGC = CacheMethod(env, java_lang_Daemons, true, "requestGC", "()V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700129 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
130 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700131
132 ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
133 java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
134 ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
135 java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
136
Elliott Hugheseac76672012-05-24 21:56:51 -0700137 java_lang_reflect_InvocationHandler_invoke = CacheMethod(env, java_lang_reflect_InvocationHandler, false, "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
138 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 -0700139 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
140 java_lang_Thread$UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread$UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
141 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "removeThread", "(Ljava/lang/Thread;)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700142 java_nio_ReadWriteDirectByteBuffer_init = CacheMethod(env, java_nio_ReadWriteDirectByteBuffer, false, "<init>", "(II)V");
143 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
144 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;");
145
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700146 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
147 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
148 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/ThreadLock;");
149 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
150 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
151 java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
152 java_lang_Thread_vmData = CacheField(env, java_lang_Thread, false, "vmData", "I");
153 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
154 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
155 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
156 java_lang_ThreadLock_thread = CacheField(env, java_lang_ThreadLock, false, "thread", "Ljava/lang/Thread;");
157 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
158 java_nio_ReadWriteDirectByteBuffer_capacity = CacheField(env, java_nio_ReadWriteDirectByteBuffer, false, "capacity", "I");
159 java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_ReadWriteDirectByteBuffer, false, "effectiveDirectAddress", "I");
160 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
161 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
162 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
163 org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
164}
165
166Class* WellKnownClasses::ToClass(jclass global_jclass) {
167 return reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(global_jclass));
Elliott Hugheseac76672012-05-24 21:56:51 -0700168}
169
170} // namespace art