blob: c3a96715c251b806eb21f30e80a3d065ccb577fc [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
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
17import java.io.File;
18import java.io.IOException;
19import java.lang.reflect.Constructor;
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070020import java.lang.reflect.Method;
21import java.util.Enumeration;
jeffhao5d1ac922011-09-29 17:41:15 -070022
23/**
24 * DexFile tests (Dalvik-specific).
25 */
26public class Main {
TDYa1276ce558b2012-04-11 11:17:55 -070027 private static final String CLASS_PATH = System.getenv("DEX_LOCATION") + "/071-dexfile-ex.jar";
28 private static final String ODEX_DIR = System.getenv("DEX_LOCATION");
jeffhao5d1ac922011-09-29 17:41:15 -070029 private static final String ODEX_ALT = "/tmp";
30 private static final String LIB_DIR = "/nowhere/nothing/";
31
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070032 private static final String getOdexDir() {
33 return new File(ODEX_DIR).isDirectory() ? ODEX_DIR : ODEX_ALT;
34 }
35
jeffhao5d1ac922011-09-29 17:41:15 -070036 /**
37 * Prep the environment then run the test.
38 */
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070039 public static void main(String[] args) throws Exception {
40 /*
41 * Create a sub-process to see if the ProcessManager wait
42 * interferes with the dexopt invocation wait.
43 *
44 * /dev/random never hits EOF, so we're sure that we'll still
45 * be waiting for the process to complete. On the device it
46 * stops pretty quickly (which means the child won't be
47 * spinning).
48 */
49 ProcessBuilder pb = new ProcessBuilder("cat", "/dev/random");
50 Process p = pb.start();
jeffhao5d1ac922011-09-29 17:41:15 -070051
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070052 testDexClassLoader();
53 testDexFile();
jeffhao5d1ac922011-09-29 17:41:15 -070054
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070055 // shouldn't be necessary, but it's good to be tidy
56 p.destroy();
57 // let the ProcessManager's daemon thread finish before we shut down
58 // (avoids the occasional segmentation fault)
59 Thread.sleep(500);
jeffhao5d1ac922011-09-29 17:41:15 -070060 System.out.println("done");
61 }
62
63 /**
64 * Create a class loader, explicitly specifying the source DEX and
65 * the location for the optimized DEX.
66 */
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070067 private static void testDexClassLoader() throws Exception {
jeffhao5d1ac922011-09-29 17:41:15 -070068 ClassLoader dexClassLoader = getDexClassLoader();
Andreas Gampe166aaee2016-07-18 08:27:23 -070069 Class<?> Another = dexClassLoader.loadClass("Another");
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070070 Object another = Another.newInstance();
jeffhao5d1ac922011-09-29 17:41:15 -070071 // not expected to work; just exercises the call
72 dexClassLoader.getResource("nonexistent");
73 }
74
75 /*
76 * Create an instance of DexClassLoader. The test harness doesn't
77 * have visibility into dalvik.system.*, so we do this through
78 * reflection.
79 */
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070080 private static ClassLoader getDexClassLoader() throws Exception {
81 ClassLoader classLoader = Main.class.getClassLoader();
Andreas Gampe166aaee2016-07-18 08:27:23 -070082 Class<?> DexClassLoader = classLoader.loadClass("dalvik.system.DexClassLoader");
83 Constructor<?> DexClassLoader_init = DexClassLoader.getConstructor(String.class,
84 String.class,
85 String.class,
86 ClassLoader.class);
jeffhao5d1ac922011-09-29 17:41:15 -070087 // create an instance, using the path we found
Andreas Gampe166aaee2016-07-18 08:27:23 -070088 return (ClassLoader) DexClassLoader_init.newInstance(CLASS_PATH,
89 getOdexDir(),
90 LIB_DIR,
91 classLoader);
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070092 }
jeffhao5d1ac922011-09-29 17:41:15 -070093
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070094 private static void testDexFile() throws Exception {
95 ClassLoader classLoader = Main.class.getClassLoader();
Andreas Gampe166aaee2016-07-18 08:27:23 -070096 Class<?> DexFile = classLoader.loadClass("dalvik.system.DexFile");
Brian Carlstromf2f9daf2014-05-28 11:09:10 -070097 Method DexFile_loadDex = DexFile.getMethod("loadDex",
98 String.class,
99 String.class,
100 Integer.TYPE);
101 Method DexFile_entries = DexFile.getMethod("entries");
102 Object dexFile = DexFile_loadDex.invoke(null, CLASS_PATH, null, 0);
103 Enumeration<String> e = (Enumeration<String>) DexFile_entries.invoke(dexFile);
104 while (e.hasMoreElements()) {
105 String className = e.nextElement();
106 System.out.println(className);
107 }
jeffhao5d1ac922011-09-29 17:41:15 -0700108 }
109}