2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.RandomAccessFile;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Method;
23 import java.lang.reflect.InvocationTargetException;
26 * A class loader with atypical behavior: we try to load a private
27 * class implementation before asking the system or boot loader. This
28 * is used to create multiple classes with identical names in a single VM.
30 * If DexFile is available, we use that; if not, we assume we're not in
31 * Dalvik and instantiate the class with defineClass().
33 * The location of the DEX files and class data is dependent upon the
36 public class FancyLoader extends ClassLoader {
37 /* this is where the "alternate" .class files live */
38 static final String CLASS_PATH = "classes-ex/";
40 /* this is the "alternate" DEX/Jar file */
41 static final String DEX_FILE = "test-ex.jar";
43 /* on Dalvik, this is a DexFile; otherwise, it's null */
44 private Class mDexClass;
46 private Object mDexFile;
49 * Construct FancyLoader, grabbing a reference to the DexFile class
50 * if we're running under Dalvik.
52 public FancyLoader(ClassLoader parent) {
56 mDexClass = parent.loadClass("dalvik.system.DexFile");
57 } catch (ClassNotFoundException cnfe) {
58 // ignore -- not running Dalvik
63 * Finds the class with the specified binary name.
65 * We search for a file in CLASS_PATH or pull an entry from DEX_FILE.
66 * If we don't find a match, we throw an exception.
68 protected Class<?> findClass(String name) throws ClassNotFoundException
70 if (mDexClass != null) {
71 return findClassDalvik(name);
73 return findClassNonDalvik(name);
78 * Finds the class with the specified binary name, from a DEX file.
80 private Class<?> findClassDalvik(String name)
81 throws ClassNotFoundException {
83 if (mDexFile == null) {
84 synchronized (FancyLoader.class) {
87 * Construct a DexFile object through reflection.
90 ctor = mDexClass.getConstructor(new Class[] {String.class});
91 } catch (NoSuchMethodException nsme) {
92 throw new ClassNotFoundException("getConstructor failed",
97 mDexFile = ctor.newInstance(DEX_FILE);
98 } catch (InstantiationException ie) {
99 throw new ClassNotFoundException("newInstance failed", ie);
100 } catch (IllegalAccessException iae) {
101 throw new ClassNotFoundException("newInstance failed", iae);
102 } catch (InvocationTargetException ite) {
103 throw new ClassNotFoundException("newInstance failed", ite);
109 * Call DexFile.loadClass(String, ClassLoader).
114 meth = mDexClass.getMethod("loadClass",
115 new Class[] { String.class, ClassLoader.class });
116 } catch (NoSuchMethodException nsme) {
117 throw new ClassNotFoundException("getMethod failed", nsme);
121 meth.invoke(mDexFile, name, this);
122 } catch (IllegalAccessException iae) {
123 throw new ClassNotFoundException("loadClass failed", iae);
124 } catch (InvocationTargetException ite) {
125 throw new ClassNotFoundException("loadClass failed",
133 * Finds the class with the specified binary name, from .class files.
135 private Class<?> findClassNonDalvik(String name)
136 throws ClassNotFoundException {
138 String pathName = CLASS_PATH + name + ".class";
139 //System.out.println("--- Fancy: looking for " + pathName);
141 File path = new File(pathName);
142 RandomAccessFile raf;
145 raf = new RandomAccessFile(path, "r");
146 } catch (FileNotFoundException fnfe) {
147 throw new ClassNotFoundException("Not found: " + pathName);
150 /* read the entire file in */
153 fileData = new byte[(int) raf.length()];
154 raf.readFully(fileData);
155 } catch (IOException ioe) {
156 throw new ClassNotFoundException("Read error: " + pathName);
160 } catch (IOException ioe) {
165 /* create the class */
166 //System.out.println("--- Fancy: defining " + name);
168 return defineClass(name, fileData, 0, fileData.length);
169 } catch (Throwable th) {
170 throw new ClassNotFoundException("defineClass failed", th);
177 * Normally a class loader wouldn't override this, but we want our
178 * version of the class to take precedence over an already-loaded
181 * We still want the system classes (e.g. java.lang.Object) from the
182 * bootstrap class loader.
184 protected Class<?> loadClass(String name, boolean resolve)
185 throws ClassNotFoundException
190 * 1. Invoke findLoadedClass(String) to check if the class has
191 * already been loaded.
193 * This doesn't change.
195 res = findLoadedClass(name);
197 System.out.println("FancyLoader.loadClass: "
198 + name + " already loaded");
205 * 3. Invoke the findClass(String) method to find the class.
208 res = findClass(name);
212 catch (ClassNotFoundException e) {
213 // we couldn't find it, so eat the exception and keep going
217 * 2. Invoke the loadClass method on the parent class loader. If
218 * the parent loader is null the class loader built-in to the
219 * virtual machine is used, instead.
221 * (Since we're not in java.lang, we can't actually invoke the
222 * parent's loadClass() method, but we passed our parent to the
223 * super-class which can take care of it for us.)
225 res = super.loadClass(name, resolve); // returns class or throws