Class ClassLoaderUtils


  • public final class ClassLoaderUtils
    extends Object

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

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

      • getClassLoader

        public static ClassLoader getClassLoader(Class<?> 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 Class<?> loadClass(String className)
                                  throws ClassNotFoundException

        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:
        ClassNotFoundException - if the class could not be loaded.
        Since:
        3.0.3
      • findClass

        public static Class<?> findClass(String 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(String 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 URL findResource(String 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(String 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 InputStream loadResourceAsStream(String resourceName)
                                                throws IOException

        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:
        IOException - if the resource could not be located.
        Since:
        3.0.3
      • findResourceAsStream

        public static InputStream findResourceAsStream(String 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