blob: 20e71d5fdda90b871bb0b097a1c63d357bfffc16 [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;
Elliott Hugheseac76672012-05-24 21:56:51 -070048jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
49jmethodID WellKnownClasses::java_lang_Daemons_start;
Elliott Hughesa4f94742012-05-29 16:28:38 -070050jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
51jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Elliott Hugheseac76672012-05-24 21:56:51 -070052jmethodID WellKnownClasses::java_lang_reflect_InvocationHandler_invoke;
53jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070054jmethodID WellKnownClasses::java_lang_Thread_run;
55jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException;
56jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Elliott Hugheseac76672012-05-24 21:56:51 -070057jmethodID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init;
58jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
59jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
60
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070061jfieldID WellKnownClasses::java_lang_Thread_daemon;
62jfieldID WellKnownClasses::java_lang_Thread_group;
63jfieldID WellKnownClasses::java_lang_Thread_lock;
64jfieldID WellKnownClasses::java_lang_Thread_name;
65jfieldID WellKnownClasses::java_lang_Thread_priority;
66jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
67jfieldID WellKnownClasses::java_lang_Thread_vmData;
68jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
69jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
70jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
71jfieldID WellKnownClasses::java_lang_ThreadLock_thread;
Elliott Hugheseac76672012-05-24 21:56:51 -070072jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
73jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity;
74jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress;
75jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
76jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
77jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
78jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
79
80static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
81 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
82 if (c.get() == NULL) {
83 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
84 }
85 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
86}
87
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070088static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
89 jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) : env->GetFieldID(c, name, signature);
Elliott Hugheseac76672012-05-24 21:56:51 -070090 if (fid == NULL) {
91 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\"";
92 }
93 return fid;
94}
95
96static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
97 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) : env->GetMethodID(c, name, signature);
98 if (mid == NULL) {
99 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\"";
100 }
101 return mid;
102}
103
104void WellKnownClasses::Init(JNIEnv* env) {
105 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
106 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
107 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
108 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
109 java_lang_Error = CacheClass(env, "java/lang/Error");
Elliott Hugheseac76672012-05-24 21:56:51 -0700110 java_lang_reflect_InvocationHandler = CacheClass(env, "java/lang/reflect/InvocationHandler");
111 java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
112 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700113 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Elliott Hugheseac76672012-05-24 21:56:51 -0700114 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700115 java_lang_Thread$UncaughtExceptionHandler = CacheClass(env, "java/lang/Thread$UncaughtExceptionHandler");
116 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
117 java_lang_ThreadLock = CacheClass(env, "java/lang/ThreadLock");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700118 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Elliott Hugheseac76672012-05-24 21:56:51 -0700119 java_nio_ReadWriteDirectByteBuffer = CacheClass(env, "java/nio/ReadWriteDirectByteBuffer");
120 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
121 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
122
123 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
124 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 -0700125 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Elliott Hugheseac76672012-05-24 21:56:51 -0700126 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
127 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700128
129 ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
130 java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
131 ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
132 java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
133
Elliott Hugheseac76672012-05-24 21:56:51 -0700134 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;");
135 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 -0700136 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
137 java_lang_Thread$UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread$UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
138 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "removeThread", "(Ljava/lang/Thread;)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700139 java_nio_ReadWriteDirectByteBuffer_init = CacheMethod(env, java_nio_ReadWriteDirectByteBuffer, false, "<init>", "(II)V");
140 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
141 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;");
142
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700143 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
144 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
145 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/ThreadLock;");
146 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
147 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
148 java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
149 java_lang_Thread_vmData = CacheField(env, java_lang_Thread, false, "vmData", "I");
150 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
151 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
152 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
153 java_lang_ThreadLock_thread = CacheField(env, java_lang_ThreadLock, false, "thread", "Ljava/lang/Thread;");
154 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
155 java_nio_ReadWriteDirectByteBuffer_capacity = CacheField(env, java_nio_ReadWriteDirectByteBuffer, false, "capacity", "I");
156 java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_ReadWriteDirectByteBuffer, false, "effectiveDirectAddress", "I");
157 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
158 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
159 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
160 org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
161}
162
163Class* WellKnownClasses::ToClass(jclass global_jclass) {
164 return reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(global_jclass));
Elliott Hugheseac76672012-05-24 21:56:51 -0700165}
166
167} // namespace art