Package org.unbescape

Class ClassLoaderUtils


  • final class ClassLoaderUtils
    extends java.lang.Object

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

    Since:
    1.1.6
    • 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

      • loadResourceAsStream

        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.
      • findResourceAsStream

        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.
      • 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)