FileDocCategorySizeDatePackage
ClassLoader.javaAPI DocAndroid 1.5 API40451Wed May 06 22:41:04 BST 2009java.lang

ClassLoader

public abstract class ClassLoader extends Object
Loads classes and resources from a repository. One or more class loaders are installed at runtime. These are consulted whenever the runtime system needs a specific class that is not yet available in-memory. Typically, class loaders are grouped into a tree where child class loaders delegate all requests to parent class loaders. Only if the parent class loader cannot satisfy the request, the child class loader itself tries to handle it.

{@code ClassLoader} is an abstract class that implements the common infrastructure required by all class loaders. Android provides several concrete implementations of the class, with {@link dalvik.system.PathClassLoader} being the one typically used. Other applications may implement subclasses of {@code ClassLoader} to provide special ways for loading classes.

since
Android 1.0
see
Class

Fields Summary
private ClassLoader
parent
The parent ClassLoader.
private Map
packages
The packages known to the class loader.
Constructors Summary
protected ClassLoader()
Constructs a new instance of this class with the system class loader as its parent.

throws
SecurityException if a security manager exists and it does not allow the creation of a new {@code ClassLoader}.
since
Android 1.0

        this(getSystemClassLoader(), false);
    
protected ClassLoader(ClassLoader parentLoader)
Constructs a new instance of this class with the specified class loader as its parent.

param
parentLoader The {@code ClassLoader} to use as the new class loader's parent.
throws
SecurityException if a security manager exists and it does not allow the creation of new a new {@code ClassLoader}.
since
Android 1.0

        this(parentLoader, false);
    
ClassLoader(ClassLoader parentLoader, boolean nullAllowed)

        SecurityManager smgr = System.getSecurityManager();
        if (smgr != null) {
            smgr.checkCreateClassLoader();
        }
        
        if (parentLoader == null && !nullAllowed) {
            throw new NullPointerException(
                    "Parent ClassLoader may not be null");
        }

        parent = parentLoader;
    
Methods Summary
public voidclearAssertionStatus()
Sets the default assertion status for this class loader to {@code false} and removes any package default and class assertion status settings.

Note: This method does nothing in the Android reference implementation.

since
Android 1.0

        return;
    
private static java.lang.ClassLoadercreateSystemClassLoader()
Create the system class loader. Note this is NOT the bootstrap class loader (which is managed by the VM). We use a null value for the parent to indicate that the bootstrap loader is our parent.


                                             
        
        String classPath = System.getProperty("java.class.path", ".");

        // String[] paths = classPath.split(":");
        // URL[] urls = new URL[paths.length];
        // for (int i = 0; i < paths.length; i++) {
        // try {
        // urls[i] = new URL("file://" + paths[i]);
        // }
        // catch (Exception ex) {
        // ex.printStackTrace();
        // }
        // }
        //            
        // return new java.net.URLClassLoader(urls, null);

        // TODO Make this a java.net.URLClassLoader once we have those?
        return new PathClassLoader(classPath, BootClassLoader.getInstance());
    
protected final java.lang.ClassdefineClass(java.lang.String className, byte[] classRep, int offset, int length)
Constructs a new class from an array of bytes containing a class definition in class file format.

param
className the expected name of the new class, may be {@code null} if not known.
param
classRep the memory image of a class file.
param
offset the offset into {@code classRep}.
param
length the length of the class file.
return
the {@code Class} object created from the specified subset of data in {@code classRep}.
throws
ClassFormatError if {@code classRep} does not contain a valid class.
throws
IndexOutOfBoundsException if {@code offset < 0}, {@code length < 0} or if {@code offset + length} is greater than the length of {@code classRep}.
since
Android 1.0


        // TODO Define a default ProtectionDomain on first use
        return defineClass(className, classRep, offset, length, null);
    
protected final java.lang.ClassdefineClass(java.lang.String className, byte[] classRep, int offset, int length, java.security.ProtectionDomain protectionDomain)
Constructs a new class from an array of bytes containing a class definition in class file format and assigns the specified protection domain to the new class. If the provided protection domain is {@code null} then a default protection domain is assigned to the class.

