BaseDexClassLoader.java revision a2656629522f9d79e2dca7418ab5963f50d0fda8
1/*
2 * Copyright (C) 2011 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
17package dalvik.system;
18
19import java.io.File;
20import java.net.URL;
21import java.util.ArrayList;
22import java.util.Enumeration;
23import java.util.List;
24
25/**
26 * Base class for common functionality between various dex-based
27 * {@link ClassLoader} implementations.
28 */
29public class BaseDexClassLoader extends ClassLoader {
30    private final DexPathList pathList;
31
32    /**
33     * Constructs an instance.
34     *
35     * @param dexPath the list of jar/apk files containing classes and
36     * resources, delimited by {@code File.pathSeparator}, which
37     * defaults to {@code ":"} on Android
38     * @param optimizedDirectory directory where optimized dex files
39     * should be written; may be {@code null}
40     * @param librarySearchPath the list of directories containing native
41     * libraries, delimited by {@code File.pathSeparator}; may be
42     * {@code null}
43     * @param parent the parent class loader
44     *
45     * This method will be deprecated in the next release
46     */
47    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
48            String librarySearchPath, ClassLoader parent) {
49        this(dexPath, optimizedDirectory, librarySearchPath, null, parent);
50    }
51
52    /**
53     * Constructs an instance.
54     *
55     * @param dexPath the list of jar/apk files containing classes and
56     * resources, delimited by {@code File.pathSeparator}, which
57     * defaults to {@code ":"} on Android
58     * @param optimizedDirectory directory where optimized dex files
59     * should be written; may be {@code null}
60     * @param librarySearchPath the list of directories containing native
61     * libraries, delimited by {@code File.pathSeparator}; may be
62     * {@code null}; directories in this list are used to search for
63     * a native library
64     * @param libraryPermittedPath allows opening native libraries under
65     * directories in this list. The list is delimited by
66     * {@code File.pathSeparator}. Note that the classloader
67     * is implicitly allowed to open libraries from the
68     * directories on libraryPath. Directories from this list
69     * are NOT used to search for the native library;
70     * may be {@code null}
71     * @param parent the parent class loader
72     *
73     * @hide
74     */
75    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
76            String librarySearchPath, String libraryPermittedPath, ClassLoader parent) {
77        super(parent);
78        this.pathList = new DexPathList(this, dexPath, librarySearchPath,
79                                        libraryPermittedPath, optimizedDirectory);
80    }
81
82    @Override
83    protected Class<?> findClass(String name) throws ClassNotFoundException {
84        List<Throwable> suppressedExceptions = new ArrayList<Throwable>();
85        Class c = pathList.findClass(name, suppressedExceptions);
86        if (c == null) {
87            ClassNotFoundException cnfe = new ClassNotFoundException("Didn't find class \"" + name + "\" on path: " + pathList);
88            for (Throwable t : suppressedExceptions) {
89                cnfe.addSuppressed(t);
90            }
91            throw cnfe;
92        }
93        return c;
94    }
95
96    @Override
97    protected URL findResource(String name) {
98        return pathList.findResource(name);
99    }
100
101    @Override
102    protected Enumeration<URL> findResources(String name) {
103        return pathList.findResources(name);
104    }
105
106    @Override
107    public String findLibrary(String name) {
108        return pathList.findLibrary(name);
109    }
110
111    /**
112     * Returns package information for the given package.
113     * Unfortunately, instances of this class don't really have this
114     * information, and as a non-secure {@code ClassLoader}, it isn't
115     * even required to, according to the spec. Yet, we want to
116     * provide it, in order to make all those hopeful callers of
117     * {@code myClass.getPackage().getName()} happy. Thus we construct
118     * a {@code Package} object the first time it is being requested
119     * and fill most of the fields with dummy values. The {@code
120     * Package} object is then put into the {@code ClassLoader}'s
121     * package cache, so we see the same one next time. We don't
122     * create {@code Package} objects for {@code null} arguments or
123     * for the default package.
124     *
125     * <p>There is a limited chance that we end up with multiple
126     * {@code Package} objects representing the same package: It can
127     * happen when when a package is scattered across different JAR
128     * files which were loaded by different {@code ClassLoader}
129     * instances. This is rather unlikely, and given that this whole
130     * thing is more or less a workaround, probably not worth the
131     * effort to address.
132     *
133     * @param name the name of the class
134     * @return the package information for the class, or {@code null}
135     * if there is no package information available for it
136     */
137    @Override
138    protected synchronized Package getPackage(String name) {
139        if (name != null && !name.isEmpty()) {
140            Package pack = super.getPackage(name);
141
142            if (pack == null) {
143                pack = definePackage(name, "Unknown", "0.0", "Unknown",
144                        "Unknown", "0.0", "Unknown", null);
145            }
146
147            return pack;
148        }
149
150        return null;
151    }
152
153    /**
154     * @hide
155     */
156    public String getLdLibraryPath() {
157        StringBuilder result = new StringBuilder();
158        for (File directory : pathList.getNativeLibraryDirectories()) {
159            if (result.length() > 0) {
160                result.append(':');
161            }
162            result.append(directory);
163        }
164
165        return result.toString();
166    }
167
168    /**
169     * @hide
170     */
171    public String getLibraryPermittedPath() {
172        return pathList.getLibraryPermittedPath();
173    }
174
175    @Override public String toString() {
176        return getClass().getName() + "[" + pathList + "]";
177    }
178}
179