Package de.xima.fc.classloader
Class ChainedClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- de.xima.fc.classloader.ChainedClassLoader
-
- All Implemented Interfaces:
AutoCloseable
public final class ChainedClassLoader extends ClassLoader implements AutoCloseable
A class loader that combines several other class loaders.- When
searching for a class, it tries each class loader in order, and returns the class of the first class loader that can provide the class. - When
searching for a resources, it tries each class loader in order, and returns the resource of the first class loader that can provide the resource. - When searching
for multiple resources, it returns the resources of all class loaders.
AutoCloseable. Afterwards, classes cannot be loaded anymore.- Since:
- 7.0.11
- Author:
- XIMA MEDIA GmbH
-
-
Constructor Summary
Constructors Constructor Description ChainedClassLoader(ClassLoader[] classLoaders)Creates a new chained class loader that combines the given loaders.ChainedClassLoader(Iterable<ClassLoader> classLoaders)Creates a new chained class loader that combines the given loaders.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ClassLoaderchain(ClassLoader[] classLoaders)Creates a new class loader that combines the given loaders.static ClassLoaderchain(Iterable<ClassLoader> classLoaders)Creates a new class loader that combines the given loaders.voidclose()Closes all chained loaders if they implementAutoCloseable.List<ClassLoader>getClassLoaders()Returns all class loaders which this class loader combines.URLgetResource(String name)Enumeration<URL>getResources(String name)Class<?>loadClass(String name)-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResourceAsStream, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Constructor Detail
-
ChainedClassLoader
public ChainedClassLoader(ClassLoader[] classLoaders)
Creates a new chained class loader that combines the given loaders.- Parameters:
classLoaders- Loaders to chain.
-
ChainedClassLoader
public ChainedClassLoader(Iterable<ClassLoader> classLoaders)
Creates a new chained class loader that combines the given loaders.- Parameters:
classLoaders- Loaders to chain.
-
-
Method Detail
-
close
public void close() throws ExceptionCloses all chained loaders if they implementAutoCloseable. Afterwards, classes cannot be loaded anymore.The method makes a best effort attempt to close all opened files, by catching
Exceptions internally. Unchecked exceptions and errors are not caught. Calling close on an already closed loader has no effect.- Specified by:
closein interfaceAutoCloseable- Throws:
Exception- If closing any file opened by this class loader resulted in anException. Any such exceptions are caught internally. If only one is caught, then it is re-thrown. If more than one exception is caught, then the second and following exceptions are added as suppressed exceptions of the first one caught, which is then re-thrown.
-
getClassLoaders
public List<ClassLoader> getClassLoaders()
Returns all class loaders which this class loader combines. Note: the returned list is unmodifiable.- Returns:
- The class loaders that are chained by this class loader.
-
getResource
public URL getResource(String name)
- Overrides:
getResourcein classClassLoader
-
getResources
public Enumeration<URL> getResources(String name) throws IOException
- Overrides:
getResourcesin classClassLoader- Throws:
IOException
-
loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
- Overrides:
loadClassin classClassLoader- Throws:
ClassNotFoundException
-
chain
public static ClassLoader chain(ClassLoader[] classLoaders)
Creates a new class loader that combines the given loaders. This method makes no guarantess regarding which sub class ofClassLoaderis returned.- Parameters:
classLoaders- Loaders to chain.- Returns:
- A class loader that combines the given loaders.
-
chain
public static ClassLoader chain(Iterable<ClassLoader> classLoaders)
Creates a new class loader that combines the given loaders. This method makes no guarantess regarding which sub class ofClassLoaderis returned.- Parameters:
classLoaders- Loaders to chain.- Returns:
- A class loader that combines the given loaders.
-
-