加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20090301101354/http://developer.android.com:80/reference/java/lang/ClassLoader.html
public abstract class

ClassLoader

extends Object
java.lang.Object
   ↳ java.lang.ClassLoader
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

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.

See Also

Summary

Protected Constructors
ClassLoader()
Constructs a new instance of this class with the system class loader as its parent.
ClassLoader(ClassLoader parentLoader)
Constructs a new instance of this class with the given class loader as its parent.
Public Methods
void clearAssertionStatus()
Clears the default, package and class assertion status of a classloader
final 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.
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.
Class<?> loadClass(String className)
Invoked by the Virtual Machine when resolving class references.
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 Methods
final Class<?> defineClass(byte[] classRep, int offset, int length)
This method is deprecated. Use defineClass(String, byte[], int, int)
final Class<?> defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain)

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

final 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.
final 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.
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.
Class<?> findClass(String className)
Overridden by subclasses, by default throws ClassNotFoundException.
String findLibrary(String libName)
Returns the absolute path of the file containing the library associated with the given name, or null.
final Class<?> findLoadedClass(String className)
Attempts to find and return a class which has already been loaded by the virtual machine.
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.
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.
final Class<?> findSystemClass(String className)
Attempts to load a class using the system class loader.
Package getPackage(String name)
Attempt to locate the requested package.
Package[] getPackages()
Return all the packages known to this class loader.
Class<?> loadClass(String className, boolean resolve)
Loads the class with the specified name, optionally linking the class after load.
final void resolveClass(Class<?> clazz)
Forces a class to be linked (initialized).
final void setSigners(Class<?> c, Object[] signers)
Sets the signers of a class.
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

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.

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.

Public Methods

public void clearAssertionStatus ()

Clears the default, package and class assertion status of a classloader

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.

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.

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.

public 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. 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

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.

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.

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.

public 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.

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

public Class<?> loadClass (String className)

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.

public void setClassAssertionStatus (String cname, boolean enable)

Sets the assertion status of a class.

Parameters
cname Class name
enable Enable or disable assertion

public void setDefaultAssertionStatus (boolean enable)

Sets the default assertion status of a classloader

Parameters
enable Enable or disable assertion

public void setPackageAssertionStatus (String pname, boolean enable)

Sets the assertion status of a package.

Parameters
pname Package name
enable Enable or disable assertion

Protected Methods

protected final Class<?> defineClass (byte[] classRep, int offset, int length)

This method is 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.

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

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.

protected final 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.

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.

protected final 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.

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

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.

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

protected Class<?> findClass (String className)

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.

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.

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.

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.

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

protected final Class<?> findSystemClass (String className)

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.

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

protected Package[] getPackages ()

Return all the packages known to this class loader.

Returns
  • All the packages known to this classloader

protected Class<?> loadClass (String className, boolean resolve)

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.

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.

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