This classloader is recommended to be used as a parent classloader when it is required to limit access to classes which are not directly loaded by the parent classloader.
An example:
The interface P is loaded from the classloader PCL.
A subinterface of P, S is loaded from the classloader SCL.
We'd like to create an implementation of S in a different classloader, that doesn't have direct access to P. In this
case we create a parent exclusive classloader XCL which receives SCL as its parent during construction.
We place our implementation class I which implements S in the XCL classloader. In this case, we achieved that S is
visible from XCL, and S can be successfully implemented by I, but P cannot be directly implemented in XCL, as it is
not visible from it. In other words, we cannot define a class, that implements P
in the classloader XCL.
The ClassLoader.getParent() function of this class will always return null
.
When a class loading request is issued to this classloader, the parent will be asked to load the class. If successfull, the classloader will be queried of the loaded class, and the class will only be returned if the identity of the classloader is the same as the parent specified for this instance. This means that classes that are not available through this classloader may still be loaded, and be considered not found by this instance.
This class has limited use-case, and probably not relevant to most users. For informational purposes, one use-case for this classloader is when a class with a given name is present in a hierarchical classpath in multiple levels, and one doesn't want the classes from the upper levels to leak into the lower levels. This can happen when testing frameworks want to test their own implementation, but at the same time have the framework on the classpath for the test runners.
public | Creates a new instance that is exclusive to the specified parent classloader. |
protected Class< | Finds the class with the specified binary name. |
public URL | getResource( Finds the resource with the given name. |
public InputStream | getResourceAsStream( Returns an input stream for reading the specified resource. |
public Enumeration< | getResources( Finds all the resources with the given name. |
protected Class< | Loads the class with the specified binary name. |
null
.The name of a resource is a '/'-separated path name that identifies the resource.
This method will first search the parent class loader for the resource; if the parent is null the path
of the class loader built-in to the virtual machine is searched. That failing, this method will invoke
ClassLoader.findResource(
The search order is described in the documentation for ClassLoader.getResource(
The name of a resource is a /-separated path name that identifies the resource.
The search order is described in the documentation for ClassLoader.getResource(
-
Invoke ClassLoader.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-in to the virtual machine is used, instead.
-
Invoke the ClassLoader.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 ClassLoader.resolveClass(
Subclasses of ClassLoader are encouraged to override ClassLoader.findClass(
Unless overridden, this method synchronizes on the result of getClassLoadingLock method during the entire class loading process.