diff options
-rw-r--r-- | dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java | 37 | ||||
-rw-r--r-- | dalvik/src/main/java/dalvik/system/DexPathList.java | 63 |
2 files changed, 59 insertions, 41 deletions
diff --git a/dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java b/dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java index 62ec5e3..1c006ba 100644 --- a/dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java +++ b/dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java @@ -25,14 +25,7 @@ import java.util.Enumeration; * {@link ClassLoader} implementations. */ public class BaseDexClassLoader extends ClassLoader { - /** originally specified path (just used for {@code toString()}) */ - private final String originalPath; - - /** originally specified library path (just used for {@code toString()}) */ - private final String originalLibraryPath; - - /** structured lists of path elements */ - private final DexPathList pathList; + private final DexPathList path; /** * Constructs an instance. @@ -50,37 +43,31 @@ public class BaseDexClassLoader extends ClassLoader { public BaseDexClassLoader(String dexPath, File optimizedDirectory, String libraryPath, ClassLoader parent) { super(parent); - - this.originalPath = dexPath; - this.originalLibraryPath = libraryPath; - this.pathList = - new DexPathList(this, dexPath, libraryPath, optimizedDirectory); + this.path = new DexPathList(this, dexPath, libraryPath, optimizedDirectory); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { - Class clazz = pathList.findClass(name); - - if (clazz == null) { - throw new ClassNotFoundException("Didn't find class \"" + name + "\" on path: " + originalPath); + Class c = path.findClass(name); + if (c == null) { + throw new ClassNotFoundException("Didn't find class \"" + name + "\" on path: " + path); } - - return clazz; + return c; } @Override protected URL findResource(String name) { - return pathList.findResource(name); + return path.findResource(name); } @Override protected Enumeration<URL> findResources(String name) { - return pathList.findResources(name); + return path.findResources(name); } @Override public String findLibrary(String name) { - return pathList.findLibrary(name); + return path.findLibrary(name); } /** @@ -125,9 +112,7 @@ public class BaseDexClassLoader extends ClassLoader { return null; } - @Override - public String toString() { - return getClass().getName() - + "[dexPath=" + originalPath + ",libraryPath=" + originalLibraryPath + "]"; + @Override public String toString() { + return getClass().getName() + "[" + path + "]"; } } diff --git a/dalvik/src/main/java/dalvik/system/DexPathList.java b/dalvik/src/main/java/dalvik/system/DexPathList.java index 728639b..1ae48d9 100644 --- a/dalvik/src/main/java/dalvik/system/DexPathList.java +++ b/dalvik/src/main/java/dalvik/system/DexPathList.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.regex.Pattern; @@ -54,7 +55,7 @@ import static libcore.io.OsConstants.*; private final ClassLoader definingContext; /** list of dex/resource (class path) elements */ - private final Element[] dexElements; + private final Element[] pathElements; /** list of native library directory elements */ private final File[] nativeLibraryDirectories; @@ -98,11 +99,15 @@ import static libcore.io.OsConstants.*; } this.definingContext = definingContext; - this.dexElements = - makeDexElements(splitDexPath(dexPath), optimizedDirectory); + this.pathElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory); this.nativeLibraryDirectories = splitLibraryPath(libraryPath); } + @Override public String toString() { + return "DexPathList[pathElements=" + Arrays.toString(pathElements) + + ",nativeLibraryDirectories=" + Arrays.toString(nativeLibraryDirectories) + "]"; + } + /** * Splits the given dex path string into elements using the path * separator, pruning out any elements that do not refer to existing @@ -159,7 +164,7 @@ import static libcore.io.OsConstants.*; * Helper for {@link #splitPaths}, which does the actual splitting * and filtering and adding to a result. */ - private static void splitAndAdd(String searchPath, boolean wantDirectories, + private static void splitAndAdd(String searchPath, boolean directoriesOnly, ArrayList<File> resultList) { if (searchPath == null) { return; @@ -167,8 +172,7 @@ import static libcore.io.OsConstants.*; for (String path : searchPath.split(":")) { try { StructStat sb = Libcore.os.stat(path); - if ((wantDirectories && S_ISDIR(sb.st_mode)) || - (!wantDirectories && S_ISREG(sb.st_mode))) { + if (!directoriesOnly || S_ISDIR(sb.st_mode)) { resultList.add(new File(path)); } } catch (ErrnoException ignored) { @@ -215,12 +219,16 @@ import static libcore.io.OsConstants.*; * the exception here, and let dex == null. */ } + } else if (file.isDirectory()) { + // We support directories for looking up resources. + // This is only useful for running libcore tests. + elements.add(new Element(file, true, null, null)); } else { System.logW("Unknown file type for: " + file); } if ((zip != null) || (dex != null)) { - elements.add(new Element(file, zip, dex)); + elements.add(new Element(file, false, zip, dex)); } } @@ -287,7 +295,7 @@ import static libcore.io.OsConstants.*; * found in any of the dex files */ public Class findClass(String name) { - for (Element element : dexElements) { + for (Element element : pathElements) { DexFile dex = element.dexFile; if (dex != null) { @@ -310,7 +318,7 @@ import static libcore.io.OsConstants.*; * resource is not found in any of the zip/jar files */ public URL findResource(String name) { - for (Element element : dexElements) { + for (Element element : pathElements) { URL url = element.findResource(name); if (url != null) { return url; @@ -328,7 +336,7 @@ import static libcore.io.OsConstants.*; public Enumeration<URL> findResources(String name) { ArrayList<URL> result = new ArrayList<URL>(); - for (Element element : dexElements) { + for (Element element : pathElements) { URL url = element.findResource(name); if (url != null) { result.add(url); @@ -363,26 +371,38 @@ import static libcore.io.OsConstants.*; */ /*package*/ static class Element { private final File file; + private final boolean isDirectory; private final File zip; private final DexFile dexFile; private ZipFile zipFile; - private boolean init; + private boolean initialized; - public Element(File file, File zip, DexFile dexFile) { + public Element(File file, boolean isDirectory, File zip, DexFile dexFile) { this.file = file; + this.isDirectory = isDirectory; this.zip = zip; this.dexFile = dexFile; } + @Override public String toString() { + if (isDirectory) { + return "directory \"" + file + "\""; + } else if (zip != null) { + return "zip file \"" + zip + "\""; + } else { + return "dex file \"" + dexFile + "\""; + } + } + public synchronized void maybeInit() { - if (init) { + if (initialized) { return; } - init = true; + initialized = true; - if (zip == null) { + if (isDirectory || zip == null) { return; } @@ -403,6 +423,19 @@ import static libcore.io.OsConstants.*; public URL findResource(String name) { maybeInit(); + // We support directories so we can run tests and/or legacy code + // that uses Class.getResource. + if (isDirectory) { + File resourceFile = new File(file, name); + if (resourceFile.exists()) { + try { + return resourceFile.toURI().toURL(); + } catch (MalformedURLException ex) { + throw new RuntimeException(ex); + } + } + } + if (zipFile == null || zipFile.getEntry(name) == null) { /* * Either this element has no zip/jar file (first |