blob: 588e5eb4f503633e8946fc8fd9cd777c7253013f [file] [log] [blame]
Andreas Gampeb9aec2c2015-04-23 22:23:47 -07001/*
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
17import java.io.File;
Jeff Hao0cb17282017-07-12 14:51:49 -070018import java.lang.reflect.Constructor;
Andreas Gampeb9aec2c2015-04-23 22:23:47 -070019import java.lang.reflect.Method;
20
21/**
22 * Structural hazard test.
23 */
24public class Main {
Jeff Hao0cb17282017-07-12 14:51:49 -070025 public static String TEST_NAME = "138-duplicate-classes-check2";
26
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
Andreas Gampeb9aec2c2015-04-23 22:23:47 -070042 public static void main(String[] args) {
43 new Main().run();
44 }
45
46 private void run() {
47 System.out.println(new A().i);
48
49 // Now run the class from the -ex file.
Andreas Gampeb9aec2c2015-04-23 22:23:47 -070050 try {
Jeff Hao0cb17282017-07-12 14:51:49 -070051 /* 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);
Andreas Gampeb9aec2c2015-04-23 22:23:47 -070062 }
63 }
64}