Class ClassLoaderUtils


  • public final class ClassLoaderUtils
    extends java.lang.Object

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

    Since:
    2.0.6
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ClassLoaderUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> findClass​(java.lang.String className)
      Try to obtain a class by name, returning null if not found.
      static java.net.URL findResource​(java.lang.String resourceName)
      Try to obtain a resource by name, returning null if it could not be located.
      static java.io.InputStream findResourceAsStream​(java.lang.String resourceName)
      Try to obtain a resource by name, returning null if it could not be located.
      private static java.lang.ClassLoader getClassClassLoader​(java.lang.Class<?> clazz)  
      static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> clazz)
      Try to obtain a classloader, following these priorities:
      private static java.lang.ClassLoader getSystemClassLoader()  
      private static java.lang.ClassLoader getThreadContextClassLoader()  
      static boolean isClassPresent​(java.lang.String className)
      Checks whether a class is present at the application's class path.
      private static boolean isKnownClassLoaderAccessibleFrom​(java.lang.ClassLoader accessibleCL, java.lang.ClassLoader fromCL)  
      private static boolean isKnownLeafClassLoader​(java.lang.ClassLoader classLoader)  
      static boolean isResourcePresent​(java.lang.String resourceName)
      Checks whether a resource is present at the application's class path.
      static java.lang.Class<?> loadClass​(java.lang.String className)
      Obtain a class by name, throwing an exception if it is not present.
      static java.io.InputStream loadResourceAsStream​(java.lang.String resourceName)
      Obtain a resource by name, throwing an exception if it is not present.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • classClassLoader

        private static final java.lang.ClassLoader classClassLoader
      • systemClassLoader

        private static final java.lang.ClassLoader systemClassLoader
      • systemClassLoaderAccessibleFromClassClassLoader

        private static final boolean systemClassLoaderAccessibleFromClassClassLoader
    • Constructor Detail

      • ClassLoaderUtils

        private ClassLoaderUtils()
    • Method Detail

      • getClassLoader

        public static java.lang.ClassLoader getClassLoader​(java.lang.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 java.lang.Class<?> loadClass​(java.lang.String className)
                                            throws java.lang.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:
        java.lang.ClassNotFoundException - if the class could not be loaded.
        Since:
        3.0.3
      • findClass

        public static java.lang.Class<?> findClass​(java.lang.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​(java.lang.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 java.net.URL findResource​(java.lang.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​(java.lang.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 java.io.InputStream loadResourceAsStream​(java.lang.String resourceName)
                                                        throws java.io.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:
        java.io.IOException - if the resource could not be located.
        Since:
        3.0.3
      • findResourceAsStream

        public static java.io.InputStream findResourceAsStream​(java.lang.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
      • getThreadContextClassLoader

        private static java.lang.ClassLoader getThreadContextClassLoader()
      • getClassClassLoader

        private static java.lang.ClassLoader getClassClassLoader​(java.lang.Class<?> clazz)
      • getSystemClassLoader

        private static java.lang.ClassLoader getSystemClassLoader()
      • isKnownClassLoaderAccessibleFrom

        private static boolean isKnownClassLoaderAccessibleFrom​(java.lang.ClassLoader accessibleCL,
                                                                java.lang.ClassLoader fromCL)
      • isKnownLeafClassLoader

        private static boolean isKnownLeafClassLoader​(java.lang.ClassLoader classLoader)