Class ClassLoaderUtils


  • public final class ClassLoaderUtils
    extends Object
    Utility methods for working with ClassLoaders and classes.
    • Method Detail

      • applyWithClassLoader

        @Beta
        public static <T,​R> R applyWithClassLoader​(@NonNull ClassLoader cls,
                                                         @NonNull Function<T,​R> function,
                                                         T input)
        Immediately call Function.apply(Object) with provided ClassLoader. This method safely switches the thread's Thread Context Class Loader to the specified class loader for the duration of execution of that method.
        Type Parameters:
        T - the type of the input to the function
        R - the type of the result of the function
        Parameters:
        cls - ClassLoader to be used.
        function - Function to be applied.
        input - Function input
        Returns:
        Result of function invocation.
        Throws:
        NullPointerException - if class loader or function is null
      • callWithClassLoader

        @Beta
        public static <V> V callWithClassLoader​(@NonNull ClassLoader cls,
                                                @NonNull Callable<V> callable)
                                         throws Exception
        Immediately call Callable.call() with provided ClassLoader. This method safely switches the thread's Thread Context Class Loader to the specified class loader for the duration of execution of that method.
        Type Parameters:
        V - the result type of the callable
        Parameters:
        cls - ClassLoader to be used.
        callable - Function to be executed.
        Returns:
        Result of callable invocation.
        Throws:
        NullPointerException - if class loader or callable is null
        Exception - if the callable fails
      • getWithClassLoader

        @Beta
        public static <V> V getWithClassLoader​(@NonNull ClassLoader cls,
                                               @NonNull Supplier<V> supplier)
        Immediately call Supplier.get() with provided ClassLoader. This method safely switches the thread's Thread Context Class Loader to the specified class loader for the duration of execution of that method.
        Type Parameters:
        V - the result type of the supplier
        Parameters:
        cls - ClassLoader to be used.
        supplier - Function to be executed.
        Returns:
        Result of supplier invocation.
        Throws:
        NullPointerException - if class loader or supplier is null
      • runWithClassLoader

        @Beta
        public static void runWithClassLoader​(@NonNull ClassLoader cls,
                                              @NonNull Runnable runnable)
        Immediately call Runnable.run() with provided ClassLoader. This method safely switches the thread's Thread Context Class Loader to the specified class loader for the duration of execution of that method.
        Parameters:
        cls - ClassLoader to be used.
        runnable - Function to be executed.
        Throws:
        NullPointerException - if class loader or runnable is null
      • tryToLoadClassWithTCCL

        public static Optional<Class<?>> tryToLoadClassWithTCCL​(String fullyQualifiedClassName)
      • findFirstGenericArgument

        public static <S,​G,​P> Optional<Class<P>> findFirstGenericArgument​(Class<S> scannedClass,
                                                                                      Class<G> genericType)
      • findParameterizedType

        public static Optional<ParameterizedType> findParameterizedType​(Class<?> subclass,
                                                                        Class<?> genericType)
        Find the parameterized instantiation of a particular interface implemented by a class.
        Parameters:
        subclass - Implementing class
        genericType - Interface to search for
        Returns:
        Parameterized interface as implemented by the class, if present
      • getFirstGenericParameter

        public static Optional<Type> getFirstGenericParameter​(Type type)
        Extract the first generic type argument for a Type. If the type is not parameterized, this method returns empty.
        Parameters:
        type - Type to examine
        Returns:
        First generic type argument, if present
        Throws:
        NullPointerException - if type is null