Build 1.0_r1(from source)

java.lang
Class ClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
Direct Known Subclasses:
BootClassLoader, PathClassLoader, SecureClassLoader, TestCaseClassLoader, TouchDexLoader

public abstract class ClassLoader
extends Object

A ClassLoader is used for loading classes.

VM Implementors Note

This class must be implemented by the VM. The documented methods and natives must be implemented to support other provided class implementations in this package.

Since:
1.0
See Also:
Class

Constructor Summary
protected ClassLoader()
          Constructs a new instance of this class with the system class loader as its parent.
protected ClassLoader(ClassLoader parentLoader)
          Constructs a new instance of this class with the given class loader as its parent.
 
Method Summary
(package private) static ClassLoader callerClassLoader()
          This method must be included, as it is used by System.load(), System.loadLibrary().
 void clearAssertionStatus()
          Clears the default, package and class assertion status of a classloader
protected  Class<?> defineClass(byte[] classRep, int offset, int length)
          Deprecated. Use defineClass(String, byte[], int, int)
protected  Class<?> defineClass(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.
protected  Class<?> defineClass(String className, byte[] classRep, int offset, int length, ProtectionDomain protectionDomain)
          Constructs a new class from an array of bytes containing a class definition in class file format and assigns the new class to the specified protection domain.
protected  Class<?> defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain)
           Defines a new class for the name, bytecodes in the byte buffer and the protection domain.
protected  Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)
          Define a new Package using the specified information.
protected  Class<?> findClass(String className)
          Overridden by subclasses, by default throws ClassNotFoundException.
protected  String findLibrary(String libName)
          Returns the absolute path of the file containing the library associated with the given name, or null.
protected  Class<?> findLoadedClass(String className)
          Attempts to find and return a class which has already been loaded by the virtual machine.
protected  URL findResource(String resName)
          Returns an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm.
protected  Enumeration<URL> findResources(String resName)
          Returns an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm.
protected  Class<?> findSystemClass(String className)
          Attempts to load a class using the system class loader.
