Class ProxyClassLoader
java.lang.Object
java.lang.ClassLoader
net.thevpc.common.prs.classloader.ProxyClassLoader
A class loader that has multiple parents and uses them for loading
classes and resources. It can be used in tree hierarchy, where it
can exploit its capability to not throw ClassNotFoundException when
communicating with other ProxyClassLoader.
It itself doesn't load classes or resources, but allows subclasses
to add such functionality.
- Author:
- Petr Nejedly, Jesse Glick (ProxyClassLoader)
-
Constructor Summary
ConstructorsConstructorDescriptionProxyClassLoader(ClassLoader[] parents) Create a multi-parented classloader.ProxyClassLoader(String name, ClassLoader[] parents) ProxyClassLoader(String name, ClassLoader[] parents, boolean transitive) Create a multi-parented classloader. -
Method Summary
Modifier and TypeMethodDescriptionvoidappend(ClassLoader[] classLoaders) Add new parents dynamically.protected PackagedefinePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) This is here just for locking serialization purposes.voiddestroy()Try to destroy this classloader.protected URLfindResource(String name) This ClassLoader can't load anything itself.protected final EnumerationfindResources(String name) Finds all the resource with the given name.getName()protected PackagegetPackage(String name) Returns a Package that has been defined by this class loader or any of its parents.getPackageAccessibly(String name) protected PackagegetPackageFast(String name, String sname, boolean recurse) Faster way to find a package.protected Package[]Returns all of the Packages defined by this class loader and its parents.Package[]final URLgetResource(String name) Finds the resource with the given name.protected booleanisSpecialResource(String pkg) Test whether a given resource name is something that any JAR might have, and for which the domain cache should be disabled.protected final ClassLoads the class with the specified name.protected booleanshouldDelegateResource(String pkg, ClassLoader parent) Test whether a given resource request (for a class or not) should be searched for in the specified parent classloader or not.protected ClasssimpleFindClass(String name, String fileName, String pkg) This ClassLoader can't load anything itself.protected EnumerationsimpleFindResources(String name) This ClassLoader can't load anything itself.Methods inherited from class ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, findClass, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getParent, getPlatformClassLoader, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Constructor Details
-
ProxyClassLoader
Create a multi-parented classloader. Loads recursively from parents.- Parameters:
parents- list of parent classloaders.- Throws:
IllegalArgumentException- if there are any nulls or duplicate parent loaders or cycles.
-
ProxyClassLoader
-
ProxyClassLoader
Create a multi-parented classloader.- Parameters:
parents- list of parent classloaders.transitive- whether other PCLs depending on this one will automatically search through its parent list- Throws:
IllegalArgumentException- if there are any nulls or duplicate parent loaders or cycles.- Since:
- org.netbeans.core/1 > 1.6
-
-
Method Details
-
getName
- Overrides:
getNamein classClassLoader
-
append
Add new parents dynamically.- Parameters:
classLoaders- the new parents to add (append to list)- Throws:
IllegalArgumentException- in case of a null or cyclic parent (duplicate OK)
-
destroy
public void destroy()Try to destroy this classloader. Subsequent attempts to use it will log an error (at most one though). -
loadClass
Loads the class with the specified name. The implementation of this method searches for classes in the following order: CallsClassLoader.findLoadedClass(String)to check if the class has * already been loaded. * Checks the caches whether another class from the same package * was already loaded and uses the same classloader * Tries to find the class using parent loaders in their order. * Calls thesimpleFindClass(String, String, String)method to find * the class using this class loader. *- Overrides:
loadClassin classClassLoader- Parameters:
name- the name of the classresolve- if true then resolve the class- Returns:
- the resulting Class object
- Throws:
ClassNotFoundException- if the class could not be found
-
simpleFindClass
This ClassLoader can't load anything itself. Subclasses may override this method to do some class loading themselves. The implementation should not throw any exception, just return null if it can't load required class.- Parameters:
name- the name of the classfileName- the expected filename of the classfile, like java/lang/Object.class for java.lang.Object The ClassLoader implementation may or may not use it, depending whether it is usefull to it.pkg- the package name, in the format org/netbeans/modules/foo/- Returns:
- the resulting Class object or null
-
getResource
Finds the resource with the given name. The implementation of this method searches for resources in the following order: Checks the caches whether another resource or class from the same * package was already loaded and uses the same classloader. * Tries to find the resources using parent loaders in their order. * Calls thefindResource(String)method to find * the resources using this class loader. *- Overrides:
getResourcein classClassLoader- Parameters:
name- a "/"-separated path name that identifies the resource.- Returns:
- a URL for reading the resource, or null if the resource could not be found.
- See Also:
-
findResource
This ClassLoader can't load anything itself. Subclasses may override this method to do some resource loading themselves.- Overrides:
findResourcein classClassLoader- Parameters:
name- the resource name- Returns:
- a URL for reading the resource, or null if the resource could not be found.
-
findResources
Finds all the resource with the given name. The implementation of this method uses thesimpleFindResources(String)method to find all the resources available from this classloader and adds all the resources from all the parents.- Overrides:
findResourcesin classClassLoader- Parameters:
name- the resource name- Returns:
- an Enumeration of URLs for the resources
- Throws:
IOException- if I/O errors occur
-
simpleFindResources
This ClassLoader can't load anything itself. Subclasses may override this method to do some resource loading themselves, this implementation simply delegates to findResources method of the superclass that should return empty Enumeration.- Parameters:
name- the resource name- Returns:
- an Enumeration of URLs for the resources
- Throws:
IOException- if I/O errors occur
-
getPackage
Returns a Package that has been defined by this class loader or any of its parents.- Overrides:
getPackagein classClassLoader- Parameters:
name- the package name- Returns:
- the Package corresponding to the given name, or null if not found
-
getPackageFast
-
definePackage
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException This is here just for locking serialization purposes. Delegates to super.definePackage with proper locking. Also tracks the package in our private cache, since getPackageFast(...,...,false) will not call super.getPackage.- Overrides:
definePackagein classClassLoader- Throws:
IllegalArgumentException
-
getPackages
Returns all of the Packages defined by this class loader and its parents.- Overrides:
getPackagesin classClassLoader- Returns:
- the array of Package objects defined by this ClassLoader
-
getPackageAccessibly
-
getPackagesAccessibly
-
isSpecialResource
Test whether a given resource name is something that any JAR might have, and for which the domain cache should be disabled. The result must not change from one call to the next with the same argument. By default the domain cache is disabled only for META-INF/* JAR information.- Parameters:
pkg- the package component of the resource path ending with a slash, e.g. "org/netbeans/modules/foo/"- Returns:
- true if it is a special resource, false for normal domain-cached resource
- Since:
- org.netbeans.core/1 1.3
-
shouldDelegateResource
Test whether a given resource request (for a class or not) should be searched for in the specified parent classloader or not. The result must not change from one call to the next with the same arguments. By default, always true. Subclasses may override to "mask" certain packages from view, possibly according to the classloader chain.- Parameters:
pkg- the package component of the resource path ending with a slash, e.g. "org/netbeans/modules/foo/"parent- a classloader which is a direct or indirect parent of this one- Returns:
- true if the request should be delegated to this parent; false to only search elsewhere (other parents, this loader's own namespace)
- Since:
- org.netbeans.core/1 1.3
-