blob: 1b210bb4c3934f5b51edf3f5b878f42136ee3788 [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_DexFile.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Igor Murashkinbc1d78d2015-07-30 16:39:45 -070020#include "base/out.h"
Andreas Gampe833a4852014-05-21 18:46:59 -070021#include "base/stl_util.h"
Andreas Gampe20c89302014-08-19 17:28:06 -070022#include "base/stringprintf.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070023#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080024#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080028#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/string.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080030#include "oat_file_assistant.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070031#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030032#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070033#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080035#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070036#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010037#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070038#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070039#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040
41namespace art {
42
Andreas Gampe324b9bb2015-02-23 16:33:22 -080043static std::unique_ptr<std::vector<const DexFile*>>
44ConvertJavaArrayToNative(JNIEnv* env, jobject arrayObject) {
45 jarray array = reinterpret_cast<jarray>(arrayObject);
46
47 jsize array_size = env->GetArrayLength(array);
48 if (env->ExceptionCheck() == JNI_TRUE) {
49 return std::unique_ptr<std::vector<const DexFile*>>();
50 }
51
52 // TODO: Optimize. On 32bit we can use an int array.
53 jboolean is_long_data_copied;
54 jlong* long_data = env->GetLongArrayElements(reinterpret_cast<jlongArray>(array),
55 &is_long_data_copied);
56 if (env->ExceptionCheck() == JNI_TRUE) {
57 return std::unique_ptr<std::vector<const DexFile*>>();
58 }
59
60 std::unique_ptr<std::vector<const DexFile*>> ret(new std::vector<const DexFile*>());
61 ret->reserve(array_size);
62 for (jsize i = 0; i < array_size; ++i) {
63 ret->push_back(reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(*(long_data + i))));
64 }
65
66 env->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), long_data, JNI_ABORT);
67 if (env->ExceptionCheck() == JNI_TRUE) {
68 return std::unique_ptr<std::vector<const DexFile*>>();
69 }
70
71 return ret;
72}
73
74static jlongArray ConvertNativeToJavaArray(JNIEnv* env,
75 std::vector<std::unique_ptr<const DexFile>>& vec) {
76 size_t vec_size = vec.size();
77 jlongArray long_array = env->NewLongArray(static_cast<jsize>(vec_size));
78 if (env->ExceptionCheck() == JNI_TRUE) {
79 return nullptr;
80 }
81
82 jboolean is_long_data_copied;
83 jlong* long_data = env->GetLongArrayElements(long_array, &is_long_data_copied);
84 if (env->ExceptionCheck() == JNI_TRUE) {
85 return nullptr;
86 }
87
88 jlong* tmp = long_data;
89 for (auto& dex_file : vec) {
90 *tmp = reinterpret_cast<uintptr_t>(dex_file.get());
91 tmp++;
92 }
93
94 env->ReleaseLongArrayElements(long_array, long_data, 0);
95 if (env->ExceptionCheck() == JNI_TRUE) {
96 return nullptr;
97 }
98
99 // Now release all the unique_ptrs.
100 for (auto& dex_file : vec) {
101 dex_file.release();
102 }
103
104 return long_array;
105}
106
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700107// A smart pointer that provides read-only access to a Java string's UTF chars.
108// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
109// passed a null jstring. The correct idiom is:
110//
111// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700112// if (env->ExceptionCheck()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700113// return null;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700114// }
115// // ... use name.c_str()
116//
117// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
118class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800119 public:
120 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 mUtfChars = (s != nullptr) ? env->GetStringUTFChars(s, nullptr) : nullptr;
Elliott Hughesba8eee12012-01-24 20:25:24 -0800122 }
123
124 ~NullableScopedUtfChars() {
125 if (mUtfChars) {
126 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700127 }
Elliott Hughesba8eee12012-01-24 20:25:24 -0800128 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700129
Elliott Hughesba8eee12012-01-24 20:25:24 -0800130 const char* c_str() const {
131 return mUtfChars;
132 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700133
Elliott Hughesba8eee12012-01-24 20:25:24 -0800134 size_t size() const {
135 return strlen(mUtfChars);
136 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700137
Elliott Hughesba8eee12012-01-24 20:25:24 -0800138 // Element access.
139 const char& operator[](size_t n) const {
140 return mUtfChars[n];
141 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700142
Elliott Hughesba8eee12012-01-24 20:25:24 -0800143 private:
144 JNIEnv* mEnv;
145 jstring mString;
146 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700147
Elliott Hughesba8eee12012-01-24 20:25:24 -0800148 // Disallow copy and assignment.
149 NullableScopedUtfChars(const NullableScopedUtfChars&);
150 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700151};
152
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700153static jobject DexFile_openDexFileNative(
154 JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700155 ScopedUtfChars sourceName(env, javaSourceName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 if (sourceName.c_str() == nullptr) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700157 return 0;
158 }
159 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700160 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700161 return 0;
162 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700163
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700164 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800165 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe833a4852014-05-21 18:46:59 -0700166 std::vector<std::string> error_msgs;
167
Igor Murashkinbc1d78d2015-07-30 16:39:45 -0700168 dex_files =
169 linker->OpenDexFilesFromOat(sourceName.c_str(), outputName.c_str(), outof(error_msgs));
Andreas Gampe833a4852014-05-21 18:46:59 -0700170
Richard Uhler66d874d2015-01-15 09:37:19 -0800171 if (!dex_files.empty()) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800172 jlongArray array = ConvertNativeToJavaArray(env, dex_files);
173 if (array == nullptr) {
174 ScopedObjectAccess soa(env);
175 for (auto& dex_file : dex_files) {
176 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
177 dex_file.release();
178 }
179 }
180 }
181 return array;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700182 } else {
Vladimir Marko60836d52014-01-16 15:53:38 +0000183 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700184 CHECK(!error_msgs.empty());
185 // The most important message is at the end. So set up nesting by going forward, which will
186 // wrap the existing exception as a cause for the following one.
187 auto it = error_msgs.begin();
188 auto itEnd = error_msgs.end();
189 for ( ; it != itEnd; ++it) {
190 ThrowWrappedIOException("%s", it->c_str());
191 }
192
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800193 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195}
196
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800197static void DexFile_closeDexFile(JNIEnv* env, jclass, jobject cookie) {
198 std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie);
Andreas Gampe833a4852014-05-21 18:46:59 -0700199 if (dex_files.get() == nullptr) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800200 DCHECK(env->ExceptionCheck());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 return;
202 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800203
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700204 ScopedObjectAccess soa(env);
Andreas Gampe833a4852014-05-21 18:46:59 -0700205
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800206 // The Runtime currently never unloads classes, which means any registered
207 // dex files must be kept around forever in case they are used. We
208 // accomplish this here by explicitly leaking those dex files that are
209 // registered.
210 //
211 // TODO: The Runtime should support unloading of classes and freeing of the
212 // dex files for those unloaded classes rather than leaking dex files here.
213 for (auto& dex_file : *dex_files) {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800214 if (!Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
215 delete dex_file;
Andreas Gampe833a4852014-05-21 18:46:59 -0700216 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700217 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700218}
219
Elliott Hughes0512f022012-03-15 22:10:52 -0700220static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800221 jobject cookie) {
222 std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie);
223 if (dex_files.get() == nullptr) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700224 VLOG(class_linker) << "Failed to find dex_file";
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800225 DCHECK(env->ExceptionCheck());
226 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700227 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800228
Brian Carlstromdf143242011-10-10 18:05:34 -0700229 ScopedUtfChars class_name(env, javaName);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700230 if (class_name.c_str() == nullptr) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700231 VLOG(class_linker) << "Failed to find class_name";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700232 return nullptr;
Brian Carlstromdf143242011-10-10 18:05:34 -0700233 }
Elliott Hughes95572412011-12-13 18:14:20 -0800234 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800235 const size_t hash(ComputeModifiedUtf8Hash(descriptor.c_str()));
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800236 for (auto& dex_file : *dex_files) {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800237 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str(), hash);
Andreas Gampe833a4852014-05-21 18:46:59 -0700238 if (dex_class_def != nullptr) {
239 ScopedObjectAccess soa(env);
240 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
241 class_linker->RegisterDexFile(*dex_file);
242 StackHandleScope<1> hs(soa.Self());
243 Handle<mirror::ClassLoader> class_loader(
244 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800245 mirror::Class* result = class_linker->DefineClass(soa.Self(), descriptor.c_str(), hash,
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 class_loader, *dex_file, *dex_class_def);
Andreas Gampe833a4852014-05-21 18:46:59 -0700247 if (result != nullptr) {
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700248 VLOG(class_linker) << "DexFile_defineClassNative returning " << result
249 << " for " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700250 return soa.AddLocalReference<jclass>(result);
251 }
252 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700253 }
Brian Carlstrom667ab7c2014-10-16 19:12:28 -0700254 VLOG(class_linker) << "Failed to find dex_class_def " << class_name.c_str();
Andreas Gampe833a4852014-05-21 18:46:59 -0700255 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700256}
257
Andreas Gampe833a4852014-05-21 18:46:59 -0700258// Needed as a compare functor for sets of const char
259struct CharPointerComparator {
260 bool operator()(const char *str1, const char *str2) const {
261 return strcmp(str1, str2) < 0;
262 }
263};
264
265// Note: this can be an expensive call, as we sort out duplicates in MultiDex files.
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800266static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jobject cookie) {
267 std::unique_ptr<std::vector<const DexFile*>> dex_files = ConvertJavaArrayToNative(env, cookie);
268 if (dex_files.get() == nullptr) {
269 DCHECK(env->ExceptionCheck());
270 return nullptr;
271 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700272
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800273 // Push all class descriptors into a set. Use set instead of unordered_set as we want to
274 // retrieve all in the end.
275 std::set<const char*, CharPointerComparator> descriptors;
276 for (auto& dex_file : *dex_files) {
277 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
278 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
279 const char* descriptor = dex_file->GetClassDescriptor(class_def);
280 descriptors.insert(descriptor);
Andreas Gampe833a4852014-05-21 18:46:59 -0700281 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800282 }
Andreas Gampe833a4852014-05-21 18:46:59 -0700283
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800284 // Now create output array and copy the set into it.
285 jobjectArray result = env->NewObjectArray(descriptors.size(), WellKnownClasses::java_lang_String,
286 nullptr);
287 if (result != nullptr) {
288 auto it = descriptors.begin();
289 auto it_end = descriptors.end();
290 jsize i = 0;
291 for (; it != it_end; it++, ++i) {
292 std::string descriptor(DescriptorToDot(*it));
293 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
294 if (jdescriptor.get() == nullptr) {
295 return nullptr;
Ian Rogersdd157d72014-05-15 14:47:50 -0700296 }
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800297 env->SetObjectArrayElement(result, i, jdescriptor.get());
Ian Rogersdd157d72014-05-15 14:47:50 -0700298 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700299 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700300 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700301}
302
Richard Uhler95abd042015-03-24 09:51:28 -0700303static jint GetDexOptNeeded(JNIEnv* env, const char* filename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100304 const char* pkgname, const char* instruction_set, const jboolean defer) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800305
Narayan Kamath11d9f062014-04-23 20:24:57 +0100306 if ((filename == nullptr) || !OS::FileExists(filename)) {
Richard Uhler95abd042015-03-24 09:51:28 -0700307 LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700308 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100309 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700310 env->ThrowNew(fnfe.get(), message);
Richard Uhler95abd042015-03-24 09:51:28 -0700311 return OatFileAssistant::kNoDexOptNeeded;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700312 }
313
Alex Light6e183f22014-07-18 14:57:04 -0700314 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
Andreas Gampe20c89302014-08-19 17:28:06 -0700315 if (target_instruction_set == kNone) {
316 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
317 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set));
318 env->ThrowNew(iae.get(), message.c_str());
319 return 0;
320 }
Alex Light6e183f22014-07-18 14:57:04 -0700321
Richard Uhler66d874d2015-01-15 09:37:19 -0800322 // TODO: Verify the dex location is well formed, and throw an IOException if
323 // not?
324
325 OatFileAssistant oat_file_assistant(filename, target_instruction_set, false, pkgname);
326
327 // Always treat elements of the bootclasspath as up-to-date.
328 if (oat_file_assistant.IsInBootClassPath()) {
Richard Uhler95abd042015-03-24 09:51:28 -0700329 return OatFileAssistant::kNoDexOptNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800330 }
331
332 // TODO: Checking the profile should probably be done in the GetStatus()
333 // function. We have it here because GetStatus() should not be copying
334 // profile files. But who should be copying profile files?
335 if (oat_file_assistant.OdexFileIsOutOfDate()) {
336 // Needs recompile if profile has changed significantly.
337 if (Runtime::Current()->GetProfilerOptions().IsEnabled()) {
338 if (oat_file_assistant.IsProfileChangeSignificant()) {
339 if (!defer) {
340 oat_file_assistant.CopyProfileFile();
341 }
Richard Uhler95abd042015-03-24 09:51:28 -0700342 return OatFileAssistant::kDex2OatNeeded;
Richard Uhler66d874d2015-01-15 09:37:19 -0800343 } else if (oat_file_assistant.ProfileExists()
344 && !oat_file_assistant.OldProfileExists()) {
345 if (!defer) {
346 oat_file_assistant.CopyProfileFile();
347 }
348 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700349 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800350 }
351
Richard Uhler95abd042015-03-24 09:51:28 -0700352 return oat_file_assistant.GetDexOptNeeded();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700353}
354
Richard Uhler95abd042015-03-24 09:51:28 -0700355static jint DexFile_getDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename,
Narayan Kamath11d9f062014-04-23 20:24:57 +0100356 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
357 ScopedUtfChars filename(env, javaFilename);
Andreas Gampe20c89302014-08-19 17:28:06 -0700358 if (env->ExceptionCheck()) {
359 return 0;
360 }
361
Narayan Kamath11d9f062014-04-23 20:24:57 +0100362 NullableScopedUtfChars pkgname(env, javaPkgname);
Andreas Gampe20c89302014-08-19 17:28:06 -0700363
Narayan Kamath11d9f062014-04-23 20:24:57 +0100364 ScopedUtfChars instruction_set(env, javaInstructionSet);
Andreas Gampe20c89302014-08-19 17:28:06 -0700365 if (env->ExceptionCheck()) {
366 return 0;
367 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100368
Richard Uhler95abd042015-03-24 09:51:28 -0700369 return GetDexOptNeeded(env, filename.c_str(), pkgname.c_str(),
370 instruction_set.c_str(), defer);
Narayan Kamath11d9f062014-04-23 20:24:57 +0100371}
372
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700373// public API, null pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100374static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
375 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
376 ScopedUtfChars filename(env, javaFilename);
Richard Uhler95abd042015-03-24 09:51:28 -0700377 jint status = GetDexOptNeeded(env, filename.c_str(), nullptr /* pkgname */,
378 instruction_set, false /* defer */);
379 return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800380}
381
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700382static JNINativeMethod gMethods[] = {
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800383 NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)V"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700384 NATIVE_METHOD(DexFile, defineClassNative,
385 "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Object;)Ljava/lang/Class;"),
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800386 NATIVE_METHOD(DexFile, getClassNameList, "(Ljava/lang/Object;)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700387 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700388 NATIVE_METHOD(DexFile, getDexOptNeeded,
389 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)I"),
390 NATIVE_METHOD(DexFile, openDexFileNative,
391 "(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/Object;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700392};
393
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700394void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700395 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700396}
397
398} // namespace art