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 "java_lang_VMClassLoader.h"
18
19 #include "class_linker.h"
20 #include "jni_internal.h"
21 #include "mirror/class_loader.h"
22 #include "mirror/object-inl.h"
23 #include "scoped_fast_native_object_access.h"
24 #include "ScopedUtfChars.h"
25 #include "zip_archive.h"
26
27 namespace art {
28
VMClassLoader_findLoadedClass(JNIEnv * env,jclass,jobject javaLoader,jstring javaName)29 static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader,
30 jstring javaName) {
31 ScopedFastNativeObjectAccess soa(env);
32 mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
33 ScopedUtfChars name(env, javaName);
34 if (name.c_str() == nullptr) {
35 return nullptr;
36 }
37 ClassLinker* cl = Runtime::Current()->GetClassLinker();
38 std::string descriptor(DotToDescriptor(name.c_str()));
39 const size_t descriptor_hash = ComputeModifiedUtf8Hash(descriptor.c_str());
40 mirror::Class* c = cl->LookupClass(soa.Self(), descriptor.c_str(), descriptor_hash, loader);
41 if (c != nullptr && c->IsResolved()) {
42 return soa.AddLocalReference<jclass>(c);
43 }
44 // If class is erroneous, throw the earlier failure, wrapped in certain cases. See b/28787733.
45 if (c != nullptr && c->IsErroneous()) {
46 cl->ThrowEarlierClassFailure(c);
47 Thread* self = soa.Self();
48 mirror::Class* eiie_class =
49 self->DecodeJObject(WellKnownClasses::java_lang_ExceptionInInitializerError)->AsClass();
50 mirror::Class* iae_class =
51 self->DecodeJObject(WellKnownClasses::java_lang_IllegalAccessError)->AsClass();
52 mirror::Class* ncdfe_class =
53 self->DecodeJObject(WellKnownClasses::java_lang_NoClassDefFoundError)->AsClass();
54 mirror::Class* exception = self->GetException()->GetClass();
55 if (exception == eiie_class || exception == iae_class || exception == ncdfe_class) {
56 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
57 PrettyDescriptor(c).c_str());
58 }
59 return nullptr;
60 }
61 if (loader != nullptr) {
62 // Try the common case.
63 StackHandleScope<1> hs(soa.Self());
64 cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), descriptor_hash,
65 hs.NewHandle(loader), &c);
66 if (c != nullptr) {
67 return soa.AddLocalReference<jclass>(c);
68 }
69 }
70 // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
71 // the regular loadClass code.
72 return nullptr;
73 }
74
75 /*
76 * Returns an array of entries from the boot classpath that could contain resources.
77 */
VMClassLoader_getBootClassPathEntries(JNIEnv * env,jclass)78 static jobjectArray VMClassLoader_getBootClassPathEntries(JNIEnv* env, jclass) {
79 const std::vector<const DexFile*>& path =
80 Runtime::Current()->GetClassLinker()->GetBootClassPath();
81 jclass stringClass = env->FindClass("java/lang/String");
82 jobjectArray array = env->NewObjectArray(path.size(), stringClass, nullptr);
83 for (size_t i = 0; i < path.size(); ++i) {
84 const DexFile* dex_file = path[i];
85
86 // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
87 const std::string& location(dex_file->GetBaseLocation());
88
89 jstring javaPath = env->NewStringUTF(location.c_str());
90 env->SetObjectArrayElement(array, i, javaPath);
91 }
92 return array;
93 }
94
95 static JNINativeMethod gMethods[] = {
96 NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
97 NATIVE_METHOD(VMClassLoader, getBootClassPathEntries, "()[Ljava/lang/String;"),
98 };
99
register_java_lang_VMClassLoader(JNIEnv * env)100 void register_java_lang_VMClassLoader(JNIEnv* env) {
101 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
102 }
103
104 } // namespace art
105