Class ClassLoaderHelper

java.lang.Object
com.helger.base.classloader.ClassLoaderHelper

@Immutable public class ClassLoaderHelper extends Object
ClassLoader utility methods.
Author:
Philip Helger
  • Constructor Details

    • ClassLoaderHelper

      protected ClassLoaderHelper()
  • Method Details

    • getSystemClassLoader

      public static @NonNull ClassLoader getSystemClassLoader()
      Get the system class loader.
      Returns:
      The system class loader. Never null.
      See Also:
    • getContextClassLoader

      public static @Nullable ClassLoader getContextClassLoader()
      Get the context class loader of the current thread.
      Returns:
      The context class loader, or null if the context class loader indicates the system class loader.
      See Also:
    • setContextClassLoader

      public static void setContextClassLoader(@NonNull ClassLoader aClassLoader)
      Set the context class loader of the current thread.
      Parameters:
      aClassLoader - The class loader to set as context class loader. May not be null.
      See Also:
    • getClassClassLoader

      public static @Nullable ClassLoader getClassClassLoader(@NonNull Class<?> aClass)
      Get the class loader that loaded the given class.
      Parameters:
      aClass - The class whose class loader is to be retrieved. May not be null.
      Returns:
      The class loader of the given class, or null if the class was loaded by the bootstrap class loader.
      See Also:
    • getParentClassLoader

      public static @Nullable ClassLoader getParentClassLoader(@NonNull ClassLoader aClassLoader)
      Get the parent class loader of the given class loader.
      Parameters:
      aClassLoader - The class loader whose parent is to be retrieved. May not be null.
      Returns:
      The parent class loader, or null if the parent is the bootstrap class loader.
      See Also:
    • getDefaultClassLoader

      public static @NonNull ClassLoader 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 the ClassLoaderHelper class itself.
      Returns:
      The default class loader. Never null.
    • internalGetPathWithoutLeadingSlash

      protected static @NonNull String internalGetPathWithoutLeadingSlash(@Nonempty @NonNull @Nonempty String sPath)
    • 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 around classLoader.getResource (sPath).
      Parameters:
      aClassLoader - The class loader to be used. May not be null.
      sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does NOT start with a slash.
      Returns:
      null if 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 around classLoader.getResources (sPath).
      Parameters:
      aClassLoader - The class loader to be used. May not be null.
      sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does NOT start with a slash.
      Returns:
      null if 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 around classLoader.getResourceAsStream (sPath).
      Parameters:
      aClassLoader - The class loader to be used. May not be null.
      sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does NOT start with a slash.
      Returns:
      null if the path could not be resolved using the specified class loader.