Package org.opendaylight.yangtools.util
Class ClassLoaderUtils
java.lang.Object
org.opendaylight.yangtools.util.ClassLoaderUtils
Utility methods for working with ClassLoaders and classes.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
R> R applyWithClassLoader
(@NonNull ClassLoader cls, @NonNull Function<T, R> function, T input) Immediately callFunction.apply(Object)
with providedClassLoader
.static <V> V
callWithClassLoader
(@NonNull ClassLoader cls, @NonNull Callable<V> callable) Immediately callCallable.call()
with providedClassLoader
.findFirstGenericArgument
(Class<S> scannedClass, Class<G> genericType) static Optional<ParameterizedType>
findParameterizedType
(Class<?> subclass, Class<?> genericType) Find the parameterized instantiation of a particular interface implemented by a class.getFirstGenericParameter
(Type type) Extract the first generic type argument for a Type.static <V> V
getWithClassLoader
(@NonNull ClassLoader cls, @NonNull Supplier<V> supplier) Immediately callSupplier.get()
with providedClassLoader
.static void
runWithClassLoader
(@NonNull ClassLoader cls, @NonNull Runnable runnable) Immediately callRunnable.run()
with providedClassLoader
.
-
Method Details
-
applyWithClassLoader
public static <T,R> R applyWithClassLoader(@NonNull ClassLoader cls, @NonNull Function<T, R> function, T input) Immediately callFunction.apply(Object)
with providedClassLoader
. 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 functionR
- 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
public static <V> V callWithClassLoader(@NonNull ClassLoader cls, @NonNull Callable<V> callable) throws Exception Immediately callCallable.call()
with providedClassLoader
. 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 nullException
- if the callable fails
-
getWithClassLoader
Immediately callSupplier.get()
with providedClassLoader
. 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
Immediately callRunnable.run()
with providedClassLoader
. 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
-
findFirstGenericArgument
-
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 classgenericType
- Interface to search for- Returns:
- Parameterized interface as implemented by the class, if present
-
getFirstGenericParameter
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
- iftype
is null
-