blob: cad1acdda3266e6dd09c37b98d49455ae9f6da7f [file] [log] [blame]
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001/*
2 * Copyright (C) 2008 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
Brian Carlstromaded5f72011-10-07 17:15:04 -070017#include "class_loader.h"
18#include "class_linker.h"
19#include "dex_file.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070020#include "logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070021#include "runtime.h"
Brian Carlstrom03a20ba2011-10-13 10:24:13 -070022#include "toStringArray.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070023#include "ScopedUtfChars.h"
24
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26
27namespace art {
28
29namespace {
30
31// A smart pointer that provides read-only access to a Java string's UTF chars.
32// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
33// passed a null jstring. The correct idiom is:
34//
35// NullableScopedUtfChars name(env, javaName);
36// if (env->ExceptionOccurred()) {
37// return NULL;
38// }
39// // ... use name.c_str()
40//
41// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
42class NullableScopedUtfChars {
43public:
44 NullableScopedUtfChars(JNIEnv* env, jstring s)
45 : mEnv(env), mString(s)
46 {
47 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
48 }
49
50 ~NullableScopedUtfChars() {
51 if (mUtfChars) {
52 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
53 }
54 }
55
56 const char* c_str() const {
57 return mUtfChars;
58 }
59
60 size_t size() const {
61 return strlen(mUtfChars);
62 }
63
64 // Element access.
65 const char& operator[](size_t n) const {
66 return mUtfChars[n];
67 }
68
69private:
70 JNIEnv* mEnv;
71 jstring mString;
72 const char* mUtfChars;
73
74 // Disallow copy and assignment.
75 NullableScopedUtfChars(const NullableScopedUtfChars&);
76 void operator=(const NullableScopedUtfChars&);
77};
78
79static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName) {
80 ScopedUtfChars sourceName(env, javaSourceName);
81 if (sourceName.c_str() == NULL) {
82 return 0;
83 }
84 NullableScopedUtfChars outputName(env, javaOutputName);
85 if (env->ExceptionOccurred()) {
86 return 0;
87 }
Brian Carlstromaded5f72011-10-07 17:15:04 -070088 const DexFile* dex_file = DexFile::Open(sourceName.c_str(), "");
89 if (dex_file == NULL) {
90 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open DEX file: %s",
91 sourceName.c_str());
92 return NULL;
93 }
94 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
95}
96
97static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
98 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
99 if ((dex_file == NULL)) {
100 jniThrowNullPointerException(env, "dex_file == null");
101 }
102 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700103}
104
105void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106 const DexFile* dex_file = toDexFile(env, cookie);
107 if (dex_file == NULL) {
108 return;
109 }
110 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
111 return;
112 }
113 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114}
115
116jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700117 const DexFile* dex_file = toDexFile(env, cookie);
118 if (dex_file == NULL) {
119 return NULL;
120 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700121 ScopedUtfChars class_name(env, javaName);
122 if (class_name.c_str() == NULL) {
123 return NULL;
124 }
125 const std::string descriptor = DotToDescriptor(class_name.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700126 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
127 if (dex_class_def == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128 return NULL;
129 }
130
131 Object* class_loader_object = Decode<Object*>(env, javaLoader);
132 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
133 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
134 class_linker->RegisterDexFile(*dex_file);
135 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
Brian Carlstrom41ec4772011-10-11 01:35:58 -0700136 if (env->ExceptionCheck()) {
137 env->ExceptionClear();
138 return NULL;
139 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700141}
142
143jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700144 const DexFile* dex_file = toDexFile(env, cookie);
145 if (dex_file == NULL) {
146 return NULL;
147 }
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700148
149 std::vector<std::string> class_names;
150 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
151 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
152 const char* descriptor = dex_file->GetClassDescriptor(class_def);
153 class_names.push_back(DescriptorToDot(descriptor));
154 }
155 return toStringArray(env, class_names);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700156}
157
158jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
Brian Carlstrom03a20ba2011-10-13 10:24:13 -0700159 ScopedUtfChars filename(env, javaFilename);
160 if (filename.c_str() == NULL) {
161 return JNI_FALSE;
162 }
163 // TODO: return true if we need to extract dex or run dex2oat
164 UNIMPLEMENTED(WARNING) << filename.c_str();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700165 return JNI_FALSE;
166}
167
168static JNINativeMethod gMethods[] = {
169 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
170 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
171 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
172 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
173 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
174};
175
176} // namespace
177
178void register_dalvik_system_DexFile(JNIEnv* env) {
179 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
180}
181
182} // namespace art