param
className the expected name of the new class, may be {@code null} if not known.
param
classRep the memory image of a class file.
param
offset the offset into {@code classRep}.
param
length the length of the class file.
param
protectionDomain the protection domain to assign to the loaded class, may be {@code null}.
return
the {@code Class} object created from the specified subset of data in {@code classRep}.
throws
ClassFormatError if {@code classRep} does not contain a valid class.
throws
IndexOutOfBoundsException if {@code offset < 0}, {@code length < 0} or if {@code offset + length} is greater than the length of {@code classRep}.
throws
NoClassDefFoundError if {@code className} is not equal to the name of the class contained in {@code classRep}.
since
Android 1.0


        return VMClassLoader.defineClass(this, className, classRep, offset, length,
                protectionDomain);
    
protected final java.lang.ClassdefineClass(java.lang.String name, java.nio.ByteBuffer b, java.security.ProtectionDomain protectionDomain)
Defines a new class with the specified name, byte code from the byte buffer and the optional protection domain. If the provided protection domain is {@code null} then a default protection domain is assigned to the class.

param
name the expected name of the new class, may be {@code null} if not known.
param
b the byte buffer containing the byte code of the new class.
param
protectionDomain the protection domain to assign to the loaded class, may be {@code null}.
return
the {@code Class} object created from the data in {@code b}.
throws
ClassFormatError if {@code b} does not contain a valid class.
throws
NoClassDefFoundError if {@code className} is not equal to the name of the class contained in {@code b}.
since
Android 1.0


        byte[] temp = new byte[b.remaining()];
        b.get(temp);
        return defineClass(name, temp, 0, temp.length, protectionDomain);
    
protected final java.lang.ClassdefineClass(byte[] classRep, int offset, int length)
Constructs a new class from an array of bytes containing a class definition in class file format.

