BaseDexClassLoader.java revision f904678355f850a647f22e2689a836f895974fdf
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.Enumeration;
22
23/**
24 * Base class for common functionality between various dex-based
25 * {@link ClassLoader} implementations.
26 */
27public class BaseDexClassLoader extends ClassLoader {
28    private final DexPathList path;
29
30    /**
31     * Constructs an instance.
32     *
33     * @param dexPath the list of jar/apk files containing classes and
34     * resources, delimited by {@code File.pathSeparator}, which
35     * defaults to {@code ":"} on Android
36     * @param optimizedDirectory directory where optimized dex files
37     * should be written; may be {@code null}
38     * @param libraryPath the list of directories containing native
39     * libraries, delimited by {@code File.pathSeparator}; may be
40     * {@code null}
41     * @param parent the parent class loader
42     */
43    public BaseDexClassLoader(String dexPath, File optimizedDirectory,
44            String libraryPath, ClassLoader parent) {
45        super(parent);
46        this.path = new DexPathList(this, dexPath, libraryPath, optimizedDirectory);
47    }
48
49    @Override
50    protected Class<?> findClass(String name) throws ClassNotFoundException {
51        Class c = path.findClass(name);
52        if (c == null) {
53            throw new ClassNotFoundException("Didn't find class \"" + name + "\" on path: " + path);
54        }
55        return c;
56    }
57
58    @Override
59    protected URL findResource(String name) {
60        return path.findResource(name);
61    }
62
63    @Override
64    protected Enumeration<URL> findResources(String name) {
65        return path.findResources(name);
66    }
67
68    @Override
69    public String findLibrary(String name) {
70        return path.findLibrary(name);
71    }
72
73    /**
74     * Returns package information for the given package.
75     * Unfortunately, instances of this class don't really have this
76     * information, and as a non-secure {@code ClassLoader}, it isn't
77     * even required to, according to the spec. Yet, we want to
78     * provide it, in order to make all those hopeful callers of
79     * {@code myClass.getPackage().getName()} happy. Thus we construct
80     * a {@code Package} object the first time it is being requested
81     * and fill most of the fields with dummy values. The {@code
82     * Package} object is then put into the {@code ClassLoader}'s
83     * package cache, so we see the same one next time. We don't
84     * create {@code Package} objects for {@code null} arguments or
85     * for the default package.
86     *
87     * <p>There is a limited chance that we end up with multiple
88     * {@code Package} objects representing the same package: It can
89     * happen when when a package is scattered across different JAR
90     * files which were loaded by different {@code ClassLoader}
91     * instances. This is rather unlikely, and given that this whole
92     * thing is more or less a workaround, probably not worth the
93     * effort to address.
94     *
95     * @param name the name of the class
96     * @return the package information for the class, or {@code null}
97     * if there is no package information available for it
98     */
99    @Override
100    protected synchronized Package getPackage(String name) {
101        if (name != null && !name.isEmpty()) {
102            Package pack = super.getPackage(name);
103
104            if (pack == null) {
105                pack = definePackage(name, "Unknown", "0.0", "Unknown",
106                        "Unknown", "0.0", "Unknown", null);
107            }
108
109            return pack;
110        }
111
112        return null;
113    }
114
115    @Override public String toString() {
116        return getClass().getName() + "[" + path + "]";
117    }
118}
119