blob: 62a0b7653d7ef7047cc37ff9599a05960117c9be [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
Igor Murashkinbc1d78d2015-07-30 16:39:45 -070019#include "base/out.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070020#include "class_linker.h"
21#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class_loader.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "mirror/object-inl.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070024#include "scoped_fast_native_object_access.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070025#include "ScopedUtfChars.h"
26#include "zip_archive.h"
27
Elliott Hughes64bf5a32011-09-20 14:43:12 -070028namespace art {
29
Ian Rogers7b078e82014-09-10 14:44:24 -070030static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
31 jstring javaName) {
Ian Rogers53b8b092014-03-13 23:45:53 -070032 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070034 ScopedUtfChars name(env, javaName);
Ian Rogers7b078e82014-09-10 14:44:24 -070035 if (name.c_str() == nullptr) {
36 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070037 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070038 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Brian Carlstromf91c8c32011-09-21 17:30:34 -070039 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -080040 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
41 mirror::Class* c = cl->LookupClass(soa.Self(), descriptor.c_str(), descriptor_hash, loader);
Ian Rogers7b078e82014-09-10 14:44:24 -070042 if (c != nullptr && c->IsResolved()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 return soa.AddLocalReference<jclass>(c);
Ian Rogersbe125a92012-01-11 15:19:49 -080044 }
Mathieu Chartierab0ed822014-09-11 14:21:41 -070045 if (loader != nullptr) {
46 // Try the common case.
47 StackHandleScope<1> hs(soa.Self());
Andreas Gampef865ea92015-04-13 22:14:19 -070048 cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), descriptor_hash,
Igor Murashkinbc1d78d2015-07-30 16:39:45 -070049 hs.NewHandle(loader), outof(c));
Mathieu Chartierab0ed822014-09-11 14:21:41 -070050 if (c != nullptr) {
51 return soa.AddLocalReference<jclass>(c);
52 }
53 }
54 // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
55 // the regular loadClass code.
Ian Rogers7b078e82014-09-10 14:44:24 -070056 return nullptr;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070057}
58
Elliott Hughes64bf5a32011-09-20 14:43:12 -070059/*
Neil Fulleref486052015-06-04 12:24:08 +000060 * Returns an array of entries from the boot classpath that could contain resources.
Elliott Hughes64bf5a32011-09-20 14:43:12 -070061 */
Neil Fulleref486052015-06-04 12:24:08 +000062static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
Ian Rogers7b078e82014-09-10 14:44:24 -070063 const std::vector<const DexFile*>& path =
64 Runtime::Current()->GetClassLinker()->GetBootClassPath();
Neil Fulleref486052015-06-04 12:24:08 +000065 jclass stringClass = env->FindClass("java/lang/String");
66 jobjectArray array = env->NewObjectArray(path.size(), stringClass, nullptr);
67 for (size_t i = 0; i < path.size(); ++i) {
68 const DexFile* dex_file = path[i];
Neil Fuller1e27c5b2015-06-03 15:46:29 +000069
Neil Fulleref486052015-06-04 12:24:08 +000070 // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
71 const std::string& location(dex_file->GetBaseLocation());
Neil Fuller1e27c5b2015-06-03 15:46:29 +000072
Neil Fulleref486052015-06-04 12:24:08 +000073 jstring javaPath = env->NewStringUTF(location.c_str());
74 env->SetObjectArrayElement(array, i, javaPath);
Neil Fuller1e27c5b2015-06-03 15:46:29 +000075 }
Neil Fulleref486052015-06-04 12:24:08 +000076 return array;
Elliott Hughes64bf5a32011-09-20 14:43:12 -070077}
78
Elliott Hughes64bf5a32011-09-20 14:43:12 -070079static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -070080 NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
Neil Fulleref486052015-06-04 12:24:08 +000081 NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -070082};
83
Elliott Hughes64bf5a32011-09-20 14:43:12 -070084void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070085 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -070086}
87
88} // namespace art