Main.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 17import java.io.File; 18import java.lang.reflect.Constructor; 19 20/** 21 * DexFile tests (Dalvik-specific). 22 */ 23public class Main { 24 private static final String CLASS_PATH = "test-ex.jar"; 25 private static final String ODEX_DIR = "/sdcard"; 26 //private static final String ODEX_DIR = "."; 27 private static final String ODEX_ALT = "/tmp"; 28 private static final String LIB_DIR = "/nowhere/nothing/"; 29 30 /** 31 * Create a class loader, explicitly specifying the source DEX and 32 * the location for the optimized DEX. 33 */ 34 public static void main(String[] args) { 35 ClassLoader dexClassLoader = getDexClassLoader(); 36 37 Class anotherClass; 38 try { 39 anotherClass = dexClassLoader.loadClass("Another"); 40 } catch (ClassNotFoundException cnfe) { 41 throw new RuntimeException("Another?"); 42 } 43 44 Object another; 45 try { 46 another = anotherClass.newInstance(); 47 } catch (IllegalAccessException ie) { 48 throw new RuntimeException("new another", ie); 49 } catch (InstantiationException ie) { 50 throw new RuntimeException("new another", ie); 51 } 52 53 /* not expected to work; just exercises the call */ 54 dexClassLoader.getResource("nonexistent"); 55 56 System.out.println("done"); 57 } 58 59 /* 60 * Create an instance of DexClassLoader. The test harness doesn't 61 * have visibility into dalvik.system.*, so we do this through 62 * reflection. 63 */ 64 private static ClassLoader getDexClassLoader() { 65 String odexDir; 66 67 /* 68 String androidData = System.getenv("ANDROID_DATA"); 69 if (androidData == null) 70 androidData = ""; 71 odexDir = androidData + "/" + ODEX_DIR; 72 */ 73 74 File test = new File(ODEX_DIR); 75 if (test.isDirectory()) 76 odexDir = ODEX_DIR; 77 else 78 odexDir = ODEX_ALT; 79 //System.out.println("Output dir is " + odexDir); 80 81 ClassLoader myLoader = Main.class.getClassLoader(); 82 Class dclClass; 83 try { 84 dclClass = myLoader.loadClass("dalvik.system.DexClassLoader"); 85 } catch (ClassNotFoundException cnfe) { 86 throw new RuntimeException("dalvik.system.DexClassLoader not found"); 87 } 88 89 Constructor ctor; 90 try { 91 ctor = dclClass.getConstructor(String.class, String.class, 92 String.class, ClassLoader.class); 93 } catch (NoSuchMethodException nsme) { 94 throw new RuntimeException("DCL ctor", nsme); 95 } 96 97 Object dclObj; 98 try { 99 dclObj = ctor.newInstance(CLASS_PATH, odexDir, LIB_DIR, myLoader); 100 } catch (Exception ex) { 101 throw new RuntimeException("DCL newInstance", ex); 102 } 103 104 return (ClassLoader) dclObj; 105 } 106} 107 108