ClassLoader
abstract class ClassLoader
| kotlin.Any | |
| ↳ | java.lang.ClassLoader |
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
Every Class object contains a reference to the ClassLoader that defined it.
Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.
Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
Class loaders may typically be used by security managers to indicate security domains.
In addition to loading classes, a class loader is also responsible for locating resources. A resource is some data (a ".class" file, configuration data, or an image for example) that is identified with an abstract '/'-separated path name. Resources are typically packaged with an application or library so that they can be located by code in the application or library. In some cases, the resources are included so that they can be located by other libraries.
The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will usually delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.
Class loaders that support concurrent loading of classes are known as isRegisteredAsParallelCapable() parallel capable class loaders and are required to register themselves at their class initialization time by invoking the registerAsParallelCapable ClassLoader.registerAsParallelCapable method. Note that the ClassLoader class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable. In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see #loadClass methods).
Run-time Built-in Class Loaders
The Java run-time has the following built-in class loaders:- Bootstrap class loader. It is the virtual machine's built-in class loader, typically represented as
null, and does not have a parent. - System class loader. It is also known as application class loader and is distinct from the platform class loader. The system class loader is typically used to define classes on the application class path, module path, and JDK-specific tools. The platform class loader is the parent or an ancestor of the system class loader, so the system class loader can load platform classes by delegating to its parent.
Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application. The method defineClass converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using Class.newInstance.
The methods and constructors of objects created by a class loader may reference other classes. To determine the class(es) referred to, the Java virtual machine invokes the #loadClass method of the class loader that originally created the class.
For example, an application could create a network class loader to download class files from a server. Sample code might look like:
ClassLoader loader = new NetworkClassLoader(host, port); Object main = loader.loadClass("Main", true).newInstance(); . . .
The network class loader subclass must define the methods findClass and loadClassData to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method #defineClass to create a class instance. A sample implementation is:
class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the class data from the connection . . . } }
Binary names
Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by The Java Language Specification.
Examples of valid class names include:
"java.lang.String" "javax.swing.JSpinner$DefaultEditor" "java.security.KeyStore$Builder$FileBuilder$1" "java.net.URLClassLoader$3$1"
Any package name provided as a String parameter to methods in ClassLoader must be either the empty string (denoting an unnamed package) or a fully qualified name as defined by The Java Language Specification.
Summary
| Protected constructors | |
|---|---|
|
Creates a new class loader using the |
|
ClassLoader(parent: ClassLoader!)Creates a new class loader using the specified parent class loader for delegation. |
|
| Public methods | |
|---|---|
| open Unit |
Sets the default assertion status for this class loader to |
| ClassLoader! |
Returns the parent class loader for delegation. |
| open URL! |
getResource(name: String!)Finds the resource with the given name. |
| open InputStream! |
getResourceAsStream(name: String!)Returns an input stream for reading the specified resource. |
| open Enumeration<URL!>! |
getResources(name: String!)Finds all the resources with the given name. |
| open static ClassLoader! |
Returns the system class loader. |
| open static URL! |
getSystemResource(name: String!)Find a resource of the specified name from the search path used to load classes. |
| open static InputStream! |
getSystemResourceAsStream(name: String!)Open for reading, a resource of the specified name from the search path used to load classes. |
| open static Enumeration<URL!>! |
getSystemResources(name: String!)Finds all resources of the specified name from the search path used to load classes. |
| open Class<*>! |
Loads the class with the specified binary name. |
| open Unit |
setClassAssertionStatus(className: String!, enabled: Boolean)Sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein. |
| open Unit |
setDefaultAssertionStatus(enabled: Boolean)Sets the default assertion status for this class loader. |
| open Unit |
setPackageAssertionStatus(packageName: String!, enabled: Boolean)Sets the package default assertion status for the named package. |
| Protected methods | |
|---|---|
| Class<*>! |
defineClass(b: ByteArray!, off: Int, len: Int)Converts an array of bytes into an instance of class |
| Class<*>! |
defineClass(name: String!, b: ByteArray!, off: Int, len: Int)Converts an array of bytes into an instance of class |
| Class<*>! |
defineClass(name: String!, b: ByteArray!, off: Int, len: Int, protectionDomain: ProtectionDomain!)Converts an array of bytes into an instance of class |
| Class<*>! |
defineClass(name: String!, b: ByteBuffer!, protectionDomain: ProtectionDomain!)Converts a |
| open Package! |
definePackage(name: String!, specTitle: String!, specVersion: String!, specVendor: String!, implTitle: String!, implVersion: String!, implVendor: String!, sealBase: URL!)Defines a package by name in this |
| open Class<*>! |
Finds the class with the specified binary name. |
| open String! |
findLibrary(libname: String!)Returns the absolute path name of a native library. |
| Class<*>! |
findLoadedClass(name: String!)Returns the class with the given binary name if this loader has been recorded by the Java virtual machine as an initiating loader of a class with that binary name. |
| open URL! |
findResource(name: String!)Finds the resource with the given name. |
| open Enumeration<URL!>! |
findResources(name: String!)Returns an enumeration of |
| Class<*>! |
findSystemClass(name: String!)Finds a class with the specified binary name, loading it if necessary. |
| open Package! |
getPackage(name: String!)Finds a package by name in this class loader and its ancestors. |
| open Array<Package!>! |
Returns all of the |
| open Class<*>! |
Loads the class with the specified binary name. |
| open static Boolean |
Registers the caller as |
| Unit |
resolveClass(c: Class<*>!)Links the specified class. |
| Unit |
setSigners(c: Class<*>!, signers: Array<Any!>!)Sets the signers of a class. |
Protected constructors
ClassLoader
protected ClassLoader()
Creates a new class loader using the ClassLoader returned by the method getSystemClassLoader() as the parent class loader.
If there is a security manager, its checkCreateClassLoader method is invoked. This may result in a security exception.
| Exceptions | |
|---|---|
java.lang.SecurityException |
If a security manager exists and its checkCreateClassLoader method doesn't allow creation of a new class loader. |
ClassLoader
protected ClassLoader(parent: ClassLoader!)
Creates a new class loader using the specified parent class loader for delegation.
If there is a security manager, its checkCreateClassLoader method is invoked. This may result in a security exception.
| Parameters | |
|---|---|
parent |
ClassLoader!: The parent class loader |
| Exceptions | |
|---|---|
java.lang.SecurityException |
If a security manager exists and its checkCreateClassLoader method doesn't allow creation of a new class loader. |
Public methods
clearAssertionStatus
open fun clearAssertionStatus(): Unit
Sets the default assertion status for this class loader to false and discards any package defaults or class assertion status settings associated with the class loader. This method is provided so that class loaders can be made to ignore any command line or persistent assertion status settings and "start with a clean slate." Android-note: AssertionStatuses are unsupported. This method is a no-op.
getParent
fun getParent(): ClassLoader!
Returns the parent class loader for delegation. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class loader's parent is the bootstrap class loader.
| Return | |
|---|---|
ClassLoader! |
The parent ClassLoader |
| Exceptions | |
|---|---|
java.lang.SecurityException |
If a security manager is present, and the caller's class loader is not null and is not an ancestor of this class loader, and the caller does not have the RuntimePermission("getClassLoader") |
getResource
open fun getResource(name: String!): URL!
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a '/'-separated path name thatf identifies the resource.
| Parameters | |
|---|---|
name |
String!: The resource name |
| Return | |
|---|---|
URL! |
URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager. |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
If name is null |
getResourceAsStream
open fun getResourceAsStream(name: String!): InputStream!
Returns an input stream for reading the specified resource.
The search order is described in the documentation for getResource(String).
| Parameters | |
|---|---|
name |
String!: The resource name |
| Return | |
|---|---|
InputStream! |
An input stream for reading the resource; null if the resource could not be found, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager. |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
If name is null |
getResources
open fun getResources(name: String!): Enumeration<URL!>!
Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a /-separated path name that identifies the resource.
| Parameters | |
|---|---|
name |
String!: The resource name |
| Return | |
|---|---|
Enumeration<URL!>! |
An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration. |
| Exceptions | |
|---|---|
java.io.IOException |
If I/O errors occur |
java.lang.NullPointerException |
If name is null |
getSystemClassLoader
open static fun getSystemClassLoader(): ClassLoader!
Returns the system class loader. This is the default delegation parent for new ClassLoader instances, and is typically the class loader used to start the application.
This method is first invoked earl