Class ClassLoaderUtils

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.management.ClassLoadingMXBean classLoadingMXBean  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Set<java.net.URL> findAllClassPathURLs​(java.lang.ClassLoader classLoader)  
      static java.lang.Class<?> findLoadedClass​(java.lang.ClassLoader classLoader, java.lang.String className)
      Find Loaded Class under specified inheritable ClassLoader
      static java.util.Set<java.lang.Class<?>> findLoadedClasses​(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classNames)
      Find Loaded Class under specified inheritable ClassLoader and class names
      static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(java.lang.ClassLoader classLoader)
      Find loaded classes Set in class path
      static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(java.lang.ClassLoader classLoader, java.lang.String classPath)
      Find loaded classes Set in class path
      static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPaths​(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classPaths)
      Find loaded classes Set in class paths Set
      static java.net.URLClassLoader findURLClassLoader​(java.lang.ClassLoader classLoader)  
      static java.util.Set<java.lang.Class<?>> getAllLoadedClasses​(java.lang.ClassLoader classLoader)
      Get all loaded classes Set under specified inheritable ClassLoader
      static java.util.Map<java.lang.ClassLoader,​java.util.Set<java.lang.Class<?>>> getAllLoadedClassesMap​(java.lang.ClassLoader classLoader)
      Get all loaded classes Map under specified inheritable ClassLoader , ClassLoader as key , its loaded classes Set as value.
      static java.lang.ClassLoader getCallerClassLoader()
      Return the ClassLoader from the caller class
      static java.lang.ClassLoader getClassLoader​(java.lang.Class<?> loadedClass)
      Get the ClassLoader from the loaded class if present.
      static java.net.URL getClassResource​(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
      Get the Class resource URL under specified Class
      static java.net.URL getClassResource​(java.lang.ClassLoader classLoader, java.lang.String className)
      Get the Class resource URL under specified Class name
      static java.lang.ClassLoader getDefaultClassLoader()
      Get the default the ClassLoader to use.
      static java.util.Set<java.lang.ClassLoader> getInheritableClassLoaders​(java.lang.ClassLoader classLoader)
      Get all Inheritable ClassLoaders Set (including ClassLoader argument)
      static int getLoadedClassCount()
      Returns the number of classes that are currently loaded in the Java virtual machine.
      static java.util.Set<java.lang.Class<?>> getLoadedClasses​(java.lang.ClassLoader classLoader)
      Get loaded classes Set under specified ClassLoader( not all inheritable ClassLoaders)
      static java.net.URL getResource​(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName)
      Get the resource URL under specified resource name and type
      static java.net.URL getResource​(java.lang.ClassLoader classLoader, java.lang.String resourceName)
      Get the resource URL under specified resource name
      static java.util.Set<java.net.URL> getResources​(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName)
      Get the resource URLs Set under specified resource name and type
      static java.util.Set<java.net.URL> getResources​(java.lang.ClassLoader classLoader, java.lang.String resourceName)
      Get the resource URLs list under specified resource name
      static long getTotalLoadedClassCount()
      Returns the total number of classes that have been loaded since the Java virtual machine has started execution.
      static long getUnloadedClassCount()
      Returns the total number of classes unloaded since the Java virtual machine has started execution.
      static boolean isLoadedClass​(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
      Check specified Class is loaded on specified inheritable ClassLoader
      static boolean isLoadedClass​(java.lang.ClassLoader classLoader, java.lang.String className)
      Check specified class name is loaded on specified inheritable ClassLoader
      static boolean isPresent​(java.lang.String className)
      Test the specified class name is present in the default ClassLoader
      static boolean isPresent​(java.lang.String className, java.lang.ClassLoader classLoader)
      Test the specified class name is present in the ClassLoader
      static boolean isVerbose()
      Tests if the verbose output for the class loading system is enabled.
      static java.lang.Class<?> loadClass​(java.lang.String className, java.lang.ClassLoader classLoader)
      Loaded specified class name under ClassLoader
      static java.lang.Class<?> loadClass​(java.lang.String className, java.lang.ClassLoader classLoader, boolean cached)
      Loaded specified class name under ClassLoader
      static boolean removeClassPathURL​(java.lang.ClassLoader classLoader, java.net.URL url)  
      static java.lang.Class<?> resolveClass​(java.lang.String className)
      Resolve the Class by the specified name in the default ClassLoader
      static java.lang.Class<?> resolveClass​(java.lang.String className, java.lang.ClassLoader classLoader)
      Resolve the Class by the specified name and ClassLoader
      static java.lang.Class<?> resolveClass​(java.lang.String className, java.lang.ClassLoader classLoader, boolean cached)
      Resolve the Class by the specified name and ClassLoader
      static void setVerbose​(boolean value)
      Enables or disables the verbose output for the class loading system.
      • Methods inherited from class java.lang.Object

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

      • classLoadingMXBean

        protected static final java.lang.management.ClassLoadingMXBean classLoadingMXBean
    • Constructor Detail

      • ClassLoaderUtils

        public ClassLoaderUtils()
    • Method Detail

      • getLoadedClassCount

        public static int getLoadedClassCount()
        Returns the number of classes that are currently loaded in the Java virtual machine.
        Returns:
        the number of currently loaded classes.
      • getUnloadedClassCount

        public static long getUnloadedClassCount()
        Returns the total number of classes unloaded since the Java virtual machine has started execution.
        Returns:
        the total number of unloaded classes.
      • isVerbose

        public static boolean isVerbose()
        Tests if the verbose output for the class loading system is enabled.
        Returns:
        true if the verbose output for the class loading system is enabled; false otherwise.
      • setVerbose

        public static void setVerbose​(boolean value)
        Enables or disables the verbose output for the class loading system. The verbose output information and the output stream to which the verbose information is emitted are implementation dependent. Typically, a Java virtual machine implementation prints a message each time a class file is loaded.

        This method can be called by multiple threads concurrently. Each invocation of this method enables or disables the verbose output globally.

        Parameters:
        value - true to enable the verbose output; false to disable.
        Throws:
        java.lang.SecurityException - if a security manager exists and the caller does not have ManagementPermission("control").
      • getTotalLoadedClassCount

        public static long getTotalLoadedClassCount()
        Returns the total number of classes that have been loaded since the Java virtual machine has started execution.
        Returns:
        the total number of classes loaded.
      • getDefaultClassLoader

        @Nullable
        public static java.lang.ClassLoader getDefaultClassLoader()
        Get the default the ClassLoader to use.
        Returns:
        the ClassLoader (only null if even the system ClassLoader isn't accessible)
        See Also:
        Thread.getContextClassLoader(), Class.getClassLoader(), ClassLoader.getSystemClassLoader(), ReflectionUtils.getCallerClass()
      • getClassLoader

        @Nullable
        public static java.lang.ClassLoader getClassLoader​(@Nullable
                                                           java.lang.Class<?> loadedClass)
        Get the ClassLoader from the loaded class if present.
        Parameters:
        loadedClass - the optional class was loaded by some ClassLoader
        Returns:
        the ClassLoader (only null if even the system ClassLoader isn't accessible)
        See Also:
        getDefaultClassLoader()
      • getCallerClassLoader

        public static java.lang.ClassLoader getCallerClassLoader()
        Return the ClassLoader from the caller class
        Returns:
        the ClassLoader (only null if the caller class was absent
        See Also:
        ReflectionUtils.getCallerClass()
      • findLoadedClasses

        public static java.util.Set<java.lang.Class<?>> findLoadedClasses​(java.lang.ClassLoader classLoader,
                                                                          java.util.Set<java.lang.String> classNames)
        Find Loaded Class under specified inheritable ClassLoader and class names
        Parameters:
        classLoader - ClassLoader
        classNames - class names set
        Returns:
        Class if loaded , or null
      • isLoadedClass

        public static boolean isLoadedClass​(java.lang.ClassLoader classLoader,
                                            java.lang.Class<?> type)
        Check specified Class is loaded on specified inheritable ClassLoader
        Parameters:
        classLoader - ClassLoader
        type - Class
        Returns:
        If Loaded , return true , or false
      • isLoadedClass

        public static boolean isLoadedClass​(java.lang.ClassLoader classLoader,
                                            java.lang.String className)
        Check specified class name is loaded on specified inheritable ClassLoader
        Parameters:
        classLoader - ClassLoader
        className - class name
        Returns:
        If Loaded , return true , or false
      • findLoadedClass

        public static java.lang.Class<?> findLoadedClass​(java.lang.ClassLoader classLoader,
                                                         java.lang.String className)
        Find Loaded Class under specified inheritable ClassLoader
        Parameters:
        classLoader - ClassLoader
        className - class name
        Returns:
        Class if loaded , or null
      • loadClass

        @Nullable
        public static java.lang.Class<?> loadClass​(@Nonnull
                                                   java.lang.String className,
                                                   @Nonnull
                                                   java.lang.ClassLoader classLoader)
        Loaded specified class name under ClassLoader
        Parameters:
        className - the name of Class
        classLoader - ClassLoader
        Returns:
        Class if can be loaded
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className,
                                                   java.lang.ClassLoader classLoader,
                                                   boolean cached)
        Loaded specified class name under ClassLoader
        Parameters:
        className - the name of Class
        classLoader - ClassLoader
        cached - the resolved class is required to be cached or not
        Returns:
        Class if can be loaded
      • getResources

        public static java.util.Set<java.net.URL> getResources​(java.lang.ClassLoader classLoader,
                                                               ClassLoaderUtils.ResourceType resourceType,
                                                               java.lang.String resourceName)
                                                        throws java.lang.NullPointerException,
                                                               java.io.IOException
        Get the resource URLs Set under specified resource name and type
        Parameters:
        classLoader - ClassLoader
        resourceType - ClassLoaderUtils.ResourceType Enum
        resourceName - resource name ,e.g :
        • Resource Name :"/com/abc/def.log"
        • Class Name : "java.lang.String"
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If any argument is null
        java.io.IOException
      • getResources

        public static java.util.Set<java.net.URL> getResources​(java.lang.ClassLoader classLoader,
                                                               java.lang.String resourceName)
                                                        throws java.lang.NullPointerException,
                                                               java.io.IOException
        Get the resource URLs list under specified resource name
        Parameters:
        classLoader - ClassLoader
        resourceName - resource name ,e.g :
        • Resource Name :"/com/abc/def.log"
        • Class Name : "java.lang.String"
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If any argument is null
        java.io.IOException
      • getResource

        public static java.net.URL getResource​(java.lang.ClassLoader classLoader,
                                               java.lang.String resourceName)
                                        throws java.lang.NullPointerException
        Get the resource URL under specified resource name
        Parameters:
        classLoader - ClassLoader
        resourceName - resource name ,e.g :
        • Resource Name :"/com/abc/def.log"
        • Class Name : "java.lang.String"
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If any argument is null
      • getResource

        public static java.net.URL getResource​(java.lang.ClassLoader classLoader,
                                               ClassLoaderUtils.ResourceType resourceType,
                                               java.lang.String resourceName)
                                        throws java.lang.NullPointerException
        Get the resource URL under specified resource name and type
        Parameters:
        classLoader - ClassLoader
        resourceType - ClassLoaderUtils.ResourceType Enum
        resourceName - resource name ,e.g :
        • Resource Name :"/com/abc/def.log"
        • Class Name : "java.lang.String"
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If any argument is null
      • getClassResource

        public static java.net.URL getClassResource​(java.lang.ClassLoader classLoader,
                                                    java.lang.String className)
        Get the Class resource URL under specified Class name
        Parameters:
        classLoader - ClassLoader
        className - class name
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If any argument is null
      • getClassResource

        public static java.net.URL getClassResource​(java.lang.ClassLoader classLoader,
                                                    java.lang.Class<?> type)
        Get the Class resource URL under specified Class
        Parameters:
        classLoader - ClassLoader
        type - type
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If any argument is null
        Since:
        1.0.0
      • getInheritableClassLoaders

        @Nonnull
        public static java.util.Set<java.lang.ClassLoader> getInheritableClassLoaders​(java.lang.ClassLoader classLoader)
                                                                               throws java.lang.NullPointerException
        Get all Inheritable ClassLoaders Set (including ClassLoader argument)
        Parameters:
        classLoader - ClassLoader
        Returns:
        Read-only Set
        Throws:
        java.lang.NullPointerException - If classLoader argument is null
      • getAllLoadedClassesMap

        @Nonnull
        public static java.util.Map<java.lang.ClassLoader,​java.util.Set<java.lang.Class<?>>> getAllLoadedClassesMap​(java.lang.ClassLoader classLoader)
                                                                                                                   throws java.lang.UnsupportedOperationException
        Get all loaded classes Map under specified inheritable ClassLoader , ClassLoader as key , its loaded classes Set as value.
        Parameters:
        classLoader - ClassLoader
        Returns:
        Read-only Map
        Throws:
        java.lang.UnsupportedOperationException
        java.lang.NullPointerException - If classLoader argument is null
      • getAllLoadedClasses

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> getAllLoadedClasses​(java.lang.ClassLoader classLoader)
                                                                     throws java.lang.UnsupportedOperationException
        Get all loaded classes Set under specified inheritable ClassLoader
        Parameters:
        classLoader - ClassLoader
        Returns:
        Read-only Set
        Throws:
        java.lang.UnsupportedOperationException - If JVM does not support
        java.lang.NullPointerException - If classLoader argument is null
      • getLoadedClasses

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> getLoadedClasses​(java.lang.ClassLoader classLoader)
                                                                  throws java.lang.UnsupportedOperationException
        Get loaded classes Set under specified ClassLoader( not all inheritable ClassLoaders)
        Parameters:
        classLoader - ClassLoader
        Returns:
        Read-only Set
        Throws:
        java.lang.UnsupportedOperationException - If JVM does not support
        java.lang.NullPointerException - If classLoader argument is null
        See Also:
        getAllLoadedClasses(ClassLoader)
      • findLoadedClassesInClassPath

        public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(java.lang.ClassLoader classLoader)
                                                                              throws java.lang.UnsupportedOperationException
        Find loaded classes Set in class path
        Parameters:
        classLoader - ClassLoader
        Returns:
        Read-only Set
        Throws:
        java.lang.UnsupportedOperationException - If JVM does not support
      • findLoadedClassesInClassPaths

        public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPaths​(java.lang.ClassLoader classLoader,
                                                                                      java.util.Set<java.lang.String> classPaths)
                                                                               throws java.lang.UnsupportedOperationException
        Find loaded classes Set in class paths Set
        Parameters:
        classLoader - ClassLoader
        classPaths - the class paths for the Set of JarFile or classes directory
        Returns:
        Read-only Set
        Throws:
        java.lang.UnsupportedOperationException - If JVM does not support
        See Also:
        findLoadedClass(ClassLoader, String)
      • findLoadedClassesInClassPath

        public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(java.lang.ClassLoader classLoader,
                                                                                     java.lang.String classPath)
                                                                              throws java.lang.UnsupportedOperationException
        Find loaded classes Set in class path
        Parameters:
        classLoader - ClassLoader
        classPath - the class path for one JarFile or classes directory
        Returns:
        Read-only Set
        Throws:
        java.lang.UnsupportedOperationException - If JVM does not support
        See Also:
        findLoadedClass(ClassLoader, String)
      • isPresent

        public static boolean isPresent​(@Nullable
                                        java.lang.String className)
        Test the specified class name is present in the default ClassLoader
        Parameters:
        className - the name of Class
        Returns:
        If found, return true
      • isPresent

        public static boolean isPresent​(@Nullable
                                        java.lang.String className,
                                        @Nullable
                                        java.lang.ClassLoader classLoader)
        Test the specified class name is present in the ClassLoader
        Parameters:
        className - the name of Class
        classLoader - ClassLoader
        Returns:
        If found, return true
      • resolveClass

        public static java.lang.Class<?> resolveClass​(@Nullable
                                                      java.lang.String className)
        Resolve the Class by the specified name in the default ClassLoader
        Parameters:
        className - the name of Class
        Returns:
        If can't be resolved , return null
      • resolveClass

        public static java.lang.Class<?> resolveClass​(@Nullable
                                                      java.lang.String className,
                                                      @Nullable
                                                      java.lang.ClassLoader classLoader)
        Resolve the Class by the specified name and ClassLoader
        Parameters:
        className - the name of Class
        classLoader - ClassLoader
        Returns:
        If can't be resolved , return null
      • resolveClass

        public static java.lang.Class<?> resolveClass​(@Nullable
                                                      java.lang.String className,
                                                      @Nullable
                                                      java.lang.ClassLoader classLoader,
                                                      boolean cached)
        Resolve the Class by the specified name and ClassLoader
        Parameters:
        className - the name of Class
        classLoader - ClassLoader
        cached - the resolved class is required to be cached or not
        Returns:
        If can't be resolved , return null
      • findAllClassPathURLs

        public static java.util.Set<java.net.URL> findAllClassPathURLs​(java.lang.ClassLoader classLoader)
      • removeClassPathURL

        public static boolean removeClassPathURL​(java.lang.ClassLoader classLoader,
                                                 java.net.URL url)
      • findURLClassLoader

        public static java.net.URLClassLoader findURLClassLoader​(java.lang.ClassLoader classLoader)