blob: 7e21a66603935d37405c8ebb3d0e4b1a92999962 [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"
23
24namespace art {
25
26jclass WellKnownClasses::com_android_dex_Dex;
27jclass WellKnownClasses::java_lang_ClassLoader;
28jclass WellKnownClasses::java_lang_ClassNotFoundException;
29jclass WellKnownClasses::java_lang_Daemons;
30jclass WellKnownClasses::java_lang_Error;
31jclass WellKnownClasses::java_lang_ExceptionInInitializerError;
32jclass WellKnownClasses::java_lang_reflect_InvocationHandler;
33jclass WellKnownClasses::java_lang_reflect_Method;
34jclass WellKnownClasses::java_lang_reflect_Proxy;
35jclass WellKnownClasses::java_lang_reflect_UndeclaredThrowableException;
36jclass WellKnownClasses::java_lang_Thread;
37jclass WellKnownClasses::java_nio_ReadWriteDirectByteBuffer;
38jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
39jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
40
41jmethodID WellKnownClasses::com_android_dex_Dex_create;
42jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
43jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
44jmethodID WellKnownClasses::java_lang_Daemons_start;
45jmethodID WellKnownClasses::java_lang_reflect_InvocationHandler_invoke;
46jmethodID WellKnownClasses::java_lang_Thread_init;
47jmethodID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_init;
48jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
49jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
50
51jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
52jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_capacity;
53jfieldID WellKnownClasses::java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress;
54jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
55jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
56jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
57jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
58
59static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
60 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
61 if (c.get() == NULL) {
62 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
63 }
64 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
65}
66
67static jfieldID CacheField(JNIEnv* env, jclass c, const char* name, const char* signature) {
68 jfieldID fid = env->GetFieldID(c, name, signature);
69 if (fid == NULL) {
70 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\"";
71 }
72 return fid;
73}
74
75static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
76 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) : env->GetMethodID(c, name, signature);
77 if (mid == NULL) {
78 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\"";
79 }
80 return mid;
81}
82
83void WellKnownClasses::Init(JNIEnv* env) {
84 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
85 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
86 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
87 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
88 java_lang_Error = CacheClass(env, "java/lang/Error");
89 java_lang_ExceptionInInitializerError = CacheClass(env, "java/lang/ExceptionInInitializerError");
90 java_lang_reflect_InvocationHandler = CacheClass(env, "java/lang/reflect/InvocationHandler");
91 java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
92 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
93 java_lang_reflect_UndeclaredThrowableException = CacheClass(env, "java/lang/reflect/UndeclaredThrowableException");
94 java_lang_Thread = CacheClass(env, "java/lang/Thread");
95 java_nio_ReadWriteDirectByteBuffer = CacheClass(env, "java/nio/ReadWriteDirectByteBuffer");
96 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
97 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
98
99 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
100 java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
101 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
102 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
103 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;");
104 java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
105 java_nio_ReadWriteDirectByteBuffer_init = CacheMethod(env, java_nio_ReadWriteDirectByteBuffer, false, "<init>", "(II)V");
106 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
107 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;");
108
109 java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, "h", "Ljava/lang/reflect/InvocationHandler;");
110 java_nio_ReadWriteDirectByteBuffer_capacity = CacheField(env, java_nio_ReadWriteDirectByteBuffer, "capacity", "I");
111 java_nio_ReadWriteDirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_ReadWriteDirectByteBuffer, "effectiveDirectAddress", "I");
112 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, "data", "[B");
113 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, "length", "I");
114 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, "offset", "I");
115 org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, "type", "I");
116}
117
118} // namespace art