blob: 96b781745a9a1d05a66918ecdd996f34c3b1bcee [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 Carlstromf91c8c32011-09-21 17:30:34 -070022#include "ScopedUtfChars.h"
23
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25
26namespace art {
27
28namespace {
29
30// A smart pointer that provides read-only access to a Java string's UTF chars.
31// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
32// passed a null jstring. The correct idiom is:
33//
34// NullableScopedUtfChars name(env, javaName);
35// if (env->ExceptionOccurred()) {
36// return NULL;
37// }
38// // ... use name.c_str()
39//
40// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
41class NullableScopedUtfChars {
42public:
43 NullableScopedUtfChars(JNIEnv* env, jstring s)
44 : mEnv(env), mString(s)
45 {
46 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
47 }
48
49 ~NullableScopedUtfChars() {
50 if (mUtfChars) {
51 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
52 }
53 }
54
55 const char* c_str() const {
56 return mUtfChars;
57 }
58
59 size_t size() const {
60 return strlen(mUtfChars);
61 }
62
63 // Element access.
64 const char& operator[](size_t n) const {
65 return mUtfChars[n];
66 }
67
68private:
69 JNIEnv* mEnv;
70 jstring mString;
71 const char* mUtfChars;
72
73 // Disallow copy and assignment.
74 NullableScopedUtfChars(const NullableScopedUtfChars&);
75 void operator=(const NullableScopedUtfChars&);
76};
77
78static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName) {
79 ScopedUtfChars sourceName(env, javaSourceName);
80 if (sourceName.c_str() == NULL) {
81 return 0;
82 }
83 NullableScopedUtfChars outputName(env, javaOutputName);
84 if (env->ExceptionOccurred()) {
85 return 0;
86 }
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 const DexFile* dex_file = DexFile::Open(sourceName.c_str(), "");
88 if (dex_file == NULL) {
89 jniThrowExceptionFmt(env, "java/io/IOException", "unable to open DEX file: %s",
90 sourceName.c_str());
91 return NULL;
92 }
93 return static_cast<jint>(reinterpret_cast<uintptr_t>(dex_file));
94}
95
96static const DexFile* toDexFile(JNIEnv* env, int dex_file_address) {
97 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
98 if ((dex_file == NULL)) {
99 jniThrowNullPointerException(env, "dex_file == null");
100 }
101 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700102}
103
104void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700105 const DexFile* dex_file = toDexFile(env, cookie);
106 if (dex_file == NULL) {
107 return;
108 }
109 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
110 return;
111 }
112 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700113}
114
115jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700116 const DexFile* dex_file = toDexFile(env, cookie);
117 if (dex_file == NULL) {
118 return NULL;
119 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700120 ScopedUtfChars class_name(env, javaName);
121 if (class_name.c_str() == NULL) {
122 return NULL;
123 }
124 const std::string descriptor = DotToDescriptor(class_name.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700125 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
126 if (dex_class_def == NULL) {
Brian Carlstromdf143242011-10-10 18:05:34 -0700127 jniThrowExceptionFmt(env, "java/lang/NoClassDefFoundError", "Class %s not found",
128 class_name.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 return NULL;
130 }
131
132 Object* class_loader_object = Decode<Object*>(env, javaLoader);
133 ClassLoader* class_loader = down_cast<ClassLoader*>(class_loader_object);
134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
135 class_linker->RegisterDexFile(*dex_file);
136 Class* result = class_linker->DefineClass(descriptor, class_loader, *dex_file, *dex_class_def);
137 return AddLocalReference<jclass>(env, result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700138}
139
140jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 const DexFile* dex_file = toDexFile(env, cookie);
142 if (dex_file == NULL) {
143 return NULL;
144 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700145 UNIMPLEMENTED(ERROR);
146 return NULL;
147}
148
149jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
150 // TODO: run dex2oat?
151 UNIMPLEMENTED(WARNING);
152 return JNI_FALSE;
153}
154
155static JNINativeMethod gMethods[] = {
156 NATIVE_METHOD(DexFile, closeDexFile, "(I)V"),
157 NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"),
158 NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"),
159 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
160 NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"),
161};
162
163} // namespace
164
165void register_dalvik_system_DexFile(JNIEnv* env) {
166 jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods));
167}
168
169} // namespace art