• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.io.File;
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.Method;
20 
21 /**
22  * Structural hazard test.
23  */
24 public class Main {
25     public static String TEST_NAME = "138-duplicate-classes-check2";
26 
getClassLoaderFor(String location)27     public static ClassLoader getClassLoaderFor(String location) throws Exception {
28         try {
29             Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
30             Constructor<?> ctor =
31                     class_loader_class.getConstructor(String.class, ClassLoader.class);
32             /* on Dalvik, this is a DexFile; otherwise, it's null */
33             return (ClassLoader) ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar",
34                                                   Main.class.getClassLoader());
35         } catch (ClassNotFoundException e) {
36             // Running on RI. Use URLClassLoader.
37             return new java.net.URLClassLoader(
38                     new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") });
39         }
40     }
41 
main(String[] args)42     public static void main(String[] args) {
43         new Main().run();
44     }
45 
run()46     private void run() {
47         System.out.println(new A().i);
48 
49         // Now run the class from the -ex file.
50         try {
51             /* this is the "alternate" DEX/Jar file */
52             ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION"));
53             Class<?> klass = (Class<?>) new_loader.loadClass("TestEx");
54             if (klass == null) {
55                 throw new AssertionError("loadClass failed");
56             }
57             Method run_test = klass.getMethod("test");
58             run_test.invoke(null);
59         } catch (Exception e) {
60             System.out.println(e.toString());
61             e.printStackTrace(System.out);
62         }
63     }
64 }
65