Reference:Java.lang.ClassLoader
From Triled Wiki
java.lang
public abstract class java.lang.ClassLoader
| java.lang.Object | ||
| java.lang.ClassLoader | ||
A ClassLoader is used for loading classes.
[edit] 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.[edit] Known Direct Subclasses
SecureClassLoader[edit] Known Indirect Subclasses
See Also
[edit] Summary
[edit] 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. | |||||
[edit] 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) | ||||
| Answers 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) | ||||
| Answers a stream on a resource found by looking up resName using the class loader's resource lookup algorithm. | |||||
| Enumeration | getResources (String resName) | ||||
| Answers 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) | |||
| Answers 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) | |||
| Answers a stream on a resource found by looking up resName using the system class loader's resource lookup algorithm. | |||||
| static | Enumeration | getSystemResources (String resName) | |||
| Answers 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. | |||||
[edit] 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) | ||||
| Answers 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) | ||||
| Answers an URL which can be used to access the resource described by resName, using the class loader's resource lookup algorithm. | |||||
| Enumeration | findResources (String resName) | ||||
| Answers 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 | , java.lang.Object[])">setSigners (Class c, Object[ ] signers) | |||
| Sets the signers of a class. | |||||
[edit] Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
[edit] Details
[edit] Protected Constructors
[edit] 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. |
|---|
[edit] 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. |
[edit] Public Methods
[edit] public void clearAssertionStatus()
Clears the default, package and class assertion status of a classloader
[edit] 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. |
|---|
[edit] public URL getResource(String resName)
Answers 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
[edit] public InputStream getResourceAsStream(String resName)
Answers 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
[edit] public Enumeration getResources(String resName)
Answers 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 |
|---|
[edit] 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. |
|---|
[edit] public static URL getSystemResource(String resName)
Answers 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
[edit] public static InputStream getSystemResourceAsStream(String resName)
Answers 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
[edit] public static Enumeration getSystemResources(String resName)
Answers 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 |
|---|
[edit] 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. |
|---|
[edit] public void setClassAssertionStatus(String cname, boolean enable)
Sets the assertion status of a class.
Parameters
| cname | Class name |
|---|---|
| enable | Enable or disable assertion |
[edit] public void setDefaultAssertionStatus(boolean enable)
Sets the default assertion status of a classloader
Parameters
| enable | Enable or disable assertion |
|---|
[edit] public void setPackageAssertionStatus(String pname, boolean enable)
Sets the assertion status of a package.
Parameters
| pname | Package name |
|---|---|
| enable | Enable or disable assertion |
[edit] Protected Methods
[edit] 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. |
Throws
| ClassFormatError |
|---|
[edit] 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. |
|---|
[edit] 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. |
Throws
| ClassFormatError |
|---|
[edit] 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 |
Throws
| ClassFormatError |
|---|
[edit] 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 |
|---|
[edit] 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. |
|---|
[edit] protected String findLibrary(String libName)
Answers 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.
[edit] 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.
[edit] protected URL findResource(String resName)
Answers 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.
[edit] protected Enumeration findResources(String resName)
Answers 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 |
|---|
[edit] 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. |
|---|
[edit] 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
[edit] protected Package[ ]getPackages()
Return all the packages known to this class loader.
Returns
- All the packages known to this classloader
[edit] 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. |
Returns
- The Class object.
Throws
| ClassNotFoundException | if the class could not be found. |
|---|
[edit] 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.
Parameters
| clazz | The Class to link. |
|---|
Throws
| NullPointerException | if clazz is null. |
|---|
See Also
[edit] 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 |
[edit] References
- Original documentation page for Android SDK build m5-rc15e - 14 apr 2008 17:27
