|
FlexDoc/Javadoc 2.0 Demo Java Doc |
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 parallel capable class loaders and are required to register themselves at their class initialization time by invoking the 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).
Bootstrap class loader. It is the virtual machine's built-in class loader, typically represented as null, and does not have a parent.
Platform class loader. The platform class loader is responsible for loading the platform classes. Platform classes include Java SE platform APIs, their implementation classes and JDK-specific run-time classes that are defined by the platform class loader or its ancestors. The platform class loader can be used as the parent of a ClassLoader instance.
To allow for upgrading/overriding of modules defined to the platform class loader, and where upgraded modules read modules defined to class loaders other than the platform class loader and its ancestors, then the platform class loader may have to delegate to other class loaders, the application class loader for example. In other words, classes in named modules defined to class loaders other than the platform class loader and its ancestors may be visible to the platform class loader.
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 . . . } }
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.
Constructor Summary |
||
protected |
Creates a new class loader using the ClassLoader returned by
the method getSystemClassLoader() as the parent class loader.
|
|
protected |
ClassLoader(ClassLoader parent)
Creates a new class loader using the specified parent class loader for
delegation.
|
|
protected |
Creates a new class loader of the specified name and using the
specified parent class loader for delegation.
|
Method Summary |
||
void |
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.
|
|
protected final Class<?> |
defineClass(byte[] b, int off, int len)
|
|
protected final Class<?> |
defineClass(String name, byte[] b, int off, int len)
Converts an array of bytes into an instance of class Class.
|
|
protected final Class<?> |
Converts an array of bytes into an instance of class Class,
with a given ProtectionDomain.
|
|
protected final Class<?> |
||
protected Package |
definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)
Defines a package by name in this ClassLoader.
|
|
protected Class<?> |
Finds the class with the specified binary name.
|
|
protected Class<?> |
Finds the class with the given binary name
in a module defined to this class loader.
|
|
protected String |
findLibrary(String libname)
Returns the absolute path name of a native library.
|
|
protected final Class<?> |
findLoadedClass(String name)
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.
|
|
protected URL |
findResource(String name)
Finds the resource with the given name.
|
|
protected URL |
Returns a URL to a resource in a module defined to this class loader.
|
|
protected Enumeration<URL> |
findResources(String name)
Returns an enumeration of URL objects
representing all the resources with the given name.
|
|
protected final Class<?> |
findSystemClass(String name)
Finds a class with the specified binary name,
loading it if necessary.
|
|
protected Object |
getClassLoadingLock(String className)
Returns the lock object for class loading operations.
|
|
final Package |
getDefinedPackage(String name)
Returns a Package of the given name that
has been defined by this class loader.
|
|
final Package[] |
Returns all of the Packages that have been defined by
this class loader.
|
|
getName()
Returns the name of this class loader or null if
this class loader is not named.
|
||
protected Package |
getPackage(String name)
Deprecated. If multiple class loaders delegate to each other and define classes
with the same package name, and one such loader relies on the lookup
behavior of getPackage to return a Package from
a parent loader, then the properties exposed by the Package
may not be as expected in the rest of the program.
|
|
protected Package[] |
Returns all of the Packages that have been defined by
this class loader and its ancestors.
|
|
final ClassLoader |
Returns the parent class loader for delegation.
|
|
static ClassLoader |
Returns the platform class loader.
|
|
getResource(String name)
Finds the resource with the given name.
|
||
getResourceAsStream(String name)
Returns an input stream for reading the specified resource.
|
||
getResources(String name)
Finds all the resources with the given name.
|
||
static ClassLoader |
Returns the system class loader.
|
|
static URL |
getSystemResource(String name)
Find a resource of the specified name from the search path used to load
classes.
|
|
static InputStream |
Open for reading, a resource of the specified name from the search path
used to load classes.
|
|
static Enumeration<URL> |
getSystemResources(String name)
Finds all resources of the specified name from the search path used to
load classes.
|
|
final Module |
Returns the unnamed Module for this class loader.
|
|
final boolean |
||
Class<?> |
Loads the class with the specified binary name.
|
|
protected Class<?> |
Loads the class with the specified binary name.
|
|
protected static boolean |
Registers the caller as
parallel capable.
|
|
protected final void |
resolveClass(Class<?> c)
Links the specified class.
|
|
Returns a stream whose elements are the URLs of all the resources with
the given name.
|
||
void |
setClassAssertionStatus(String className, boolean enabled)
Sets the desired assertion status for the named top-level class in this
class loader and any nested classes contained therein.
|
|
void |
setDefaultAssertionStatus(boolean enabled)
Sets the default assertion status for this class loader.
|
|
void |
setPackageAssertionStatus(String packageName, boolean enabled)
Sets the package default assertion status for the named package.
|
|
protected final void |
Sets the signers of a class.
|
Methods inherited from class java.lang.Object |
protected ClassLoader |
protected ClassLoader |
(ClassLoader parent) |
If there is a security manager, its checkCreateClassLoader method is invoked. This may result in a security exception.
protected ClassLoader |
() |
If there is a security manager, its checkCreateClassLoader method is invoked. This may result in a security exception.
public String getName |
() |
public Class<?> loadClass |
(String name) |
throws |
protected Class<?> loadClass |
(String name, boolean resolve) |
throws |
Invoke findLoadedClass(String) to check if the class has already been loaded.
Invoke the loadClass method on the parent class loader. If the parent is null the class loader built into the virtual machine is used, instead.
Invoke the findClass(String) method to find the class.
If the class was found using the above steps, and the resolve flag is true, this method will then invoke the resolveClass(Class) method on the resulting Class object.
Subclasses of ClassLoader are encouraged to override findClass(String), rather than this method.
Unless overridden, this method synchronizes on the result of getClassLoadingLock method during the entire class loading process.
protected Object getClassLoadingLock |
(String className) |
protected Class<?> findClass |
(String name) |
throws |
protected Class<?> findClass |
protected final Class<?> defineClass |
(byte[] b, int off, int len) |
throws |
protected final Class<?> defineClass |
|
throws |
This method assigns a default ProtectionDomain to the newly defined class. The ProtectionDomain is effectively granted the same set of permissions returned when Policy.getPolicy().getPermissions(new CodeSource(null, null)) is invoked. The default protection domain is created on the first invocation of defineClass, and re-used on subsequent invocations.
To assign a specific ProtectionDomain to the class, use the defineClass method that takes a ProtectionDomain as one of its arguments.
This method defines a package in this class loader corresponding to the package of the Class (if such a package has not already been defined in this class loader). The name of the defined package is derived from the binary name of the class specified by the byte array b. Other properties of the defined package are as specified by Package.
protected final Class<?> defineClass |
|
throws |
If the given ProtectionDomain is null, then a default protection domain will be assigned to the class as specified in the documentation for defineClass(String, byte[], int, int). Before the class can be used it must be resolved.
The first class defined in a package determines the exact set of certificates that all subsequent classes defined in that package must contain. The set of certificates for a class is obtained from the CodeSource within the ProtectionDomain of the class. Any classes added to that package must contain the same set of certificates or a SecurityException will be thrown. Note that if name is null, this check is not performed. You should always pass in the binary name of the class you are defining as well as the bytes. This ensures that the class you are defining is indeed the class you think it is.
If the specified name begins with "java.", it can only be defined by the platform class loader or its ancestors; otherwise SecurityException will be thrown. If name is not null, it must be equal to the binary name of the class specified by the byte array b, otherwise a NoClassDefFoundError will be thrown.
This method defines a package in this class loader corresponding to the package of the Class (if such a package has not already been defined in this class loader). The name of the defined package is derived from the binary name of the class specified by the byte array b. Other properties of the defined package are as specified by Package.
protected final Class<?> defineClass |
|
throws |
The rules about the first class defined in a package determining the set of certificates for the package, the restrictions on class names, and the defined package of the class are identical to those specified in the documentation for defineClass(String, byte[], int, int, ProtectionDomain).
An invocation of this method of the form cl.defineClass(name, bBuffer, pd) yields exactly the same result as the statements
...
byte[] temp = new byte[bBuffer.remaining()];
bBuffer.get(temp);
return cl.defineClass(name, temp, 0,
temp.length, pd);
protected final void resolveClass |
(Class<?> c) |
protected final Class<?> findSystemClass |
(String name) |
throws |
This method loads the class through the system class loader (see getSystemClassLoader()). The Class object returned might have more than one ClassLoader associated with it. Subclasses of ClassLoader need not usually invoke this method, because most class loaders need to override just findClass(String).
protected final Class<?> findLoadedClass |
(String name) |
protected final void setSigners |
protected URL findResource |
|
throws |
public URL getResource |
(String name) |
The name of a resource is a '/'-separated path name that identifies the resource.
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).
(String name) |
|
throws |
The name of a resource is a /-separated path name that identifies the resource.
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).
(String name) |
The name of a resource is a /-separated path name that identifies the resource.
The resources will be located when the returned stream is evaluated. If the evaluation results in an IOException then the I/O exception is wrapped in an UncheckedIOException that is then thrown.
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).
protected URL findResource |
(String name) |
For resources in named modules then the method must implement the rules for encapsulation specified in the Module getResourceAsStream method. Additionally, it must not find non-".class" resources in packages of named modules unless the package is opened unconditionally.
(String name) |
|
throws |
For resources in named modules then the method must implement the rules for encapsulation specified in the Module getResourceAsStream method. Additionally, it must not find non-".class" resources in packages of named modules unless the package is opened unconditionally.
protected static boolean registerAsParallelCapable |
() |
Note that once a class loader is registered as parallel capable, there is no way to change it back.
public final boolean isRegisteredAsParallelCapable |
() |
public static URL getSystemResource |
(String name) |
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally.
(String name) |
|
throws |
The search order is described in the documentation for getSystemResource(String).
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally.
public InputStream getResourceAsStream |
(String name) |
The search order is described in the documentation for getResource(String).
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally.
public static InputStream getSystemResourceAsStream |
(String name) |
Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally.
public final ClassLoader getParent |
() |
public final Module getUnnamedModule |
() |
public static ClassLoader getPlatformClassLoader |
() |
public static ClassLoader getSystemClassLoader |
() |
This method is first invoked early in the runtime's startup sequence, at which point it creates the system class loader. This class loader will be the context class loader for the main application thread (for example, the thread that invokes the main method of the main class).
The default system class loader is an implementation-dependent instance of this class.
If the system property "java.system.class.loader" is defined when this method is first invoked then the value of that property is taken to be the name of a class that will be returned as the system class loader. The class is loaded using the default system class loader and must define a public constructor that takes a single parameter of type ClassLoader which is used as the delegation parent. An instance is then created using this constructor with the default system class loader as the parameter. The resulting class loader is defined to be the system class loader. During construction, the class loader should take great care to avoid calling getSystemClassLoader(). If circular initialization of the system class loader is detected then an IllegalStateException is thrown.
The name of the built-in system class loader is "app". The system property "java.class.path" is read during early initialization of the VM to determine the class path. An empty value of "java.class.path" property is interpreted differently depending on whether the initial module (the module containing the main class) is named or unnamed: If named, the built-in system class loader will have no class path and will search for classes and resources using the application module path; otherwise, if unnamed, it will set the class path to the current working directory.
JAR files on the class path may contain a Class-Path manifest attribute to specify dependent JAR files to be included in the class path. Class-Path entries must meet certain conditions for validity (see the JAR File Specification for details). Invalid Class-Path entries are ignored. For debugging purposes, ignored entries can be printed to the console if the jdk.net.URLClassPath.showIgnoredClassPathEntries system property is set to true.
protected Package definePackage |
Package names must be unique within a class loader and cannot be redefined or changed once created.
If a class loader wishes to define a package with specific properties, such as version information, then the class loader should call this definePackage method before calling defineClass. Otherwise, the defineClass method will define a package in this class loader corresponding to the package of the newly defined class; the properties of this defined package are specified by Package.
public final Package getDefinedPackage |
(String name) |
public final Package[] getDefinedPackages |
() |
protected Package getPackage |
(String name) |
If this class loader defines a Package of the given name, the Package is returned. Otherwise, the ancestors of this class loader are searched recursively (parent by parent) for a Package of the given name.
protected Package[] getPackages |
() |
protected String findLibrary |
(String libname) |
public void setDefaultAssertionStatus |
(boolean enabled) |
public void setPackageAssertionStatus |
(String packageName, boolean enabled) |
A subpackage of a package named p is any package whose name begins with "p.". For example, javax.swing.text is a subpackage of javax.swing, and both java.util and java.lang.reflect are subpackages of java.
In the event that multiple package defaults apply to a given class, the package default pertaining to the most specific package takes precedence over the others. For example, if javax.lang and javax.lang.reflect both have package defaults associated with them, the latter package default applies to classes in javax.lang.reflect.
Package defaults take precedence over the class loader's default assertion status, and may be overridden on a per-class basis by invoking setClassAssertionStatus(String, boolean).
public void setClassAssertionStatus |
(String className, boolean enabled) |
If the named class is not a top-level class, this invocation will have no effect on the actual assertion status of any class.
public void clearAssertionStatus |
() |
|
FlexDoc/Javadoc 2.0 Demo Java Doc |