Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include "class_linker.h" |
| 18 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "mirror/class_loader.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 20 | #include "mirror/object-inl.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 21 | #include "scoped_fast_native_object_access.h" |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 22 | #include "ScopedUtfChars.h" |
| 23 | #include "zip_archive.h" |
| 24 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 27 | static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 28 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 30 | ScopedUtfChars name(env, javaName); |
| 31 | if (name.c_str() == NULL) { |
| 32 | return NULL; |
| 33 | } |
| 34 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 35 | std::string descriptor(DotToDescriptor(name.c_str())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | mirror::Class* c = Runtime::Current()->GetClassLinker()->LookupClass(descriptor.c_str(), loader); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 37 | if (c != NULL && c->IsResolved()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | return soa.AddLocalReference<jclass>(c); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 39 | } else { |
| 40 | // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into |
| 41 | // the regular loadClass code. |
| 42 | return NULL; |
| 43 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 46 | static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 47 | return Runtime::Current()->GetClassLinker()->GetBootClassPath().size(); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Returns a string URL for a resource with the specified 'javaName' in |
| 52 | * entry 'index' of the boot class path. |
| 53 | * |
| 54 | * We return a newly-allocated String in the following form: |
| 55 | * |
| 56 | * jar:file://path!/name |
| 57 | * |
| 58 | * Where "path" is the bootstrap class path entry and "name" is the string |
| 59 | * passed into this method. "path" needs to be an absolute path (starting |
| 60 | * with '/'); if it's not we'd need to make it absolute as part of forming |
| 61 | * the URL string. |
| 62 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 63 | static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 64 | ScopedUtfChars name(env, javaName); |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 65 | if (name.c_str() == nullptr) { |
| 66 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const std::vector<const DexFile*>& path = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 70 | if (index < 0 || size_t(index) >= path.size()) { |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 71 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 72 | } |
| 73 | const DexFile* dex_file = path[index]; |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 74 | |
| 75 | // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar. |
| 76 | const std::string& location(dex_file->GetBaseLocation()); |
| 77 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 78 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 79 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(location.c_str(), &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 80 | if (zip_archive.get() == nullptr) { |
| 81 | LOG(WARNING) << "Failed to open zip archive '" << location << "': " << error_msg; |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 82 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 83 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 84 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(name.c_str(), &error_msg)); |
Andreas Gampe | cb8f9e8 | 2014-07-24 15:35:50 -0700 | [diff] [blame] | 85 | if (zip_entry.get() == nullptr) { |
| 86 | return nullptr; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::string url; |
| 90 | StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str()); |
| 91 | return env->NewStringUTF(url.c_str()); |
| 92 | } |
| 93 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 94 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 95 | NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"), |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 96 | NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 97 | NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "!()I"), |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 100 | void register_java_lang_VMClassLoader(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 101 | REGISTER_NATIVE_METHODS("java/lang/VMClassLoader"); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace art |