Class 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.
    When this class loader is closed, all chained loaders are closed if they implement AutoCloseable. Afterwards, classes cannot be loaded anymore.
    Since:
    7.0.11
    Author:
    XIMA MEDIA GmbH
    • 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 Exception
        Closes all chained loaders if they implement AutoCloseable. 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:
        close in interface AutoCloseable
        Throws:
        Exception - If closing any file opened by this class loader resulted in an Exception. 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.
      • 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 of ClassLoader is 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 of ClassLoader is returned.
        Parameters:
        classLoaders - Loaders to chain.
        Returns:
        A class loader that combines the given loaders.