Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 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 "logging.h" |
| 22 | #include "ScopedLocalRef.h" |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 23 | #include "thread.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | jclass WellKnownClasses::com_android_dex_Dex; |
| 28 | jclass WellKnownClasses::java_lang_ClassLoader; |
| 29 | jclass WellKnownClasses::java_lang_ClassNotFoundException; |
| 30 | jclass WellKnownClasses::java_lang_Daemons; |
| 31 | jclass WellKnownClasses::java_lang_Error; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 32 | jclass WellKnownClasses::java_lang_reflect_InvocationHandler; |
| 33 | jclass WellKnownClasses::java_lang_reflect_Method; |
| 34 | jclass WellKnownClasses::java_lang_reflect_Proxy; |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 35 | jclass WellKnownClasses::java_lang_RuntimeException; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 36 | jclass WellKnownClasses::java_lang_Thread; |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 37 | jclass WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler; |
| 38 | jclass WellKnownClasses::java_lang_ThreadGroup; |
| 39 | jclass WellKnownClasses::java_lang_ThreadLock; |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 40 | jclass WellKnownClasses::java_lang_Throwable; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 41 | jclass WellKnownClasses::java_nio_ReadWriteDirectByteBuffer; |
| 42 | jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk; |
| 43 | jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer; |
| 44 | |
| 45 | jmethodID WellKnownClasses::com_android_dex_Dex_create; |
| 46 | jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init; |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 47 | jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame^] | 48 | jmethodID WellKnownClasses::java_lang_Daemons_requestGC; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 49 | jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim; |
| 50 | jmethodID WellKnownClasses::java_lang_Daemons_start; |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 51 | jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add; |
| 52 | jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 53 | jmethodID WellKnownClasses::java_lang_reflect_InvocationHandler_invoke; |
| 54 | jmethodID WellKnownClasses::java_lang_Thread_init; |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 55 | jmethodID WellKnownClasses::java_lang_Thread_run; |
| 56 | jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException; |
| 57 | jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 58 | jmethodID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init; |
| 59 | jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast; |
| 60 | jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch; |
| 61 | |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 62 | jfieldID WellKnownClasses::java_lang_Thread_daemon; |
| 63 | jfieldID WellKnownClasses::java_lang_Thread_group; |
| 64 | jfieldID WellKnownClasses::java_lang_Thread_lock; |
| 65 | jfieldID WellKnownClasses::java_lang_Thread_name; |
| 66 | jfieldID WellKnownClasses::java_lang_Thread_priority; |
| 67 | jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler; |
| 68 | jfieldID WellKnownClasses::java_lang_Thread_vmData; |
| 69 | jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup; |
| 70 | jfieldID WellKnownClasses::java_lang_ThreadGroup_name; |
| 71 | jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup; |
| 72 | jfieldID WellKnownClasses::java_lang_ThreadLock_thread; |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 73 | jfieldID WellKnownClasses::java_lang_reflect_Proxy_h; |
| 74 | jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity; |
| 75 | jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress; |
| 76 | jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data; |
| 77 | jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length; |
| 78 | jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset; |
| 79 | jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type; |
| 80 | |
| 81 | static 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 Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 89 | static 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 Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 91 | if (fid == NULL) { |
| 92 | LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\""; |
| 93 | } |
| 94 | return fid; |
| 95 | } |
| 96 | |
| 97 | static 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 | |
| 105 | void 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 Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 111 | 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 Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 114 | java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException"); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 115 | java_lang_Thread = CacheClass(env, "java/lang/Thread"); |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 116 | 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 Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 119 | java_lang_Throwable = CacheClass(env, "java/lang/Throwable"); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 120 | 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 Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 126 | java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame^] | 127 | |
| 128 | java_lang_Daemons_requestGC = CacheMethod(env, java_lang_Daemons, true, "requestGC", "()V"); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 129 | 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 Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 131 | |
| 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 Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 137 | 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 Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 139 | 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 Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 142 | 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 Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 146 | 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 | |
| 166 | Class* WellKnownClasses::ToClass(jclass global_jclass) { |
| 167 | return reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(global_jclass)); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace art |