Class ClassLoaderUtils


  • public final class ClassLoaderUtils
    extends 

    Utility class for obtaining a correct classloader on which to operate from a specific class.

    Since:
    2.0.6
    Author:
    Daniel Fernández
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <?>  className)
      Try to obtain a class by name, returning null if not found.
      static  resourceName)
      Try to obtain a resource by name, returning null if it could not be located.
      static  resourceName)
      Try to obtain a resource by name, returning null if it could not be located.
      static <?> clazz)
      Try to obtain a classloader, following these priorities:
      static boolean  className)
      Checks whether a class is present at the application's class path.
      static boolean  resourceName)
      Checks whether a resource is present at the application's class path.
      static <?>  className)
      Obtain a class by name, throwing an exception if it is not present.
      static  resourceName)
      Obtain a resource by name, throwing an exception if it is not present.
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Method Detail

      • getClassLoader

        public static  getClassLoader​(<?> clazz)

        Try to obtain a classloader, following these priorities:

        1. If there is a thread context class loader, return it.
        2. Else if there is a class loader related to the class passed as argument, return it.
        3. Else return the system class loader.
        Parameters:
        clazz - the class which loader will be obtained in the second step. Can be null (that will skip that second step).
        Returns:
        a non-null, safe classloader to use.
      • loadClass

        public static <?> loadClass​( className)
                                  throws 

        Obtain a class by name, throwing an exception if it is not present.

        First the context class loader will be used. If this class loader is not able to load the class, then the class class loader (ClassLoaderUtils.class.getClassLoader()) will be used if it is different from the thread context one. Last, the System class loader will be tried.

        This method does never return null.

        Parameters:
        className - the name of the class to be obtained.
        Returns:
        the loaded class (null never returned).
        Throws:
        - if the class could not be loaded.
        Since:
        3.0.3
      • findClass

        public static <?> findClass​( className)

        Try to obtain a class by name, returning null if not found.

        This method works very similarly to loadClass(String) but will just return null if the class is not found by the sequence of class loaders being tried.

        Parameters:
        className - the name of the class to be obtained.
        Returns:
        the found class, or null if it could not be found.
        Since:
        3.0.3
      • isClassPresent

        public static boolean isClassPresent​( className)

        Checks whether a class is present at the application's class path.

        This method works very similarly to findClass(String) but will just return true or false depending on whether the class could be found or not.

        Parameters:
        className - the name of the class to be checked.
        Returns:
        true if the class was found (by any class loader), false if not.
        Since:
        3.0.3
      • findResource

        public static  findResource​( resourceName)

        Try to obtain a resource by name, returning null if it could not be located.

        First the context class loader will be used. If this class loader is not able to locate the resource, then the class class loader (ClassLoaderUtils.class.getClassLoader()) will be used if it is different from the thread context one. Last, the System class loader will be tried.

        Parameters:
        resourceName - the name of the resource to be obtained.
        Returns:
        the found resource, or null if it could not be located.
        Since:
        3.0.3
      • isResourcePresent

        public static boolean isResourcePresent​( resourceName)

        Checks whether a resource is present at the application's class path.

        This method works very similarly to findResource(String) but will just return true or false depending on whether the resource could be located or not.

        Parameters:
        resourceName - the name of the resource to be checked.
        Returns:
        true if the class was located (by any class loader), false if not.
        Since:
        3.0.3
      • loadResourceAsStream

        public static  loadResourceAsStream​( resourceName)
                                                throws 

        Obtain a resource by name, throwing an exception if it is not present.

        First the context class loader will be used. If this class loader is not able to locate the resource, then the class class loader (ClassLoaderUtils.class.getClassLoader()) will be used if it is different from the thread context one. Last, the System class loader will be tried.

        This method does never return null.

        Parameters:
        resourceName - the name of the resource to be obtained.
        Returns:
        an input stream on the resource (null never returned).
        Throws:
        - if the resource could not be located.
        Since:
        3.0.3
      • findResourceAsStream

        public static  findResourceAsStream​( resourceName)

        Try to obtain a resource by name, returning null if it could not be located.

        This method works very similarly to loadResourceAsStream(String) but will just return null if the resource cannot be located by the sequence of class loaders being tried.

        Parameters:
        resourceName - the name of the resource to be obtained.
        Returns:
        an input stream on the resource, or null if it could not be located.
        Since:
        3.0.3