Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2017 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 import java.lang.reflect.Constructor;
     18 import java.lang.reflect.Method;
     19 
     20 public class Main {
     21     public static String TEST_NAME = "164-resolution-trampoline-dex-cache";
     22 
     23     public static void main(String[] args) {
     24         // Load the test JNI for ensureJitCompiled(). Note that classes Main loaded
     25         // by other class loaders do not have the binding, so we need to pass the
     26         // current Main.class around and call ensureJitCompiled() via reflection.
     27         System.loadLibrary(args[0]);
     28         try {
     29             String dex_location = System.getenv("DEX_LOCATION");
     30             ClassLoader systemLoader = ClassLoader.getSystemClassLoader().getParent();
     31             ClassLoader baseLoader = getClassLoaderFor(dex_location, systemLoader, /* ex */ false);
     32             ClassLoader mainLoader = getClassLoaderFor(dex_location, baseLoader, /* ex */ true);
     33 
     34             Class<?> tc = Class.forName("MostDerived", true, mainLoader);
     35             Method m = tc.getDeclaredMethod("test", Class.class);
     36             m.invoke(null, Main.class);
     37         } catch (Throwable t) {
     38             t.printStackTrace(System.out);
     39         }
     40     }
     41 
     42     public static ClassLoader getClassLoaderFor(String location, ClassLoader parent, boolean ex)
     43             throws Exception {
     44         try {
     45             Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
     46             Constructor<?> ctor =
     47                     class_loader_class.getConstructor(String.class, ClassLoader.class);
     48             String path = location + "/" + TEST_NAME + (ex ? "-ex.jar" : ".jar");
     49             return (ClassLoader)ctor.newInstance(path, parent);
     50         } catch (ClassNotFoundException e) {
     51             // Running on RI. Use URLClassLoader.
     52             String url = "file://" + location + (ex ? "/classes-ex/" : "/classes/");
     53             return new java.net.URLClassLoader(
     54                     new java.net.URL[] { new java.net.URL(url) }, parent);
     55         }
     56     }
     57 
     58     public static native void ensureJitCompiled(Class<?> klass, String method_name);
     59 }
     60