blob: 45563d299c7869e46b51268da1075b5315a8bbe1 [file] [log] [blame]
Elliott Hughes64bf5a32011-09-20 14:43:12 -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 "java_lang_VMClassLoader.h"
18
Elliott Hughes64bf5a32011-09-20 14:43:12 -070019#include "class_linker.h"
20#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "mirror/object-inl.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070023#include "scoped_fast_native_object_access.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070024#include "ScopedUtfChars.h"
25#include "zip_archive.h"
26
Elliott Hughes64bf5a32011-09-20 14:43:12 -070027namespace art {
28
Ian Rogers7b078e82014-09-10 14:44:24 -070029static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
30 jstring javaName) {
Ian Rogers53b8b092014-03-13 23:45:53 -070031 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032 mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070033 ScopedUtfChars name(env, javaName);
Ian Rogers7b078e82014-09-10 14:44:24 -070034 if (name.c_str() == nullptr) {
35 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070036 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070037 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Brian Carlstromf91c8c32011-09-21 17:30:34 -070038 std::string descriptor(DotToDescriptor(name.c_str()));
Ian Rogers7b078e82014-09-10 14:44:24 -070039 mirror::Class* c = cl->LookupClass(soa.Self(), descriptor.c_str(), loader);
40 if (c != nullptr && c->IsResolved()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 return soa.AddLocalReference<jclass>(c);
Ian Rogersbe125a92012-01-11 15:19:49 -080042 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070043 if (loader != nullptr) {
44 // Try the common case.
45 StackHandleScope<1> hs(soa.Self());
46 c = cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), hs.NewHandle(loader));
47 if (c != nullptr) {
48 return soa.AddLocalReference<jclass>(c);
49 }
50 }
51 // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
52 // the regular loadClass code.
Ian Rogers7b078e82014-09-10 14:44:24 -070053 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070054}
55
Elliott Hughes1bac54f2012-03-16 12:48:31 -070056static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070057 return Runtime::Current()->GetClassLinker()->GetBootClassPath().size();
58}
59
60/*
61 * Returns a string URL for a resource with the specified 'javaName' in
62 * entry 'index' of the boot class path.
63 *
64 * We return a newly-allocated String in the following form:
65 *
66 * jar:file://path!/name
67 *
68 * Where "path" is the bootstrap class path entry and "name" is the string
69 * passed into this method. "path" needs to be an absolute path (starting
70 * with '/'); if it's not we'd need to make it absolute as part of forming
71 * the URL string.
72 */
Ian Rogers7b078e82014-09-10 14:44:24 -070073static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName,
74 jint index) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070075 ScopedUtfChars name(env, javaName);
Andreas Gampecb8f9e82014-07-24 15:35:50 -070076 if (name.c_str() == nullptr) {
77 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070078 }
79
Ian Rogers7b078e82014-09-10 14:44:24 -070080 const std::vector<const DexFile*>& path =
81 Runtime::Current()->GetClassLinker()->GetBootClassPath();
Elliott Hughes64bf5a32011-09-20 14:43:12 -070082 if (index < 0 || size_t(index) >= path.size()) {
Andreas Gampecb8f9e82014-07-24 15:35:50 -070083 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070084 }
85 const DexFile* dex_file = path[index];
Andreas Gampecb8f9e82014-07-24 15:35:50 -070086
87 // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
88 const std::string& location(dex_file->GetBaseLocation());
89
Ian Rogers8d31bbd2013-10-13 10:44:14 -070090 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070091 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(location.c_str(), &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 if (zip_archive.get() == nullptr) {
93 LOG(WARNING) << "Failed to open zip archive '" << location << "': " << error_msg;
Andreas Gampecb8f9e82014-07-24 15:35:50 -070094 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070095 }
Ian Rogers700a4022014-05-19 16:49:03 -070096 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(name.c_str(), &error_msg));
Andreas Gampecb8f9e82014-07-24 15:35:50 -070097 if (zip_entry.get() == nullptr) {
98 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070099 }
100
101 std::string url;
102 StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str());
103 return env->NewStringUTF(url.c_str());
104}
105
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700106static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700107 NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700108 NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700109 NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "!()I"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700110};
111
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700112void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700113 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700114}
115
116} // namespace art