(package private)  boolean getClassAssertionStatus(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.
(package private)  boolean getDefaultAssertionStatus()
          Returns the default assertion status
(package private) static Package getPackage(ClassLoader loader, String name)
          Attempt to locate the requested package using the given class loader.
protected  Package getPackage(String name)
          Attempt to locate the requested package.
(package private)  boolean getPackageAssertionStatus(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.
protected  Package[] getPackages()
          Return all the packages known to this class loader.
 ClassLoader getParent()
          Returns the specified ClassLoader's parent.
 URL getResource(String resName)
          Returns an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm.
 InputStream getResourceAsStream(String resName)
          Returns a stream on a resource found by looking up resName using the class loader's resource lookup algorithm.
 Enumeration<URL> getResources(String resName)
          Returns an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm.
(package private)  Object[] getSigners(Class<?> c)
          Gets the signers of a class.
(package private) static ClassLoader getStackClassLoader(int depth)
           This must be provided by the VM vendor.
static ClassLoader getSystemClassLoader()
          Returns the system class loader.
static URL getSystemResource(String resName)
          Returns an URL specifying a resource which can be found by looking up resName using the system class loader's resource lookup algorithm.
static InputStream getSystemResourceAsStream(String resName)
          Returns a stream on a resource found by looking up resName using the system class loader's resource lookup algorithm.
static Enumeration<URL> getSystemResources(String resName)
          Returns an Enumeration of URLs containing all resources which can be found by looking up resName using the system class loader's resource lookup algorithm.
(package private)  boolean isAncestorOf(ClassLoader child)
           Returns true if the receiver is ancestor of another class loader.
(package private)  boolean isSystemClassLoader()
           This method must be provided by the VM vendor, as it is used by other provided class implementations in this package.
 Class<?> loadClass(String className)
          Invoked by the Virtual Machine when resolving class references.
protected  Class<?> loadClass(String className, boolean resolve)
          Loads the class with the specified name, optionally linking the class after load.
(package private) static void loadLibraryWithClassLoader(String libName, ClassLoader loader)
          This method must be provided by the VM vendor, as it is called by java.lang.System.loadLibrary().
(package private) static void loadLibraryWithPath(String libName, ClassLoader loader, String libraryPath)
          This method must be provided by the VM vendor, as it is called by java.lang.System.load().
protected  void resolveClass(Class<?> clazz)
          Forces a class to be linked (initialized).
 void setClassAssertionStatus(String cname, boolean enable)
          Sets the assertion status of a class.
 void setDefaultAssertionStatus(boolean enable)
          Sets the default assertion status of a classloader
 void setPackageAssertionStatus(String pname, boolean enable)
          Sets the assertion status of a package.
protected  void setSigners(Class<?> c, Object[] signers)
          Sets the signers of a class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassLoader

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 new ClassLoaders.

ClassLoader

protected ClassLoader(ClassLoader parentLoader)
Constructs a new instance of this class with the given class loader as its parent.

Parameters:
parentLoader - The ClassLoader to use as the new class loaders parent.
Throws:
SecurityException - if a security manager exists and it does not allow the creation of new ClassLoaders.
NullPointerException - if the parent is null.
Method Detail

getSystemClassLoader

public static ClassLoader getSystemClassLoader()
Returns the system class loader. This is the parent for new 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 not null and the caller's class loader is not 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 it's ok to access the system class loader. If not, a SecurityException will be thrown.

Returns:
The system classLoader.
Throws:
SecurityException - if a security manager exists and it does not allow access to the system class loader.

getSystemResource

public static URL getSystemResource(String resName)
Returns an URL specifying a resource which can be found by looking up resName using the system class loader's resource lookup algorithm.

Parameters:
resName - The name of the resource to find.
Returns:
A URL specifying a system resource or null.
See Also:
Class.getResource(java.lang.String)

getSystemResources

public static Enumeration<URL> getSystemResources(String resName)
                                           throws IOException
Returns an Enumeration of URLs containing all resources which can be found by looking up resName using the system class loader's resource lookup algorithm.

Parameters:
resName - String the name of the resource to find.
Returns:
An Enumeration of URLs containing the system resources
Throws:
IOException

getSystemResourceAsStream

public static InputStream getSystemResourceAsStream(String resName)
Returns a stream on a resource found by looking up resName using the system class loader's resource lookup algorithm. Basically, the contents of the java.class.path are searched in order, looking for a path which matches the specified resource.

Parameters:
resName - The name of the resource to find.
Returns:
A stream on the resource or null.
See Also:
Class.getResourceAsStream(java.lang.String)

defineClass

@Deprecated
protected final Class<?> defineClass(byte[] classRep,
                                                int offset,
                                                int length)
                              throws ClassFormatError
Deprecated. Use defineClass(String, byte[], int, int)

Constructs a new class from an array of bytes containing a class definition in class file format.

Parameters:
classRep - A memory image of a class file.
offset - The offset into the classRep.
length - The length of the class file.
Throws:
ClassFormatError

defineClass

protected final Class<?> defineClass(String className,
                                     byte[] classRep,
                                     int offset,
                                     int length)
                              throws ClassFormatError
Constructs a new class from an array of bytes containing a class definition in class file format.

Parameters:
className - The name of the new class
classRep - A memory image of a class file
offset - The offset into the classRep
length - The length of the class file
Throws:
ClassFormatError

defineClass

protected final Class<?> defineClass(String className,
                                     byte[] classRep,
                                     int offset,
                                     int length,
                                     ProtectionDomain protectionDomain)
                              throws ClassFormatError
Constructs a new class from an array of bytes containing a class definition in class file format and assigns the new class to the specified protection domain.

Parameters:
className - The name of the new class.
classRep - A memory image of a class file.
offset - The offset into the classRep.
length - The length of the class file.
protectionDomain - The protection domain this class should belongs to.
Throws:
ClassFormatError

defineClass

protected final Class<?> defineClass(String name,
                                     ByteBuffer b,
                                     ProtectionDomain protectionDomain)
                              throws ClassFormatError

Defines a new class for the name, bytecodes in the byte buffer and the protection domain.

Parameters:
name - The name of the class to define.
b - The byte buffer containing the bytecodes of the new class.
protectionDomain - The protection domain this class belongs to.
Returns:
The defined class.
Throws:
ClassFormatError - if an invalid class file is defined.
Since:
1.5

findClass

protected Class<?> findClass(String className)
                      throws ClassNotFoundException
Overridden by subclasses, by default throws ClassNotFoundException. This method is called by loadClass() after the parent ClassLoader has failed to find a loaded class of the same name.

Parameters:
className - The name of the class to search for.
Returns:
The class or null.
Throws:
ClassNotFoundException - if the class cannot be found.

findLoadedClass

protected final Class<?> findLoadedClass(String className)
Attempts to find and return a class which has already been loaded by the virtual machine. Note that the class may not have been linked and the caller should call resolveClass() on the result if necessary.

Parameters:
className - The name of the class to search for.
Returns:
The class or null.

findSystemClass

protected final Class<?> findSystemClass(String className)
                                  throws ClassNotFoundException
Attempts to load a class using the system class loader. Note that the class has already been been linked.

Parameters:
className - The name of the class to search for.
Returns:
The class which was loaded.
Throws:
ClassNotFoundException - if the class cannot be found.

getParent

public final ClassLoader getParent()
Returns the specified ClassLoader's parent.

Returns:
The class or null.
Throws:
SecurityException - if a security manager exists and it does not allow the parent loader to be retrieved.

getResource

public URL getResource(String resName)
Returns an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return null.

Parameters:
resName - String the name of the resource to find.
Returns:
The location of the resource.
See Also:
Class.getResource(java.lang.String)

getResources

public Enumeration<URL> getResources(String resName)
                              throws IOException
Returns an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return an empty Enumeration.

Parameters:
resName - String the name of the resource to find.
Returns:
The location of the resources.
Throws:
IOException

getResourceAsStream

public InputStream getResourceAsStream(String resName)
Returns a stream on a resource found by looking up resName using the class loader's resource lookup algorithm. The default behavior is just to return null.

Parameters:
resName - String the name of the resource to find.
Returns:
A stream on the resource or null.
See Also:
Class.getResourceAsStream(java.lang.String)

loadClass

public Class<?> loadClass(String className)
                   throws ClassNotFoundException
Invoked by the Virtual Machine when resolving class references. Equivalent to loadClass(className, false);

Parameters:
className - The name of the class to search for.
Returns:
The Class object.
Throws:
ClassNotFoundException - if the class could not be found.

loadClass

protected Class<?> loadClass(String className,
                             boolean resolve)
                      throws ClassNotFoundException
Loads the class with the specified name, optionally linking the class after load. Steps are: 1) Call findLoadedClass(className) to determine if class is loaded 2) Call loadClass(className, resolveClass) on the parent loader. 3) Call findClass(className) to find the class

