xref: /aosp_15_r20/art/test/071-dexfile/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.io.File;
18*795d594fSAndroid Build Coastguard Worker import java.io.IOException;
19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Constructor;
20*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
21*795d594fSAndroid Build Coastguard Worker import java.util.Enumeration;
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker /**
24*795d594fSAndroid Build Coastguard Worker  * DexFile tests (Dalvik-specific).
25*795d594fSAndroid Build Coastguard Worker  */
26*795d594fSAndroid Build Coastguard Worker public class Main {
27*795d594fSAndroid Build Coastguard Worker     private static final String CLASS_PATH = System.getenv("DEX_LOCATION") + "/071-dexfile-ex.jar";
28*795d594fSAndroid Build Coastguard Worker     private static final String ODEX_DIR = System.getenv("DEX_LOCATION");
29*795d594fSAndroid Build Coastguard Worker     private static final String ODEX_ALT = "/tmp";
30*795d594fSAndroid Build Coastguard Worker     private static final String LIB_DIR = "/nowhere/nothing/";
31*795d594fSAndroid Build Coastguard Worker 
getOdexDir()32*795d594fSAndroid Build Coastguard Worker     private static final String getOdexDir() {
33*795d594fSAndroid Build Coastguard Worker         return new File(ODEX_DIR).isDirectory() ? ODEX_DIR : ODEX_ALT;
34*795d594fSAndroid Build Coastguard Worker     }
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker     /**
37*795d594fSAndroid Build Coastguard Worker      * Prep the environment then run the test.
38*795d594fSAndroid Build Coastguard Worker      */
main(String[] args)39*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
40*795d594fSAndroid Build Coastguard Worker         /*
41*795d594fSAndroid Build Coastguard Worker          * Create a sub-process to see if the ProcessManager wait
42*795d594fSAndroid Build Coastguard Worker          * interferes with the dexopt invocation wait.
43*795d594fSAndroid Build Coastguard Worker          *
44*795d594fSAndroid Build Coastguard Worker          * /dev/random never hits EOF, so we're sure that we'll still
45*795d594fSAndroid Build Coastguard Worker          * be waiting for the process to complete.  On the device it
46*795d594fSAndroid Build Coastguard Worker          * stops pretty quickly (which means the child won't be
47*795d594fSAndroid Build Coastguard Worker          * spinning).
48*795d594fSAndroid Build Coastguard Worker          */
49*795d594fSAndroid Build Coastguard Worker         ProcessBuilder pb = new ProcessBuilder("cat", "/dev/random");
50*795d594fSAndroid Build Coastguard Worker         Process p = pb.start();
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker         testDexClassLoader();
53*795d594fSAndroid Build Coastguard Worker         testDexFile();
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker         // shouldn't be necessary, but it's good to be tidy
56*795d594fSAndroid Build Coastguard Worker         p.destroy();
57*795d594fSAndroid Build Coastguard Worker         // let the ProcessManager's daemon thread finish before we shut down
58*795d594fSAndroid Build Coastguard Worker         // (avoids the occasional segmentation fault)
59*795d594fSAndroid Build Coastguard Worker         Thread.sleep(500);
60*795d594fSAndroid Build Coastguard Worker         System.out.println("done");
61*795d594fSAndroid Build Coastguard Worker     }
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker     /**
64*795d594fSAndroid Build Coastguard Worker      * Create a class loader, explicitly specifying the source DEX and
65*795d594fSAndroid Build Coastguard Worker      * the location for the optimized DEX.
66*795d594fSAndroid Build Coastguard Worker      */
testDexClassLoader()67*795d594fSAndroid Build Coastguard Worker     private static void testDexClassLoader() throws Exception {
68*795d594fSAndroid Build Coastguard Worker         ClassLoader dexClassLoader = getDexClassLoader();
69*795d594fSAndroid Build Coastguard Worker         Class<?> Another = dexClassLoader.loadClass("Another");
70*795d594fSAndroid Build Coastguard Worker         Object another = Another.newInstance();
71*795d594fSAndroid Build Coastguard Worker         // not expected to work; just exercises the call
72*795d594fSAndroid Build Coastguard Worker         dexClassLoader.getResource("nonexistent");
73*795d594fSAndroid Build Coastguard Worker     }
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker     /*
76*795d594fSAndroid Build Coastguard Worker      * Create an instance of DexClassLoader.  The test harness doesn't
77*795d594fSAndroid Build Coastguard Worker      * have visibility into dalvik.system.*, so we do this through
78*795d594fSAndroid Build Coastguard Worker      * reflection.
79*795d594fSAndroid Build Coastguard Worker      */
getDexClassLoader()80*795d594fSAndroid Build Coastguard Worker     private static ClassLoader getDexClassLoader() throws Exception {
81*795d594fSAndroid Build Coastguard Worker         ClassLoader classLoader = Main.class.getClassLoader();
82*795d594fSAndroid Build Coastguard Worker         Class<?> DexClassLoader = classLoader.loadClass("dalvik.system.DexClassLoader");
83*795d594fSAndroid Build Coastguard Worker         Constructor<?> DexClassLoader_init = DexClassLoader.getConstructor(String.class,
84*795d594fSAndroid Build Coastguard Worker                                                                            String.class,
85*795d594fSAndroid Build Coastguard Worker                                                                            String.class,
86*795d594fSAndroid Build Coastguard Worker                                                                            ClassLoader.class);
87*795d594fSAndroid Build Coastguard Worker         // create an instance, using the path we found
88*795d594fSAndroid Build Coastguard Worker         return (ClassLoader) DexClassLoader_init.newInstance(CLASS_PATH,
89*795d594fSAndroid Build Coastguard Worker                                                              getOdexDir(),
90*795d594fSAndroid Build Coastguard Worker                                                              LIB_DIR,
91*795d594fSAndroid Build Coastguard Worker                                                              classLoader);
92*795d594fSAndroid Build Coastguard Worker     }
93*795d594fSAndroid Build Coastguard Worker 
testDexFile()94*795d594fSAndroid Build Coastguard Worker     private static void testDexFile() throws Exception {
95*795d594fSAndroid Build Coastguard Worker         ClassLoader classLoader = Main.class.getClassLoader();
96*795d594fSAndroid Build Coastguard Worker         Class<?> DexFile = classLoader.loadClass("dalvik.system.DexFile");
97*795d594fSAndroid Build Coastguard Worker         Method DexFile_loadDex = DexFile.getMethod("loadDex",
98*795d594fSAndroid Build Coastguard Worker                                                    String.class,
99*795d594fSAndroid Build Coastguard Worker                                                    String.class,
100*795d594fSAndroid Build Coastguard Worker                                                    Integer.TYPE);
101*795d594fSAndroid Build Coastguard Worker         Method DexFile_entries = DexFile.getMethod("entries");
102*795d594fSAndroid Build Coastguard Worker         Object dexFile = DexFile_loadDex.invoke(null, CLASS_PATH, null, 0);
103*795d594fSAndroid Build Coastguard Worker         Enumeration<String> e = (Enumeration<String>) DexFile_entries.invoke(dexFile);
104*795d594fSAndroid Build Coastguard Worker         while (e.hasMoreElements()) {
105*795d594fSAndroid Build Coastguard Worker             String className = e.nextElement();
106*795d594fSAndroid Build Coastguard Worker             System.out.println(className);
107*795d594fSAndroid Build Coastguard Worker         }
108*795d594fSAndroid Build Coastguard Worker     }
109*795d594fSAndroid Build Coastguard Worker }
110