Class Classes.ClassLoaderBuilder<B extends Classes.ClassLoaderBuilder<B>>

  • Enclosing class:
    Classes

    public abstract static class Classes.ClassLoaderBuilder<B extends Classes.ClassLoaderBuilder<B>>
    extends Object
    A builder for class loaders with more features than URLClassLoader.
    • Method Detail

      • setParent

        @CanIgnoreReturnValue
        public abstract B setParent​(ClassLoader parent)
        Set parent of new class loader. If not set the default delegation parent class loader will be used (like URLClassLoader(URL[]).
      • setUrls

        @CanIgnoreReturnValue
        public abstract B setUrls​(URL... urls)
        Set sources for classes of new class loader just like for URLClassLoader (this or setUrls(Iterable) are required).
      • setDirectLoadClasses

        @CanIgnoreReturnValue
        public abstract B setDirectLoadClasses​(Predicate<String> classes)
        Set a predicate that specifies which classes are forced to be loaded by the new class loader and not its parent, even if the latter could load them.

        The predicate should match the fully-qualified class name. The default is to not match any classes.

        Normally class loaders follow the parent-first strategy, so they never load classes which their parent could also load. The new class loader follows the child-first strategy for a specific set of classes (as specified by this predicate) and the parent-first strategy for the rest.

        This feature can be used if you want to load a component with its own class loader (so that it can be garbage collected independently, for example), but the parent class loader also sees the classes.

      • setCustomLookupNativeLibraries

        @CanIgnoreReturnValue
        public abstract B setCustomLookupNativeLibraries​(Predicate<String> libraries)
        Set a predicate that specifies for which native libraries we should use a custom lookup for the binary as documented in NativeLibraries.

        The predicate should match the library name as given to System.loadLibrary(String). The default is to not match any libraries.

        Note that this is only effective if the new class loader is actually the one that is asked to load the new library. Because Java's class loaders follow the parent-first strategy, it is easy to end up with a parent class loader loading the library, if the parent can see the class(es) that do the loading. In this case, use setDirectLoadClasses(Predicate) to ensure the new class loader loads all relevant classes itself.