blob: a97693308790de1d9a32454b6e014c089daf8a6c [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"
19#include "ScopedUtfChars.h"
20#include "zip_archive.h"
21
Elliott Hughes64bf5a32011-09-20 14:43:12 -070022namespace art {
23
Elliott Hughes0512f022012-03-15 22:10:52 -070024static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070025 ClassLoader* loader = Decode<ClassLoader*>(env, javaLoader);
26 ScopedUtfChars name(env, javaName);
27 if (name.c_str() == NULL) {
28 return NULL;
29 }
30
Brian Carlstromf91c8c32011-09-21 17:30:34 -070031 std::string descriptor(DotToDescriptor(name.c_str()));
Elliott Hughes64bf5a32011-09-20 14:43:12 -070032 Class* c = Runtime::Current()->GetClassLinker()->LookupClass(descriptor.c_str(), loader);
Ian Rogersbe125a92012-01-11 15:19:49 -080033 if (c != NULL && c->IsResolved()) {
34 return AddLocalReference<jclass>(env, c);
35 } else {
36 // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
37 // the regular loadClass code.
38 return NULL;
39 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -070040}
41
Elliott Hughes1bac54f2012-03-16 12:48:31 -070042static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070043 return Runtime::Current()->GetClassLinker()->GetBootClassPath().size();
44}
45
46/*
47 * Returns a string URL for a resource with the specified 'javaName' in
48 * entry 'index' of the boot class path.
49 *
50 * We return a newly-allocated String in the following form:
51 *
52 * jar:file://path!/name
53 *
54 * Where "path" is the bootstrap class path entry and "name" is the string
55 * passed into this method. "path" needs to be an absolute path (starting
56 * with '/'); if it's not we'd need to make it absolute as part of forming
57 * the URL string.
58 */
Elliott Hughes0512f022012-03-15 22:10:52 -070059static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -070060 ScopedUtfChars name(env, javaName);
61 if (name.c_str() == NULL) {
62 return NULL;
63 }
64
65 const std::vector<const DexFile*>& path = Runtime::Current()->GetClassLinker()->GetBootClassPath();
66 if (index < 0 || size_t(index) >= path.size()) {
67 return NULL;
68 }
69 const DexFile* dex_file = path[index];
70 const std::string& location(dex_file->GetLocation());
71 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(location));
72 if (zip_archive.get() == NULL) {
73 return NULL;
74 }
75 UniquePtr<ZipEntry> zip_entry(zip_archive->Find(name.c_str()));
76 if (zip_entry.get() == NULL) {
77 return NULL;
78 }
79
80 std::string url;
81 StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str());
82 return env->NewStringUTF(url.c_str());
83}
84
Elliott Hughes64bf5a32011-09-20 14:43:12 -070085static JNINativeMethod gMethods[] = {
86 NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
87 NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"),
88 NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "()I"),
Elliott Hughes64bf5a32011-09-20 14:43:12 -070089};
90
Elliott Hughes64bf5a32011-09-20 14:43:12 -070091void register_java_lang_VMClassLoader(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070092 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
Elliott Hughes64bf5a32011-09-20 14:43:12 -070093}
94
95} // namespace art