Package org.opendaylight.yangtools.util
Class ClassLoaderUtils
- java.lang.Object
-
- org.opendaylight.yangtools.util.ClassLoaderUtils
-
public final class ClassLoaderUtils extends Object
Utility methods for working with ClassLoaders and classes.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,R>
RapplyWithClassLoader(@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
.static <S,G,P>
Optional<Class<P>>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.static Optional<Type>
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 Class<?>
loadClass(ClassLoader cls, String name)
Loads class using this supplied classloader.static Class<?>
loadClassWithTCCL(String name)
static void
runWithClassLoader(@NonNull ClassLoader cls, @NonNull Runnable runnable)
Immediately callRunnable.run()
with providedClassLoader
.static Optional<Class<?>>
tryToLoadClassWithTCCL(String fullyQualifiedClassName)
-
-
-
Method Detail
-
applyWithClassLoader
@Beta 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.- Parameters:
cls
-ClassLoader
to be used.function
- Function to be applied.input
- Function input- 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 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.- 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
-
getWithClassLoader
@Beta public static <V> V getWithClassLoader(@NonNull ClassLoader cls, @NonNull Supplier<V> supplier)
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.- 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 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
-
loadClass
public static Class<?> loadClass(ClassLoader cls, String name) throws ClassNotFoundException
Loads class using this supplied classloader.- Parameters:
name
- String name of class.- Throws:
ClassNotFoundException
-
loadClassWithTCCL
public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
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 classgenericType
- 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
- iftype
is null
-
-