Home | History | Annotate | Download | only in native
      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 "mirror/class_loader.h"
     20 #include "mirror/object-inl.h"
     21 #include "scoped_fast_native_object_access.h"
     22 #include "ScopedUtfChars.h"
     23 #include "zip_archive.h"
     24 
     25 namespace art {
     26 
     27 static jclass VMClassLoader_findLoadedClass(JNIEnv* env, jclass, jobject javaLoader, jstring javaName) {
     28   ScopedFastNativeObjectAccess soa(env);
     29   mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
     30   ScopedUtfChars name(env, javaName);
     31   if (name.c_str() == NULL) {
     32     return NULL;
     33   }
     34   ClassLinker* cl = Runtime::Current()->GetClassLinker();
     35   std::string descriptor(DotToDescriptor(name.c_str()));
     36   mirror::Class* c = cl->LookupClass(descriptor.c_str(), loader);
     37   if (c != NULL && c->IsResolved()) {
     38     return soa.AddLocalReference<jclass>(c);
     39   }
     40   if (loader != nullptr) {
     41     // Try the common case.
     42     StackHandleScope<1> hs(soa.Self());
     43     c = cl->FindClassInPathClassLoader(soa, soa.Self(), descriptor.c_str(), hs.NewHandle(loader));
     44     if (c != nullptr) {
     45       return soa.AddLocalReference<jclass>(c);
     46     }
     47   }
     48   // Class wasn't resolved so it may be erroneous or not yet ready, force the caller to go into
     49   // the regular loadClass code.
     50   return NULL;
     51 }
     52 
     53 static jint VMClassLoader_getBootClassPathSize(JNIEnv*, jclass) {
     54   return Runtime::Current()->GetClassLinker()->GetBootClassPath().size();
     55 }
     56 
     57 /*
     58  * Returns a string URL for a resource with the specified 'javaName' in
     59  * entry 'index' of the boot class path.
     60  *
     61  * We return a newly-allocated String in the following form:
     62  *
     63  *   jar:file://path!/name
     64  *
     65  * Where "path" is the bootstrap class path entry and "name" is the string
     66  * passed into this method.  "path" needs to be an absolute path (starting
     67  * with '/'); if it's not we'd need to make it absolute as part of forming
     68  * the URL string.
     69  */
     70 static jstring VMClassLoader_getBootClassPathResource(JNIEnv* env, jclass, jstring javaName, jint index) {
     71   ScopedUtfChars name(env, javaName);
     72   if (name.c_str() == nullptr) {
     73     return nullptr;
     74   }
     75 
     76   const std::vector<const DexFile*>& path = Runtime::Current()->GetClassLinker()->GetBootClassPath();
     77   if (index < 0 || size_t(index) >= path.size()) {
     78     return nullptr;
     79   }
     80   const DexFile* dex_file = path[index];
     81 
     82   // For multidex locations, e.g., x.jar:classes2.dex, we want to look into x.jar.
     83   const std::string& location(dex_file->GetBaseLocation());
     84 
     85   std::string error_msg;
     86   std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(location.c_str(), &error_msg));
     87   if (zip_archive.get() == nullptr) {
     88     LOG(WARNING) << "Failed to open zip archive '" << location << "': " << error_msg;
     89     return nullptr;
     90   }
     91   std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(name.c_str(), &error_msg));
     92   if (zip_entry.get() == nullptr) {
     93     return nullptr;
     94   }
     95 
     96   std::string url;
     97   StringAppendF(&url, "jar:file://%s!/%s", location.c_str(), name.c_str());
     98   return env->NewStringUTF(url.c_str());
     99 }
    100 
    101 static JNINativeMethod gMethods[] = {
    102   NATIVE_METHOD(VMClassLoader, findLoadedClass, "!(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;"),
    103   NATIVE_METHOD(VMClassLoader, getBootClassPathResource, "(Ljava/lang/String;I)Ljava/lang/String;"),
    104   NATIVE_METHOD(VMClassLoader, getBootClassPathSize, "!()I"),
    105 };
    106 
    107 void register_java_lang_VMClassLoader(JNIEnv* env) {
    108   REGISTER_NATIVE_METHODS("java/lang/VMClassLoader");
    109 }
    110 
    111 }  // namespace art
    112