jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.io.File; |
| 18 | import java.io.IOException; |
| 19 | import java.lang.reflect.Constructor; |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 20 | import java.lang.reflect.Method; |
| 21 | import java.util.Enumeration; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * DexFile tests (Dalvik-specific). |
| 25 | */ |
| 26 | public class Main { |
TDYa127 | 6ce558b | 2012-04-11 11:17:55 -0700 | [diff] [blame] | 27 | 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"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 29 | private static final String ODEX_ALT = "/tmp"; |
| 30 | private static final String LIB_DIR = "/nowhere/nothing/"; |
| 31 | |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 32 | private static final String getOdexDir() { |
| 33 | return new File(ODEX_DIR).isDirectory() ? ODEX_DIR : ODEX_ALT; |
| 34 | } |
| 35 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Prep the environment then run the test. |
| 38 | */ |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 39 | 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(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 51 | |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 52 | testDexClassLoader(); |
| 53 | testDexFile(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 54 | |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 55 | // 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 60 | 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 Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 67 | private static void testDexClassLoader() throws Exception { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 68 | ClassLoader dexClassLoader = getDexClassLoader(); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 69 | Class<?> Another = dexClassLoader.loadClass("Another"); |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 70 | Object another = Another.newInstance(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 71 | // 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 Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 80 | private static ClassLoader getDexClassLoader() throws Exception { |
| 81 | ClassLoader classLoader = Main.class.getClassLoader(); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 82 | Class<?> DexClassLoader = classLoader.loadClass("dalvik.system.DexClassLoader"); |
| 83 | Constructor<?> DexClassLoader_init = DexClassLoader.getConstructor(String.class, |
| 84 | String.class, |
| 85 | String.class, |
| 86 | ClassLoader.class); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 87 | // create an instance, using the path we found |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 88 | return (ClassLoader) DexClassLoader_init.newInstance(CLASS_PATH, |
| 89 | getOdexDir(), |
| 90 | LIB_DIR, |
| 91 | classLoader); |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 92 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 93 | |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 94 | private static void testDexFile() throws Exception { |
| 95 | ClassLoader classLoader = Main.class.getClassLoader(); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 96 | Class<?> DexFile = classLoader.loadClass("dalvik.system.DexFile"); |
Brian Carlstrom | f2f9daf | 2014-05-28 11:09:10 -0700 | [diff] [blame] | 97 | 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 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 108 | } |
| 109 | } |