saker.util Documentation TaskDoc JavaDoc Packages
public class ParentExclusiveClassLoader extends ClassLoader
ClassLoader implementation that only allows access to the classes which are directly loaded from the designated parent classloader.

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.

Constructors
public
Creates a new instance that is exclusive to the specified parent classloader.
Methods
protected Class<?>
Finds the class with the specified binary name.
public URL
Finds the resource with the given name.
public InputStream
Returns an input stream for reading the specified resource.
public Enumeration<URL>
Finds all the resources with the given name.
protected Class<?>
loadClass(String name, boolean resolve)
Loads the class with the specified binary name.
Creates a new instance that is exclusive to the specified parent classloader.
parentThe exclusive parent.
NullPointerExceptionIf the parent is null.
protected Class<?> findClass(String name) throws ClassNotFoundException
Overridden from: ClassLoader
Finds the class with the specified binary name. This method should be overridden by class loader implementations that follow the delegation model for loading classes, and will be invoked by the loadClass method after checking the parent class loader for the requested class. The default implementation throws a ClassNotFoundException.
nameThe binary name of the class
The resulting Class object
ClassNotFoundExceptionIf the class could not be found
1.2
public URL getResource(String name)
Overridden from: ClassLoader
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 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(String) to find the resource.

nameThe resource name
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
1.1
Overridden from: ClassLoader
Returns an input stream for reading the specified resource.

The search order is described in the documentation for ClassLoader.getResource(String).

nameThe resource name
An input stream for reading the resource, or null if the resource could not be found
1.1
Overridden from: ClassLoader
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.

The search order is described in the documentation for ClassLoader.getResource(String).

nameThe resource name
An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources that the class loader doesn't have access to will not be in the enumeration.
IOExceptionIf I/O errors occur
1.2
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
Overridden from: ClassLoader
Loads the class with the specified binary name. The default implementation of this method searches for classes in the following order:
  1. Invoke ClassLoader.findLoadedClass(String) to check if the class has already been loaded.

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

  3. 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(Class<?>) method on the resulting Class object.

Subclasses of ClassLoader are encouraged to override ClassLoader.findClass(String), rather than this method.

Unless overridden, this method synchronizes on the result of getClassLoadingLock method during the entire class loading process.

nameThe binary name of the class
resolveIf true then resolve the class
The resulting Class object
ClassNotFoundExceptionIf the class could not be found