Parameters:
className - The name of the class to search for.
resolve - Indicates if class should be resolved after loading. Note: On the android reference implementation this parameter does not have any effect.
Returns:
The Class object.
Throws:
ClassNotFoundException - if the class could not be found.

resolveClass

protected final void resolveClass(Class<?> clazz)
Forces a class to be linked (initialized). If the class has already been linked this operation has no effect. Note that for the android reference implementation this method does not have any effect.

Parameters:
clazz - The Class to link.
Throws:
NullPointerException - if clazz is null.
See Also:
Class.getResource(java.lang.String)

isSystemClassLoader

final boolean isSystemClassLoader()

This method must be provided by the VM 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.

Returns:
true if the receiver is a system class loader
See Also:
Class.getClassLoaderImpl()

isAncestorOf

final boolean isAncestorOf(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.

Parameters:
child - A child candidate
Returns:
true if the receiver is ancestor of, or equal to, the parameter

findResource

protected URL findResource(String resName)
Returns an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return null. This should be implemented by a ClassLoader.

Parameters:
resName - The name of the resource to find.
Returns:
The location of the resource.

findResources

protected Enumeration<URL> findResources(String resName)
                                  throws IOException
Returns an Enumeration of URL which can be used to access the resources described by resName, using the class loader's resource lookup algorithm. The default behavior is just to return an empty Enumeration.

Parameters:
resName - The name of the resource to find.
Returns:
The locations of the resources.
Throws:
IOException - when an error occurs

findLibrary

protected String findLibrary(String libName)
Returns the absolute path of the file containing the library associated with the given name, or null. If null is answered, the system searches the directories specified by the system property "java.library.path".

Parameters:
libName - The name of the library to find.
Returns:
The library file name or null.

getPackage

protected Package getPackage(String name)
Attempt to locate the requested package. If no package information can be located, null is returned.

Parameters:
name - The name of the package to find
Returns:
The package requested, or null

getPackage

static Package getPackage(ClassLoader loader,
                          String name)
Attempt to locate the requested package using the given class loader. If no package information can be located, null is returned.

Parameters:
loader - The class loader to use
name - The name of the package to find
Returns:
The package requested, or null

getPackages

protected Package[] getPackages()
Return all the packages known to this class loader.

Returns:
All the packages known to this classloader

definePackage

protected Package definePackage(String name,
                                String specTitle,
                                String specVersion,
                                String specVendor,
                                String implTitle,
                                String implVersion,
                                String implVendor,
                                URL sealBase)
                         throws IllegalArgumentException
Define a new Package using the specified information.

Parameters:
name - The name of the package
specTitle - The title of the specification for the Package
specVersion - The version of the specification for the Package
specVendor - The vendor of the specification for the Package
implTitle - The implementation title of the Package
implVersion - The implementation version of the Package
implVendor - The specification vendor of the Package
sealBase - If sealBase is null, the package is left unsealed. Otherwise, the the package is sealed using this URL.
Returns:
The Package created
Throws:
IllegalArgumentException - if the Package already exists

getSigners

final Object[] getSigners(Class<?> c)
Gets the signers of a class.

Parameters:
c - The Class object
Returns:
signers The signers for the class

setSigners

protected final void setSigners(Class<?> c,
                                Object[] signers)
Sets the signers of a class.

Parameters:
c - The Class object
signers - The signers for the class

getStackClassLoader

static final ClassLoader getStackClassLoader(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:

Parameters:
depth - the stack depth of the requested ClassLoader
Returns:
the ClassLoader at the specified depth

callerClassLoader

static ClassLoader callerClassLoader()
This method must be included, as it is used by System.load(), System.loadLibrary(). The reference implementation of this method uses the getStackClassLoader() method. Returns the ClassLoader of the method that called the caller. i.e. A.x() calls B.y() calls callerClassLoader(), A's ClassLoader will be returned. Returns null for the bootstrap ClassLoader.

Returns:
a ClassLoader or null for the bootstrap ClassLoader

loadLibraryWithClassLoader

static void loadLibraryWithClassLoader(String libName,
                                       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.

Parameters:
libName - the name of the library to load
loader - the classloader in which to load the library
Throws:
UnsatisfiedLinkError - if the library could not be loaded
SecurityException - if the library was not allowed to be loaded

loadLibraryWithPath

static void loadLibraryWithPath(String libName,
                                ClassLoader loader,
                                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.

Parameters:
libName - the name of the library to load
loader - the classloader in which to load the library
libraryPath - the library path to search, or null
Throws:
UnsatisfiedLinkError - if the library could not be loaded

setClassAssertionStatus

public void setClassAssertionStatus(String cname,
                                    boolean enable)
Sets the assertion status of a class.

Parameters:
cname - Class name
enable - Enable or disable assertion

setPackageAssertionStatus

public void setPackageAssertionStatus(String pname,
                                      boolean enable)
Sets the assertion status of a package.

Parameters:
pname - Package name
enable - Enable or disable assertion

setDefaultAssertionStatus

public void setDefaultAssertionStatus(boolean enable)
Sets the default assertion status of a classloader

Parameters:
enable - Enable or disable assertion

clearAssertionStatus

public void clearAssertionStatus()
Clears the default, package and class assertion status of a classloader


getClassAssertionStatus

boolean getClassAssertionStatus(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.

Parameters:
cname - the name of class.
Returns:
the assertion status.

getPackageAssertionStatus

boolean getPackageAssertionStatus(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.

Parameters:
pname - the name of package.
Returns:
the assertion status.

getDefaultAssertionStatus

boolean getDefaultAssertionStatus()
Returns the default assertion status

Returns:
boolean the default assertion status.

Build 1.0_r1(from source)

Please submit a feedback, bug or feature