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 = "155-java-set-resolved-type";
22 
main(String[] args)23     public static void main(String[] args) {
24         try {
25             Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
26             System.loadLibrary(args[0]);
27         } catch (ClassNotFoundException e) {
28             usingRI = true;
29             // Add expected JNI_OnLoad log line to match expected.txt.
30             System.out.println("JNI_OnLoad called");
31         }
32         try {
33             String dex_location = System.getenv("DEX_LOCATION");
34             ClassLoader systemLoader = ClassLoader.getSystemClassLoader().getParent();
35             ClassLoader exLoader = getClassLoaderFor(dex_location, systemLoader, /* ex */ true);
36             ClassLoader mainLoader = getClassLoaderFor(dex_location, exLoader, /* ex */ false);
37 
38             // Resolve TestParameter class. It shall be defined by mainLoader.
39             // This does not resolve method parameter types.
40             Class<?> tpc = Class.forName("TestParameter", false, mainLoader);
41             // Get declared methods of TestParameter.
42             // This still does not resolve method parameter types.
43             Method[] ms = tpc.getDeclaredMethods();
44             if (ms == null || ms.length != 1) { throw new Error("Unexpected methods"); };
45             // Call getParameterTypes() to resolve parameter types. The parameter type
46             // TestInterface shall be defined by the exLoader. This used to store the
47             // TestInterface class in the dex cache resolved types for the mainLoader
48             // but not in the mainLoader's class table. This discrepancy used to cause
49             // a crash further down.
50             ms[0].getParameterTypes();
51 
52             // Resolve but do not initialize TestImplementation. During the resolution,
53             // we see the TestInterface in the dex cache, so we do not try to look it up
54             // or resolve it using the mainLoader.
55             Class<?> timpl = Class.forName("TestImplementation", false, mainLoader);
56             // Clear the dex cache resolved types to force a proper lookup the next time
57             // we need to find TestInterface.
58             clearResolvedTypes(timpl);
59 
60             // Force intialization of TestImplementation. This expects the interface type
61             // to be resolved and found through simple lookup.
62             timpl.newInstance();
63         } catch (Throwable t) {
64             t.printStackTrace(System.out);
65         }
66     }
67 
getClassLoaderFor(String location, ClassLoader parent, boolean ex)68     public static ClassLoader getClassLoaderFor(String location, ClassLoader parent, boolean ex)
69             throws Exception {
70         try {
71             Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
72             Constructor<?> ctor =
73                     class_loader_class.getConstructor(String.class, ClassLoader.class);
74             /* on Dalvik, this is a DexFile; otherwise, it's null */
75             String path = location + "/" + TEST_NAME + (ex ? "-ex.jar" : ".jar");
76             return (ClassLoader)ctor.newInstance(path, parent);
77         } catch (ClassNotFoundException e) {
78             // Running on RI. Use URLClassLoader.
79             String url = "file://" + location + (ex ? "/classes-ex/" : "/classes/");
80             return new java.net.URLClassLoader(
81                     new java.net.URL[] { new java.net.URL(url) }, parent);
82         }
83     }
84 
clearResolvedTypes(Class<?> c)85     public static void clearResolvedTypes(Class<?> c) {
86         if (!usingRI) {
87             nativeClearResolvedTypes(c);
88         }
89     }
90 
91     private static boolean usingRI = false;
92 
nativeClearResolvedTypes(Class<?> c)93     public static native void nativeClearResolvedTypes(Class<?> c);
94 }
95