param
classRep the memory image of a class file.
param
offset the offset into {@code classRep}.
param
length the length of the class file.
return
the {@code Class} object created from the specified subset of data in {@code classRep}.
throws
ClassFormatError if {@code classRep} does not contain a valid class.
throws
IndexOutOfBoundsException if {@code offset < 0}, {@code length < 0} or if {@code offset + length} is greater than the length of {@code classRep}.
deprecated
Use {@link #defineClass(String, byte[], int, int)}
since
Android 1.0


        return VMClassLoader.defineClass(this, classRep, offset, length, null);
    
protected java.lang.PackagedefinePackage(java.lang.String name, java.lang.String specTitle, java.lang.String specVersion, java.lang.String specVendor, java.lang.String implTitle, java.lang.String implVersion, java.lang.String implVendor, java.net.URL sealBase)
Defines and returns a new {@code Package} using the specified information. If {@code sealBase} is {@code null}, the package is left unsealed. Otherwise, the package is sealed using this URL.

param
name the name of the package.
param
specTitle the title of the specification.
param
specVersion the version of the specification.
param
specVendor the vendor of the specification.
param
implTitle the implementation title.
param
implVersion the implementation version.
param
implVendor the specification vendor.
param
sealBase the URL used to seal this package or {@code null} to leave the package unsealed.
return
the {@code Package} object that has been created.
throws
IllegalArgumentException if a package with the specified name already exists.
since
Android 1.0


        synchronized (packages) {
            if (packages.containsKey(name)) {
                throw new IllegalArgumentException("Package " + name + " already defined");
            }

            Package newPackage = new Package(name, specTitle, specVersion, specVendor, implTitle,
                    implVersion, implVendor, sealBase);

            packages.put(name, newPackage);

            return newPackage;
        }
    
protected java.lang.ClassfindClass(java.lang.String className)
Overridden by subclasses, throws a {@code ClassNotFoundException} by default. This method is called by {@code loadClass} after the parent {@code ClassLoader} has failed to find a loaded class of the same name.

param
className the name of the class to look for.
return
the {@code Class} object that is found.
throws
ClassNotFoundException if the class cannot be found.
since
Android 1.0

        throw new ClassNotFoundException(className);
    
protected java.lang.StringfindLibrary(java.lang.String libName)
Returns the absolute path of the native library with the specified name, or {@code null}. If this method returns {@code null} then the virtual machine searches the directories specified by the system property "java.library.path".

This implementation always returns {@code null}.

param
libName the name of the library to find.
return
the absolute path of the library.
since
Android 1.0

        return null;
    
protected final java.lang.ClassfindLoadedClass(java.lang.String className)
Returns the class with the specified name if it has already been loaded by the virtual machine or {@code null} if it has not yet been loaded.

param
className the name of the class to look for.
return
the {@code Class} object or {@code null} if the requested class has not been loaded.
since
Android 1.0

        ClassLoader loader;
        if (this == BootClassLoader.getInstance())
            loader = null;
        else
            loader = this;
        return VMClassLoader.findLoadedClass(loader, className);
    
protected java.net.URLfindResource(java.lang.String resName)
Finds the URL of the resource with the specified name. This implementation just returns {@code null}; it should be overridden in subclasses.

param
resName the name of the resource to find.
return
the {@code URL} object for the requested resource.
since
Android 1.0

        return null;
    
protected java.util.EnumerationfindResources(java.lang.String resName)
Finds an enumeration of URLs for the resource with the specified name. This implementation just returns an empty {@code Enumeration}; it should be overridden in subclasses.

param
resName the name of the resource to find.
return
an enumeration of {@code URL} objects for the requested resource.
throws
IOException if an I/O error occurs.
since
Android 1.0

        return EmptyEnumeration.getInstance();
    
protected final java.lang.ClassfindSystemClass(java.lang.String className)
Finds the class with the specified name, loading it using the system class loader if necessary.

param
className the name of the class to look for.
return
the {@code Class} object with the requested {@code className}.
throws
ClassNotFoundException if the class can not be found.
since
Android 1.0

        return Class.forName(className, false, getSystemClassLoader());
    
booleangetClassAssertionStatus(java.lang.String cname)
Returns the assertion status of the named class Returns the assertion status of the class or nested class if it has been set. Otherwise returns the assertion status of its package or superpackage if that has been set. Otherwise returns the default assertion status. Returns 1 for enabled and 0 for disabled.

return
the assertion status.
param
cname the name of class.

        return false;
    
booleangetDefaultAssertionStatus()
Returns the default assertion status

return
the default assertion status.

        return false;
    
protected java.lang.PackagegetPackage(java.lang.String name)
Returns the package with the specified name. Package information is searched in this class loader.

param
name the name of the package to find.
return
the package with the requested name; {@code null} if the package can not be found.
since
Android 1.0

        synchronized (packages) {
            Package p = packages.get(name);
            return p;
        }
    
static java.lang.PackagegetPackage(java.lang.ClassLoader loader, java.lang.String name)
Gets the package with the specified name, searching it in the specified class loader.

param
loader the class loader to search the package in.
param
name the name of the package to find.
return
the package with the requested name; {@code null} if the package can not be found.
since
Android 1.0

        return loader.getPackage(name);
    
booleangetPackageAssertionStatus(java.lang.String pname)
Returns the assertion status of the named package Returns the assertion status of the named package or superpackage if that has been set. Otherwise returns the default assertion status. Returns 1 for enabled and 0 for disabled.

return
the assertion status.
param
pname the name of package.

        return false;
    
protected java.lang.Package[]getPackages()
Returns all the packages known to this class loader.

return
an array with all packages known to this class loader.
since
Android 1.0

        synchronized (packages) {
            Collection<Package> col = packages.values();
            Package[] result = new Package[col.size()];
            col.toArray(result);
            return result;
        }
    
public final java.lang.ClassLoadergetParent()
Returns this class loader's parent.

return
this class loader's parent or {@code null}.
throws
SecurityException if a security manager exists and it does not allow to retrieve the parent class loader.
since
Android 1.0

        SecurityManager smgr = System.getSecurityManager();
        if (smgr != null) {
            smgr.checkPermission(new RuntimePermission("getClassLoader"));
        }

        return parent;
    
public java.net.URLgetResource(java.lang.String resName)
Returns the URL of the resource with the specified name. This implementation first tries to use the parent class loader to find the resource; if this fails then {@link #findResource(String)} is called to find the requested resource.

param
resName the name of the resource to find.
return
the {@code URL} object for the requested resource or {@code null} if either the resource can not be found or a security manager does not allow to access the resource.
see
Class#getResource
since
Android 1.0

        URL resource = null;

        resource = parent.getResource(resName);

        if (resource == null) {
            resource = findResource(resName);
        }

        return resource;
    
public java.io.InputStreamgetResourceAsStream(java.lang.String resName)
Returns a stream for the resource with the specified name. See {@link #getResource(String)} for a description of the lookup algorithm used to find the resource.

return
a stream for the resource or {@code null} if either the resource can not be found or a security manager does not allow to access the resource.
param
resName the name of the resource to find.
see
Class#getResourceAsStream
since
Android 1.0

        try {
            URL url = getResource(resName);
            if (url != null) {
                return url.openStream();
            }
        } catch (IOException ex) {
            // Don't want to see the exception.
        }

        return null;
    
public java.util.EnumerationgetResources(java.lang.String resName)
Returns an enumeration of URLs for the resource with the specified name. This implementation first uses this class loader's parent to find the resource, then it calls {@link #findResources(String)} to get additional URLs. The returned enumeration contains the {@code URL} objects of both find operations.

return
an enumeration of {@code URL} objects for the requested resource.
param
resName the name of the resource to find.
throws
IOException if an I/O error occurs.
since
Android 1.0


        Enumeration first = parent.getResources(resName);
        Enumeration second = findResources(resName);

        return new TwoEnumerationsInOne(first, second);
    
final java.lang.Object[]getSigners(java.lang.Class c)
Gets the signers of the specified class. This implementation returns {@code null}.

param
c the {@code Class} object for which to get the signers.
return
signers the signers of {@code c}.
since
Android 1.0

        return null;
    
static final java.lang.ClassLoadergetStackClassLoader(int depth)

This must be provided by the VM vendor. It is used by SecurityManager.checkMemberAccess() with depth = 3. Note that checkMemberAccess() assumes the following stack when called:

< user code &gt; <- want this class
Class.getDeclared*();
Class.checkMemberAccess();
SecurityManager.checkMemberAccess(); <- current frame

Returns the ClassLoader of the method (including natives) at the specified depth on the stack of the calling thread. Frames representing the VM implementation of java.lang.reflect are not included in the list.

Notes:
  • This method operates on the defining classes of methods on stack. NOT the classes of receivers.
  • The item at depth zero is the caller of this method

param
depth the stack depth of the requested ClassLoader
return
the ClassLoader at the specified depth

        Class<?>[] stack = VMStack.getClasses(depth + 1, false);
        if(stack.length < depth + 1) {
            return null;
        }
        return stack[depth].getClassLoader(); 
    
public static java.lang.ClassLoadergetSystemClassLoader()
Returns the system class loader. This is the parent for new {@code ClassLoader} instances and is typically the class loader used to start the application. If a security manager is present and the caller's class loader is neither {@code null} nor the same as or an ancestor of the system class loader, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure that it is ok to access the system class loader. If not, a {@code SecurityException} is thrown.

return
the system class loader.
throws
SecurityException if a security manager exists and it does not allow access to the system class loader.
since
Android 1.0

        SecurityManager smgr = System.getSecurityManager();
        if (smgr != null) {
            ClassLoader caller = VMStack.getCallingClassLoader();
            if (caller != null && !caller.isAncestorOf(SystemClassLoader.loader)) {
                smgr.checkPermission(new RuntimePermission("getClassLoader"));
            }
        }

        return SystemClassLoader.loader;
    
public static java.net.URLgetSystemResource(java.lang.String resName)
Finds the URL of the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource.

return
the {@code URL} object for the requested resource or {@code null} if the resource can not be found.
param
resName the name of the resource to find.
see
Class#getResource
since
Android 1.0

        return SystemClassLoader.loader.getResource(resName);
    
public static java.io.InputStreamgetSystemResourceAsStream(java.lang.String resName)
Returns a stream for the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource. Basically, the contents of the java.class.path are searched in order, looking for a path which matches the specified resource.

return
a stream for the resource or {@code null}.
param
resName the name of the resource to find.
see
Class#getResourceAsStream
since
Android 1.0

        return SystemClassLoader.loader.getResourceAsStream(resName);
    
public static java.util.EnumerationgetSystemResources(java.lang.String resName)
Returns an enumeration of URLs for the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource.

return
an enumeration of {@code URL} objects containing the requested resources.
param
resName the name of the resource to find.
throws
IOException if an I/O error occurs.
since
Android 1.0

        return SystemClassLoader.loader.getResources(resName);
    
final booleanisAncestorOf(java.lang.ClassLoader child)

Returns true if the receiver is ancestor of another class loader. It also returns true if the two class loader are equal.

Note that this method has package visibility only. It is defined here to avoid the security manager check in getParent, which would be required to implement this method anywhere else. The method is also required in other places where class loaders are accesses.

param
child A child candidate
return
{@code true} if the receiver is ancestor of, or equal to, the parameter

        for (ClassLoader current = child; current != null;
                current = child.parent) {
            if (current == this) {
                return true;
            }
        }
        return false;
    
final booleanisSystemClassLoader()
Indicates whether this class loader is the system class loader. This method must be provided by the virtual machine vendor, as it is used by other provided class implementations in this package. A sample implementation of this method is provided by the reference implementation. This method is used by SecurityManager.classLoaderDepth(), currentClassLoader() and currentLoadedClass(). Returns true if the receiver is a system class loader.

Note that this method has package visibility only. It is defined here to avoid the security manager check in getSystemClassLoader, which would be required to implement this method anywhere else.

return
{@code true} if the receiver is a system class loader
see
Class#getClassLoaderImpl()

        return false;
    
public java.lang.ClassloadClass(java.lang.String className)
Loads the class with the specified name. Invoking this method is equivalent to calling {@code loadClass(className, false)}.

Note: In the Android reference implementation, the second parameter of {@link #loadClass(String, boolean)} is ignored anyway.

return
the {@code Class} object.
param
className the name of the class to look for.
throws
ClassNotFoundException if the class can not be found.
since
Android 1.0

        return loadClass(className, false);
    
protected java.lang.ClassloadClass(java.lang.String className, boolean resolve)
Loads the class with the specified name, optionally linking it after loading. The following steps are performed:
  1. Call {@link #findLoadedClass(String)} to determine if the requested class has already been loaded.
  2. If the class has not yet been loaded: Invoke this method on the parent class loader.
  3. If the class has still not been loaded: Call {@link #findClass(String)} to find the class.

Note: In the Android reference implementation, the {@code resolve} parameter is ignored; classes are never linked.

return
the {@code Class} object.
param
className the name of the class to look for.
param
resolve Indicates if the class should be resolved after loading. This parameter is ignored on the Android reference implementation; classes are not resolved.
throws
ClassNotFoundException if the class can not be found.
since
Android 1.0

        Class<?> clazz = findLoadedClass(className);

        if (clazz == null) {
            try {
                clazz = parent.loadClass(className, false);
            } catch (ClassNotFoundException e) {
                // Don't want to see this.
            }

            if (clazz == null) {
                clazz = findClass(className);
            }
        }

        return clazz;
    
static voidloadLibraryWithClassLoader(java.lang.String libName, java.lang.ClassLoader loader)
This method must be provided by the VM vendor, as it is called by java.lang.System.loadLibrary(). System.loadLibrary() cannot call Runtime.loadLibrary() because this method loads the library using the ClassLoader of the calling method. Loads and links the library specified by the argument.

param
libName the name of the library to load
param
loader the classloader in which to load the library
throws
UnsatisfiedLinkError if the library could not be loaded
throws
SecurityException if the library was not allowed to be loaded

Note: This method does nothing in the Android reference implementation.

        return;
    
static voidloadLibraryWithPath(java.lang.String libName, java.lang.ClassLoader loader, java.lang.String libraryPath)
This method must be provided by the VM vendor, as it is called by java.lang.System.load(). System.load() cannot call Runtime.load() because the library is loaded using the ClassLoader of the calling method. Loads and links the library specified by the argument. No security check is done.

Note: This method does nothing in the Android reference implementation.

param
libName the name of the library to load
param
loader the classloader in which to load the library
param
libraryPath the library path to search, or null
throws
UnsatisfiedLinkError if the library could not be loaded

        return;
    
protected final voidresolveClass(java.lang.Class clazz)
Forces a class to be linked (initialized). If the class has already been linked this operation has no effect.

Note: In the Android reference implementation, this method has no effect.

param
clazz the class to link.
since
Android 1.0

        // no-op, doesn't make sense on android.
    
public voidsetClassAssertionStatus(java.lang.String cname, boolean enable)
Sets the assertion status of the class with the specified name.

Note: This method does nothing in the Android reference implementation.

param
cname the name of the class for which to set the assertion status.
param
enable the new assertion status.
since
Android 1.0

        return;
    
public voidsetDefaultAssertionStatus(boolean enable)
Sets the default assertion status for this class loader.

Note: This method does nothing in the Android reference implementation.

param
enable the new assertion status.
since
Android 1.0

        return;
    
public voidsetPackageAssertionStatus(java.lang.String pname, boolean enable)
Sets the assertion status of the package with the specified name.

Note: This method does nothing in the Android reference implementation.

param
pname the name of the package for which to set the assertion status.
param
enable the new assertion status.
since
Android 1.0

        return;
    
protected final voidsetSigners(java.lang.Class c, java.lang.Object[] signers)
Sets the signers of the specified class. This implementation does nothing.

param
c the {@code Class} object for which to set the signers.
param
signers the signers for {@code c}.
since
Android 1.0

        return;