Package com.helger.base.classloader
Class ClassLoaderHelper
java.lang.Object
com.helger.base.classloader.ClassLoaderHelper
ClassLoader utility methods.- Author:
- Philip Helger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable ClassLoadergetClassClassLoader(@NonNull Class<?> aClass) Get the class loader that loaded the given class.static @Nullable ClassLoaderGet the context class loader of the current thread.static @NonNull ClassLoaderGet the default class loader to use.static @Nullable ClassLoadergetParentClassLoader(@NonNull ClassLoader aClassLoader) Get the parent class loader of the given class loader.static @Nullable URLgetResource(@NonNull ClassLoader aClassLoader, @NonNull @Nonempty String sPath) Get the URL of the passed resource using the specified class loader only.static @Nullable InputStreamgetResourceAsStream(@NonNull ClassLoader aClassLoader, @NonNull @Nonempty String sPath) Get the input stream of the passed resource using the specified class loader only.static @NonNull Enumeration<URL> getResources(@NonNull ClassLoader aClassLoader, @NonNull @Nonempty String sPath) Get all URLs of the passed resource using the specified class loader only.static @NonNull ClassLoaderGet the system class loader.protected static @NonNull StringinternalGetPathWithoutLeadingSlash(@NonNull @Nonempty String sPath) static voidsetContextClassLoader(@NonNull ClassLoader aClassLoader) Set the context class loader of the current thread.
-
Constructor Details
-
ClassLoaderHelper
protected ClassLoaderHelper()
-
-
Method Details
-
getSystemClassLoader
Get the system class loader.- Returns:
- The system class loader. Never
null. - See Also:
-
getContextClassLoader
Get the context class loader of the current thread.- Returns:
- The context class loader, or
nullif the context class loader indicates the system class loader. - See Also:
-
setContextClassLoader
Set the context class loader of the current thread.- Parameters:
aClassLoader- The class loader to set as context class loader. May not benull.- See Also:
-
getClassClassLoader
Get the class loader that loaded the given class.- Parameters:
aClass- The class whose class loader is to be retrieved. May not benull.- Returns:
- The class loader of the given class, or
nullif the class was loaded by the bootstrap class loader. - See Also:
-
getParentClassLoader
Get the parent class loader of the given class loader.- Parameters:
aClassLoader- The class loader whose parent is to be retrieved. May not benull.- Returns:
- The parent class loader, or
nullif the parent is the bootstrap class loader. - See Also:
-
getDefaultClassLoader
Get the default class loader to use. This first tries to get the context class loader of the current thread, and falls back to the class loader of theClassLoaderHelperclass itself.- Returns:
- The default class loader. Never
null.
-
internalGetPathWithoutLeadingSlash
-
getResource
public static @Nullable URL getResource(@NonNull ClassLoader aClassLoader, @Nonempty @NonNull @Nonempty String sPath) Get the URL of the passed resource using the specified class loader only. This is a sanity wrapper aroundclassLoader.getResource (sPath).- Parameters:
aClassLoader- The class loader to be used. May not benull.sPath- The path to be resolved. May neither benullnor empty. Internally it is ensured that the provided path does NOT start with a slash.- Returns:
nullif the path could not be resolved using the specified class loader.
-
getResources
public static @NonNull Enumeration<URL> getResources(@NonNull ClassLoader aClassLoader, @Nonempty @NonNull @Nonempty String sPath) throws IOException Get all URLs of the passed resource using the specified class loader only. This is a sanity wrapper aroundclassLoader.getResources (sPath).- Parameters:
aClassLoader- The class loader to be used. May not benull.sPath- The path to be resolved. May neither benullnor empty. Internally it is ensured that the provided path does NOT start with a slash.- Returns:
nullif the path could not be resolved using the specified class loader.- Throws:
IOException- In case an internal error occurs.
-
getResourceAsStream
public static @Nullable InputStream getResourceAsStream(@NonNull ClassLoader aClassLoader, @Nonempty @NonNull @Nonempty String sPath) Get the input stream of the passed resource using the specified class loader only. This is a sanity wrapper aroundclassLoader.getResourceAsStream (sPath).- Parameters:
aClassLoader- The class loader to be used. May not benull.sPath- The path to be resolved. May neither benullnor empty. Internally it is ensured that the provided path does NOT start with a slash.- Returns:
nullif the path could not be resolved using the specified class loader.
-