blob: 02b7c25523c07ca14935272fe588f0d6d30da09f [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
17#include "class_linker.h"
18#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/class_loader.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070020#include "scoped_thread_state_change.h"
Elliott Hughes64bf5a32011-09-20 14:43:12 -070021#include "ScopedUtfChars.h"
22#include "zip_archive.h"
23
Elliott Hughes64bf5a32011-09-20 14:43:12 -070024namespace art {
25
Elliott Hughes0512f022012-03-15 22:10:52 -070026static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028 mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
Elliott Hughes64bf5a32011-09-20 14:43:12 -070029 ScopedUtfChars name(env, javaName);
30 if (name.c_str() == NULL) {
31 return NULL;
32 }
33
Brian Carlstromf91c8c32011-09-21 17:30:34 -070034 std::string descriptor(DotToDescriptor(name.c_str()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 mirror::Class* c = Runtime::Current()->GetClassLinker()->LookupClass(descriptor.c_str(), loader);
Ian Rogersbe125a92012-01-11 15:19:49 -080036 if (c != NULL && c->IsResolved()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037 return soa.AddLocalReference<jclass>(c);
Ian Rogersbe125a92012-01-11 15:19:49 -080038 } else {
39 // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
40 // the regular loadClass code.
41 return NULL;
42 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -070043}
44
Elliott Hughes1bac54f2012-03-16 12:48:31 -070045static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070046 return Runtime::Current()->GetClassLinker()->GetBootClassPath().size();
47}
48
49/*
50 * Returns a string URL for a resource with the specified 'javaName' in
51 * entry 'index' of the boot class path.
52 *
53 * We return a newly-allocated String in the following form:
54 *
55 * jar:file://path!/name
56 *
57 * Where "path" is the bootstrap class path entry and "name" is the string
58 * passed into this method. "path" needs to be an absolute path (starting
59 * with '/'); if it's not we'd need to make it absolute as part of forming
60 * the URL string.
61 */
Elliott Hughes0512f022012-03-15 22:10:52 -070062static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070063 ScopedUtfChars name(env, javaName);
64 if (name.c_str() == NULL) {
65 return NULL;
66 }
67
68 const std::vector<const DexFile*>& path = Runtime::Current()->GetClassLinker()->GetBootClassPath();
69 if (index < 0 || size_t(index) >= path.size()) {
70 return NULL;
71 }
72 const DexFile* dex_file = path[index];
73 const std::string& location(dex_file->GetLocation());
74 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(location));
75 if (zip_archive.get() == NULL) {
76 return NULL;
77 }
78 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(name.c_str()));
79 if (zip_entry.get() == NULL) {
80 return NULL;
81 }
82
83 std::string url;
84 StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str());
85 return env->NewStringUTF(url.c_str());
86}
87
Elliott Hughes64bf5a32011-09-20 14:43:12 -070088static JNINativeMethod gMethods[] = {
89 NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
90 NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"),
91 NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "()I"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -070092};
93
Elliott Hughes64bf5a32011-09-20 14:43:12 -070094void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070095 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -070096}
97
98} // namespace art