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" |
| 19 | #include "ScopedUtfChars.h" |
| 20 | #include "zip_archive.h" |
| 21 | |
| 22 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | namespace { |
| 27 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 28 | jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) { |
| 29 | ClassLoader* loader = Decode<ClassLoader*>(env, javaLoader); |
| 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())); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 36 | Class* c = Runtime::Current()->GetClassLinker()->LookupClass(descriptor.c_str(), loader); |
| 37 | return AddLocalReference<jclass>(env, c); |
| 38 | } |
| 39 | |
| 40 | jint VMClassLoader_getBootClassPathSize(JNIEnv* env, jclass) { |
| 41 | return Runtime::Current()->GetClassLinker()->GetBootClassPath().size(); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Returns a string URL for a resource with the specified 'javaName' in |
| 46 | * entry 'index' of the boot class path. |
| 47 | * |
| 48 | * We return a newly-allocated String in the following form: |
| 49 | * |
| 50 | * jar:file://path!/name |
| 51 | * |
| 52 | * Where "path" is the bootstrap class path entry and "name" is the string |
| 53 | * passed into this method. "path" needs to be an absolute path (starting |
| 54 | * with '/'); if it's not we'd need to make it absolute as part of forming |
| 55 | * the URL string. |
| 56 | */ |
| 57 | jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) { |
| 58 | ScopedUtfChars name(env, javaName); |
| 59 | if (name.c_str() == NULL) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | const std::vector<const DexFile*>& path = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 64 | if (index < 0 || size_t(index) >= path.size()) { |
| 65 | return NULL; |
| 66 | } |
| 67 | const DexFile* dex_file = path[index]; |
| 68 | const std::string& location(dex_file->GetLocation()); |
| 69 | UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(location)); |
| 70 | if (zip_archive.get() == NULL) { |
| 71 | return NULL; |
| 72 | } |
| 73 | UniquePtr<ZipEntry> zip_entry(zip_archive->Find(name.c_str())); |
| 74 | if (zip_entry.get() == NULL) { |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | std::string url; |
| 79 | StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str()); |
| 80 | return env->NewStringUTF(url.c_str()); |
| 81 | } |
| 82 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 83 | static JNINativeMethod gMethods[] = { |
| 84 | NATIVE_METHOD(VMClassLoader, findLoadedClass, "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"), |
| 85 | NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"), |
| 86 | NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "()I"), |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | void register_java_lang_VMClassLoader(JNIEnv* env) { |
| 92 | jniRegisterNativeMethods(env, "java/lang/VMClassLoader", gMethods, NELEM(gMethods)); |
| 93 | } |
| 94 | |
| 95 | } // namespace art |