java_lang_VMClassLoader.cc revision eac766769e3114a078c188ea26776a81f0edb3cf
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 22namespace art { 23 24static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) { 25 ClassLoader* loader = Decode<ClassLoader*>(env, javaLoader); 26 ScopedUtfChars name(env, javaName); 27 if (name.c_str() == NULL) { 28 return NULL; 29 } 30 31 std::string descriptor(DotToDescriptor(name.c_str())); 32 Class* c = Runtime::Current()->GetClassLinker()->LookupClass(descriptor.c_str(), loader); 33 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 } 40} 41 42static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) { 43 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 */ 59static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) { 60 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 85static 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"), 89}; 90 91void register_java_lang_VMClassLoader(JNIEnv* env) { 92 REGISTER_NATIVE_METHODS("java/lang/VMClassLoader"); 93} 94 95} // namespace art 96