Class ClassLoaderUtils

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ClassLoaderUtils.ResourceType
      ResourceType defines different types of resources that can be accessed through a ClassLoader.
    • 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
      protected static java.lang.Class<?> doLoadClass​(java.lang.ClassLoader classLoader, java.lang.String className)  
      static java.util.Set<java.net.URL> findAllClassPathURLs​(java.lang.ClassLoader classLoader)
      Retrieves all URLs from the class path associated with the specified ClassLoader and its parent hierarchy.
      static java.lang.Class<?> findLoadedClass​(java.lang.ClassLoader classLoader, java.lang.String className)
      Finds a class that has already been loaded by the given ClassLoader or its parent hierarchy.
      static java.util.Set<java.lang.Class<?>> findLoadedClasses​(java.lang.ClassLoader classLoader, java.lang.Iterable<java.lang.String> classNames)
      Finds and returns a set of loaded classes for the given class names using the specified ClassLoader.
      static java.util.Set<java.lang.Class<?>> findLoadedClasses​(java.lang.ClassLoader classLoader, java.lang.String... classNames)
      Finds and returns a set of loaded classes for the given class names using the specified ClassLoader.
      static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(java.lang.ClassLoader classLoader)
      Retrieves a set of classes that have been loaded from the class path using the specified ClassLoader or the default one.
      static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(java.lang.ClassLoader classLoader, java.lang.String classPath)
      Retrieves a set of classes that have been loaded from the specified class path using the provided ClassLoader.
      static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPaths​(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classPaths)
      Finds and returns a set of classes that have been loaded from the specified class paths using the provided ClassLoader.
      static java.net.URLClassLoader findURLClassLoader​(java.lang.ClassLoader classLoader)
      Attempts to find and return the first URLClassLoader in the hierarchy starting from the provided ClassLoader.
      static java.util.Set<java.lang.Class<?>> getAllLoadedClasses​(java.lang.ClassLoader classLoader)
      Retrieves a set of all classes that have been loaded by the specified ClassLoader and its parent hierarchy.
      static java.util.Map<java.lang.ClassLoader,​java.util.Set<java.lang.Class<?>>> getAllLoadedClassesMap​(java.lang.ClassLoader classLoader)
      Retrieves a map of ClassLoader instances to their corresponding sets of loaded classes.
      static java.lang.ClassLoader getCallerClassLoader()
      Retrieves the ClassLoader of the caller class from the call stack.
      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.Class<?> type)
      Returns the resource URL for the class file corresponding to the specified class.
      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)
      Retrieves the resource URL for the class file corresponding to the specified class name.
      static java.lang.ClassLoader getDefaultClassLoader()
      Retrieves the default ClassLoader to use when none is explicitly provided.
      static java.util.Set<java.lang.ClassLoader> getInheritableClassLoaders​(java.lang.ClassLoader classLoader)
      Retrieves a set of inheritable ClassLoader instances starting from the specified ClassLoader, including its parent hierarchy.
      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)
      Retrieves the set of classes that have been loaded by the specified ClassLoader.
      static java.net.URL getResource​(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName)
      Retrieves a resource URL for the specified resource name using the provided ClassLoader and resource type.
      static java.net.URL getResource​(java.lang.ClassLoader classLoader, java.lang.String resourceName)
      Retrieves a resource URL for the specified resource name using the provided ClassLoader.
      static java.net.URL getResource​(java.lang.String resourceName)
      Get the resource URL under specified resource name using the default ClassLoader.
      static java.lang.String getResourceAsString​(java.lang.ClassLoader classLoader, java.lang.String resourceName)
      Gets the content of the specified resource as a String using the provided ClassLoader.
      static java.lang.String getResourceAsString​(java.lang.String resourceName)
      Gets the content of the specified resource as a String using the default ClassLoader.
      static java.util.Set<java.net.URL> getResources​(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName)
      Retrieves a set of URLs representing resources with the specified type and name using the provided ClassLoader.
      static java.util.Set<java.net.URL> getResources​(java.lang.ClassLoader classLoader, java.lang.String resourceName)
      Retrieves a set of URLs representing resources with the specified name using the provided ClassLoader.
      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 that have been unloaded since the Java virtual machine has started execution.
      static boolean isLoadedClass​(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
      Checks if the specified class is already loaded by the given ClassLoader or its parent hierarchy.
      static boolean isLoadedClass​(java.lang.ClassLoader classLoader, java.lang.String className)
      Checks if the specified class name is already loaded by the given ClassLoader or its parent hierarchy.
      static boolean isPresent​(java.lang.String className)
      Checks if a class with the specified name is present in the default ClassLoader.
      static boolean isPresent​(java.lang.String className, java.lang.ClassLoader classLoader)
      Checks if a class with the specified name is present in the given ClassLoader or the default ClassLoader.
      static boolean isVerbose()
      Checks if the Java virtual machine is currently running with verbose class loading output enabled.
      static java.lang.Class<?> loadClass​(java.lang.ClassLoader classLoader, java.lang.String className)
      Loads the class with the specified name using the provided ClassLoader or the default ClassLoader if none is specified.
      static java.lang.Class<?> loadClass​(java.lang.ClassLoader classLoader, java.lang.String className, boolean cached)
      Loads the class with the specified name using the provided ClassLoader or the default ClassLoader if none is specified, optionally caching the result for faster subsequent lookups.
      static java.net.URLClassLoader newURLClassLoader​(java.lang.Iterable<java.net.URL> urls)
      Creates a new instance of URLClassLoader using the URLs provided in an Iterable.
      static java.net.URLClassLoader newURLClassLoader​(java.lang.Iterable<java.net.URL> urls, java.lang.ClassLoader classLoader)
      Creates a new instance of URLClassLoader using the URLs provided in an Iterable.
      static java.net.URLClassLoader newURLClassLoader​(java.net.URL[] urls)
      Create a new instance of URLClassLoader
      static java.net.URLClassLoader newURLClassLoader​(java.net.URL[] urls, boolean initializedLoaders)
      Create a new instance of URLClassLoader
      static java.net.URLClassLoader newURLClassLoader​(java.net.URL[] urls, java.lang.ClassLoader parent)
      Creates a new instance of URLClassLoader using the provided array of URLs and an optional parent ClassLoader.
      static java.net.URLClassLoader newURLClassLoader​(java.net.URL[] urls, java.lang.ClassLoader parent, boolean initializedLoaders)
      Create a new instance of URLClassLoader
      static boolean removeClassPathURL​(java.lang.ClassLoader classLoader, java.net.URL url)
      Removes the specified URL from the class path of the given ClassLoader if it supports such operation.
      static java.lang.Class<?> resolveClass​(java.lang.String className)
      Resolves a class by its name using the default ClassLoader.
      static java.lang.Class<?> resolveClass​(java.lang.String className, java.lang.ClassLoader classLoader)
      Resolves a class by its name using the provided ClassLoader or the default ClassLoader if none is specified.
      static java.lang.Class<?> resolveClass​(java.lang.String className, java.lang.ClassLoader classLoader, boolean cached)
      Resolves a class by its name using the provided ClassLoader or the default ClassLoader if none is specified, optionally caching the result for faster subsequent lookups.
      static java.net.URLClassLoader resolveURLClassLoader​(java.lang.ClassLoader classLoader)
      Resolves or creates a URLClassLoader instance from the specified 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
    • Method Detail

      • getLoadedClassCount

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

        Example Usage

        
         int loadedClassCount = ClassLoaderUtils.getLoadedClassCount();
         System.out.println("Currently loaded classes: " + loadedClassCount);
         
        Returns:
        the number of currently loaded classes.
      • getUnloadedClassCount

        public static long getUnloadedClassCount()
        Returns the total number of classes that have been unloaded since the Java virtual machine has started execution.

        Example Usage

        
         long unloadedClassCount = ClassLoaderUtils.getUnloadedClassCount();
         System.out.println("Total unloaded classes: " + unloadedClassCount);
         
        Returns:
        the total number of classes unloaded.
      • getTotalLoadedClassCount

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

        Example Usage

        
         long totalLoadedClassCount = ClassLoaderUtils.getTotalLoadedClassCount();
         System.out.println("Total loaded classes: " + totalLoadedClassCount);
         
        Returns:
        the total number of classes loaded.
      • isVerbose

        public static boolean isVerbose()
        Checks if the Java virtual machine is currently running with verbose class loading output enabled.

        This method delegates to the underlying JVM's ClassLoadingMXBean.isVerbose() to determine whether verbose mode is active. When verbose mode is enabled, the JVM typically prints a message each time a class file is loaded.

        Example Usage

        
         boolean isVerbose = ClassLoaderUtils.isVerbose();
         if (isVerbose) {
             System.out.println("JVM is running in verbose class loading mode.");
         } else {
             System.out.println("Verbose class loading mode is disabled.");
         }
         
        Returns:
        true if the JVM is running with verbose class loading output enabled; false otherwise.
      • setVerbose

        public static void setVerbose​(boolean value)
        Enables or disables the verbose output for the class loading system.

        When verbose mode is enabled, the JVM typically prints a message each time a class file is loaded. The verbose output information and the output stream to which it is emitted are implementation-dependent. This method can be called by multiple threads concurrently. Each invocation enables or disables the verbose output globally.

        Example Usage

        
         // Enable verbose class loading output
         ClassLoaderUtils.setVerbose(true);
        
         // Disable verbose class loading output
         ClassLoaderUtils.setVerbose(false);
         
        Parameters:
        value - true to enable verbose output; false to disable it.
        Throws:
        java.lang.SecurityException - if a security manager exists and the caller does not have the required ManagementPermission with "control" action.
      • getDefaultClassLoader

        @Nullable
        public static java.lang.ClassLoader getDefaultClassLoader()
        Retrieves the default ClassLoader to use when none is explicitly provided. This method attempts to find the most appropriate ClassLoader by checking:
        1. The context ClassLoader of the current thread
        2. If that's not available, the ClassLoader that loaded this class (ClassLoaderUtils)
        3. Finally, if neither is available, it falls back to the system ClassLoader

        Example Usage

        
         ClassLoader classLoader = ClassLoaderUtils.getDefaultClassLoader();
         if (classLoader != null) {
             System.out.println("Using default ClassLoader: " + classLoader);
         } else {
             System.out.println("No suitable ClassLoader found.");
         }
         
        Returns:
        the default ClassLoader, or null if no suitable ClassLoader could be determined
      • getClassLoader

        @Nullable
        public static java.lang.ClassLoader getClassLoader​(@Nullable
                                                           java.lang.Class<?> loadedClass)
        Get the ClassLoader from the loaded class if present.

        If the provided loadedClass is null, this method attempts to find the ClassLoader of the caller class using the specified stack frame depth. If that fails, it falls back to the system ClassLoader.

        Example Usage

        
         // Case 1: Get ClassLoader from a known class
         ClassLoader classLoader = ClassLoaderUtils.getClassLoader(MyClass.class);
         System.out.println("ClassLoader for MyClass: " + classLoader);
         
        
         // Case 2: Get ClassLoader when no class is provided (uses caller's ClassLoader)
         ClassLoader classLoader = ClassLoaderUtils.getClassLoader(null);
         System.out.println("Default ClassLoader: " + classLoader);
         
        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:
        getCallerClassLoader(), getDefaultClassLoader()
      • getCallerClassLoader

        @Nullable
        public static java.lang.ClassLoader getCallerClassLoader()
        Retrieves the ClassLoader of the caller class from the call stack.

        This method is a convenience wrapper that determines the caller's class by analyzing the call stack, and then returns the associated ClassLoader for that class. It uses a fixed stack frame depth of 4 to identify the caller, which is suitable for most direct usage scenarios.

        Example Usage

        
         ClassLoader callerClassLoader = ClassLoaderUtils.getCallerClassLoader();
         if (callerClassLoader != null) {
             System.out.println("Caller ClassLoader: " + callerClassLoader);
         } else {
             System.out.println("Caller ClassLoader is not available.");
         }
         
        Returns:
        the ClassLoader of the caller class, or null if it cannot be determined
        See Also:
        ReflectionUtils.getCallerClass()
      • findLoadedClasses

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> findLoadedClasses​(@Nullable
                                                                          java.lang.ClassLoader classLoader,
                                                                          java.lang.String... classNames)
        Finds and returns a set of loaded classes for the given class names using the specified ClassLoader.

        If the provided ClassLoader is null, it will use the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Find loaded classes with a specific ClassLoader
         ClassLoader classLoader = MyClass.class.getClassLoader();
         Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClasses(classLoader, "com.example.ClassA", "com.example.ClassB");
         System.out.println("Loaded Classes: " + loadedClasses);
         
        
         // Example 2: Use the default ClassLoader to find loaded classes
         Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClasses(null, "com.example.ClassC", "com.example.ClassD");
         System.out.println("Loaded Classes: " + loadedClasses);
         
        Parameters:
        classLoader - the ClassLoader to use for loading classes, may be null
        classNames - the array of class names to check if they are already loaded
        Returns:
        a set of loaded classes corresponding to the provided class names; the set will be empty if none of the classes are loaded
      • findLoadedClasses

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> findLoadedClasses​(@Nullable
                                                                          java.lang.ClassLoader classLoader,
                                                                          java.lang.Iterable<java.lang.String> classNames)
        Finds and returns a set of loaded classes for the given class names using the specified ClassLoader.

        If the provided ClassLoader is null, it will use the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Find loaded classes with a specific ClassLoader
         ClassLoader classLoader = MyClass.class.getClassLoader();
         Iterable<String> classNames = Arrays.asList("com.example.ClassA", "com.example.ClassB");
         Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClasses(classLoader, classNames);
         System.out.println("Loaded Classes: " + loadedClasses);
         
        
         // Example 2: Use the default ClassLoader to find loaded classes
         Iterable<String> classNames = Arrays.asList("com.example.ClassC", "com.example.ClassD");
         Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClasses(null, classNames);
         System.out.println("Loaded Classes: " + loadedClasses);
         
        Parameters:
        classLoader - the ClassLoader to use for loading classes, may be null
        classNames - the iterable collection of class names to check if they are already loaded
        Returns:
        a set of loaded classes corresponding to the provided class names; the set will be empty if none of the classes are loaded
      • isLoadedClass

        public static boolean isLoadedClass​(@Nullable
                                            java.lang.ClassLoader classLoader,
                                            java.lang.Class<?> type)
        Checks if the specified class is already loaded by the given ClassLoader or its parent hierarchy.

        If the provided ClassLoader is null, it will use the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Check if a class is loaded using a specific ClassLoader
         ClassLoader classLoader = MyClass.class.getClassLoader();
         boolean isLoaded = ClassLoaderUtils.isLoadedClass(classLoader, MyClass.class);
         System.out.println("Is MyClass loaded? " + isLoaded);
         
        
         // Example 2: Use the default ClassLoader to check if a class is loaded
         boolean isLoaded = ClassLoaderUtils.isLoadedClass(null, SomeClass.class);
         System.out.println("Is SomeClass loaded? " + isLoaded);
         
        Parameters:
        classLoader - the ClassLoader to check, may be null
        type - the Class object representing the class to check
        Returns:
        true if the class is already loaded; false otherwise
      • isLoadedClass

        public static boolean isLoadedClass​(@Nullable
                                            java.lang.ClassLoader classLoader,
                                            java.lang.String className)
        Checks if the specified class name is already loaded by the given ClassLoader or its parent hierarchy.

        If the provided ClassLoader is null, it will use the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Check if a class is loaded using a specific ClassLoader
         ClassLoader classLoader = MyClass.class.getClassLoader();
         boolean isLoaded = ClassLoaderUtils.isLoadedClass(classLoader, "com.example.MyClass");
         System.out.println("Is com.example.MyClass loaded? " + isLoaded);
         
        
         // Example 2: Use the default ClassLoader to check if a class is loaded
         boolean isLoaded = ClassLoaderUtils.isLoadedClass(null, "com.example.SomeClass");
         System.out.println("Is com.example.SomeClass loaded? " + isLoaded);
         
        Parameters:
        classLoader - the ClassLoader to check, may be null
        className - the fully qualified name of the class to check
        Returns:
        true if the class is already loaded; false otherwise
      • findLoadedClass

        @Nullable
        public static java.lang.Class<?> findLoadedClass​(@Nullable
                                                         java.lang.ClassLoader classLoader,
                                                         java.lang.String className)
        Finds a class that has already been loaded by the given ClassLoader or its parent hierarchy.

        If the provided ClassLoader is null, it will use the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Find a loaded class using a specific ClassLoader
         ClassLoader classLoader = MyClass.class.getClassLoader();
         String className = "com.example.MyClass";
         Class<?> loadedClass = ClassLoaderUtils.findLoadedClass(classLoader, className);
         if (loadedClass != null) {
             System.out.println(className + " is already loaded.");
         } else {
             System.out.println(className + " is not loaded yet.");
         }
         
        
         // Example 2: Use the default ClassLoader to find a loaded class
         String className = "com.example.SomeClass";
         Class<?> loadedClass = ClassLoaderUtils.findLoadedClass(null, className);
         if (loadedClass != null) {
             System.out.println(className + " is already loaded.");
         } else {
             System.out.println(className + " is not loaded yet.");
         }
         
        Parameters:
        classLoader - the ClassLoader to check, may be null
        className - the fully qualified name of the class to check
        Returns:
        the Class object if the class is already loaded; null otherwise
      • loadClass

        @Nullable
        public static java.lang.Class<?> loadClass​(@Nullable
                                                   java.lang.ClassLoader classLoader,
                                                   @Nullable
                                                   java.lang.String className)
        Loads the class with the specified name using the provided ClassLoader or the default ClassLoader if none is specified.

        This method attempts to load the class using the given ClassLoader. If the ClassLoader is null, it uses the default one determined by getDefaultClassLoader(). The actual loading is delegated to the ClassLoader.loadClass(String) method.

        Example Usage

        
         // Example 1: Load a class using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Class<?> loadedClass = ClassLoaderUtils.loadClass(customClassLoader, "com.example.MyClass");
         if (loadedClass != null) {
             System.out.println("Class loaded successfully: " + loadedClass.getName());
         } else {
             System.out.println("Failed to load class.");
         }
         
        
         // Example 2: Use the default ClassLoader to load a class
         Class<?> loadedClass = ClassLoaderUtils.loadClass(null, "com.example.AnotherClass");
         if (loadedClass != null) {
             System.out.println("Class loaded successfully: " + loadedClass.getName());
         } else {
             System.out.println("Failed to load class.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for loading the class, may be null
        className - the fully qualified name of the class to load
        Returns:
        the loaded Class object if successful, or null if the class could not be found or loaded
      • loadClass

        @Nullable
        public static java.lang.Class<?> loadClass​(@Nullable
                                                   java.lang.ClassLoader classLoader,
                                                   @Nullable
                                                   java.lang.String className,
                                                   boolean cached)
        Loads the class with the specified name using the provided ClassLoader or the default ClassLoader if none is specified, optionally caching the result for faster subsequent lookups.

        This method attempts to load the class using the given ClassLoader. If the ClassLoader is null, it uses the default one determined by getDefaultClassLoader(). The actual loading is delegated to the ClassLoader.loadClass(String) method.

        Example Usage

        
         // Example 1: Load a class using a specific ClassLoader without caching
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Class<?> loadedClass = ClassLoaderUtils.loadClass(customClassLoader, "com.example.MyClass", false);
         if (loadedClass != null) {
             System.out.println("Class loaded successfully: " + loadedClass.getName());
         } else {
             System.out.println("Failed to load class.");
         }
         
        
         // Example 2: Use the default ClassLoader to load a class with caching enabled
         Class<?> loadedClass = ClassLoaderUtils.loadClass(null, "com.example.AnotherClass", true);
         if (loadedClass != null) {
             System.out.println("Class loaded successfully: " + loadedClass.getName());
         } else {
             System.out.println("Failed to load class.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for loading the class, may be null
        className - the fully qualified name of the class to load
        cached - whether to cache the loaded class for faster subsequent lookups
        Returns:
        the loaded Class object if successful, or null if the class could not be found or loaded
      • doLoadClass

        protected static java.lang.Class<?> doLoadClass​(java.lang.ClassLoader classLoader,
                                                        java.lang.String className)
      • getResources

        @Nonnull
        public static java.util.Set<java.net.URL> getResources​(@Nullable
                                                               java.lang.ClassLoader classLoader,
                                                               @Nonnull
                                                               ClassLoaderUtils.ResourceType resourceType,
                                                               java.lang.String resourceName)
                                                        throws java.lang.NullPointerException,
                                                               java.io.IOException
        Retrieves a set of URLs representing resources with the specified type and name using the provided ClassLoader.

        If the given ClassLoader is null, this method uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Get all URLs for a specific resource using a custom ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         ResourceType resourceType = ResourceType.CLASS;
         String resourceName = "com.example.MyClass.class";
         Set<URL> resourceURLs = ClassLoaderUtils.getResources(customClassLoader, resourceType, resourceName);
         System.out.println("Resource URLs: " + resourceURLs);
         
        
         // Example 2: Use the default ClassLoader to get URLs for a resource
         ResourceType resourceType = ResourceType.PACKAGE;
         String resourceName = "com.example";
         Set<URL> resourceURLs = ClassLoaderUtils.getResources(null, resourceType, resourceName);
         System.out.println("Resource URLs: " + resourceURLs);
         
        Parameters:
        classLoader - the ClassLoader to use for locating resources; may be null
        resourceType - the type of resource to locate (e.g., CLASS, PACKAGE); see ClassLoaderUtils.ResourceType
        resourceName - the name of the resource to locate
        Returns:
        a set of URLs pointing to the resources found; returns an empty set if none are found
        Throws:
        java.lang.NullPointerException - if the resourceName is null
        java.io.IOException - if I/O errors occur while searching for resources
      • getResources

        @Nonnull
        public static java.util.Set<java.net.URL> getResources​(@Nullable
                                                               java.lang.ClassLoader classLoader,
                                                               java.lang.String resourceName)
                                                        throws java.lang.NullPointerException,
                                                               java.io.IOException
        Retrieves a set of URLs representing resources with the specified name using the provided ClassLoader.

        This method attempts to locate resources across all known ClassLoaderUtils.ResourceType categories (e.g., CLASS, PACKAGE). It iterates through each resource type and returns the first non-empty set of URLs it finds. If no matching resources are found for any type, an empty set is returned.

        Example Usage

        
         // Example 1: Get all URLs for a specific resource using a custom ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String resourceName = "com.example.MyClass.class";
         Set<URL> resourceURLs = ClassLoaderUtils.getResources(customClassLoader, resourceName);
         System.out.println("Resource URLs: " + resourceURLs);
         
        
         // Example 2: Use the default ClassLoader to get URLs for a resource
         String resourceName = "com.example";
         Set<URL> resourceURLs = ClassLoaderUtils.getResources(null, resourceName);
         System.out.println("Resource URLs: " + resourceURLs);
         
        Parameters:
        classLoader - the ClassLoader to use for locating resources; may be null
        resourceName - the name of the resource to locate
        Returns:
        a set of URLs pointing to the resources found; returns an empty set if none are found
        Throws:
        java.lang.NullPointerException - if the resourceName is null
        java.io.IOException - if I/O errors occur while searching for resources
      • getResource

        @Nullable
        public static java.net.URL getResource​(java.lang.String resourceName)
                                        throws java.lang.NullPointerException
        Get the resource URL under specified resource name using the default ClassLoader.

        This method attempts to locate a resource with the given name by checking various resource types (e.g., CLASS, PACKAGE) and returns the first matching URL it finds. If no matching resource is found, it returns null.

        Example Usage

        
         // Example 1: Retrieve a resource URL using the default ClassLoader
         URL resourceUrl = ClassLoaderUtils.getResource("com.example.MyClass.class");
         if (resourceUrl != null) {
             System.out.println("Resource found at: " + resourceUrl);
         } else {
             System.out.println("Resource not found.");
         }
         
        Parameters:
        resourceName - the name of the resource to locate
        Returns:
        the resource URL under the specified resource name, or null if not found
        Throws:
        java.lang.NullPointerException - if the resourceName is null
      • getResource

        @Nullable
        public static java.net.URL getResource​(@Nullable
                                               java.lang.ClassLoader classLoader,
                                               java.lang.String resourceName)
                                        throws java.lang.NullPointerException
        Retrieves a resource URL for the specified resource name using the provided ClassLoader.

        This method attempts to locate a resource by checking various resource types (e.g., CLASS, PACKAGE) and returns the first matching URL it finds. If no matching resource is found, it returns null.

        Example Usage

        
         // Example 1: Retrieve a resource URL using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String resourceName = "com.example.MyClass.class";
         URL resourceUrl = ClassLoaderUtils.getResource(customClassLoader, resourceName);
         if (resourceUrl != null) {
             System.out.println("Resource found at: " + resourceUrl);
         } else {
             System.out.println("Resource not found.");
         }
         
        
         // Example 2: Use the default ClassLoader to retrieve a resource URL
         String resourceName = "com.example.SomeResource.txt";
         URL resourceUrl = ClassLoaderUtils.getResource(null, resourceName);
         if (resourceUrl != null) {
             System.out.println("Resource found at: " + resourceUrl);
         } else {
             System.out.println("Resource not found.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for locating resources; may be null
        resourceName - the name of the resource to locate
        Returns:
        the resource URL under the specified resource name, or null if not found
        Throws:
        java.lang.NullPointerException - if the resourceName is null
      • getResource

        @Nullable
        public static java.net.URL getResource​(@Nullable
                                               java.lang.ClassLoader classLoader,
                                               ClassLoaderUtils.ResourceType resourceType,
                                               java.lang.String resourceName)
                                        throws java.lang.NullPointerException
        Retrieves a resource URL for the specified resource name using the provided ClassLoader and resource type.

        This method attempts to locate a resource by resolving the resourceName according to the given ClassLoaderUtils.ResourceType. If the ClassLoader is null, it uses the default ClassLoader determined by getDefaultClassLoader(). The resolved resource name is normalized and searched through the provided ClassLoader's ClassLoader.getResource(String) method.

        Example Usage

        
         // Example 1: Retrieve a class resource using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String className = "com.example.MyClass.class";
         URL resourceUrl = ClassLoaderUtils.getResource(customClassLoader, ResourceType.CLASS, className);
         if (resourceUrl != null) {
             System.out.println("Resource found at: " + resourceUrl);
         } else {
             System.out.println("Resource not found.");
         }
         
        
         // Example 2: Use the default ClassLoader to retrieve a package resource
         String packageName = "com.example";
         URL resourceUrl = ClassLoaderUtils.getResource(null, ResourceType.PACKAGE, packageName);
         if (resourceUrl != null) {
             System.out.println("Resource found at: " + resourceUrl);
         } else {
             System.out.println("Resource not found.");
         }
         
        
         // Example 3: Use the default ClassLoader to retrieve a general resource
         String resourceName = "config.properties";
         URL resourceUrl = ClassLoaderUtils.getResource(null, ResourceType.DEFAULT, resourceName);
         if (resourceUrl != null) {
             System.out.println("Resource found at: " + resourceUrl);
         } else {
             System.out.println("Resource not found.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for locating resources; may be null
        resourceType - the type of resource to locate (e.g., CLASS, PACKAGE); see ClassLoaderUtils.ResourceType
        resourceName - the name of the resource to locate
        Returns:
        the resource URL under the specified resource name and type, or null if not found
        Throws:
        java.lang.NullPointerException - if the resourceName is null
      • getResourceAsString

        @Nullable
        public static java.lang.String getResourceAsString​(java.lang.String resourceName)
                                                    throws java.lang.NullPointerException,
                                                           java.io.IOException
        Gets the content of the specified resource as a String using the default ClassLoader.

        This method attempts to locate the resource using the default ClassLoader determined by getDefaultClassLoader() and reads its content into a String. If the resource cannot be found or read, an exception is thrown.

        Example Usage

        
         try {
             String content = ClassLoaderUtils.getResourceAsString("example.txt");
             System.out.println("Resource content: " + content);
         } catch (IOException e) {
             System.err.println("Failed to read resource: " + e.getMessage());
         }
         
        Parameters:
        resourceName - the name of the resource to load
        Returns:
        the content of the resource as a String
        Throws:
        java.lang.NullPointerException - if the resourceName is null
        java.io.IOException - if an I/O error occurs while reading the resource
      • getResourceAsString

        @Nullable
        public static java.lang.String getResourceAsString​(@Nullable
                                                           java.lang.ClassLoader classLoader,
                                                           java.lang.String resourceName)
                                                    throws java.lang.NullPointerException,
                                                           java.io.IOException
        Gets the content of the specified resource as a String using the provided ClassLoader.

        This method attempts to locate the resource using the given ClassLoader and reads its content into a String. If the resource cannot be found or read, an exception is thrown. If the provided ClassLoader is null, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         try {
             ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
             String content = ClassLoaderUtils.getResourceAsString(customClassLoader, "example.txt");
             System.out.println("Resource content: " + content);
         } catch (IOException e) {
             System.err.println("Failed to read resource: " + e.getMessage());
         }
         
        
         try {
             // Use the default ClassLoader
             String content = ClassLoaderUtils.getResourceAsString(null, "config.properties");
             System.out.println("Configuration content: " + content);
         } catch (IOException e) {
             System.err.println("Failed to read resource: " + e.getMessage());
         }
         
        Parameters:
        classLoader - the ClassLoader to use for locating resources; may be null
        resourceName - the name of the resource to locate
        Returns:
        the content of the resource as a String, or null if the resource could not be found
        Throws:
        java.lang.NullPointerException - if the resourceName is null
        java.io.IOException - if an I/O error occurs while reading the resource
      • getClassResource

        public static java.net.URL getClassResource​(@Nullable
                                                    java.lang.ClassLoader classLoader,
                                                    java.lang.String className)
        Retrieves the resource URL for the class file corresponding to the specified class name.

        This method resolves the resource path based on the fully qualified name of the class, appending the ".class" extension, and locating it through the provided ClassLoader. If the ClassLoader is null, the default ClassLoader determined by getDefaultClassLoader() will be used.

        Example Usage

        
         // Example 1: Get the resource URL using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String className = "com.example.MyClass";
         URL classResourceUrl = ClassLoaderUtils.getClassResource(customClassLoader, className);
         if (classResourceUrl != null) {
             System.out.println("Found class resource at: " + classResourceUrl);
         } else {
             System.out.println("Class resource not found.");
         }
         
        
         // Example 2: Use the default ClassLoader to get the class resource
         String className = "com.example.AnotherClass";
         URL classResourceUrl = ClassLoaderUtils.getClassResource(null, className);
         if (classResourceUrl != null) {
             System.out.println("Found class resource at: " + classResourceUrl);
         } else {
             System.out.println("Class resource not found.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for locating the class resource; may be null
        className - the fully qualified name of the class for which the resource URL is to be retrieved
        Returns:
        the URL to the class file resource, or null if not found
        Throws:
        java.lang.NullPointerException - if the className is null
      • getClassResource

        @Nullable
        public static java.net.URL getClassResource​(java.lang.Class<?> type)
        Returns the resource URL for the class file corresponding to the specified class.

        This method uses the class loader of the given class to locate its corresponding class file resource. It constructs the resource path based on the fully qualified name of the class, appending the ".class" extension, and resolving it according to the rules defined by the underlying class loader.

        Example Usage

        
         // Example: Get the resource URL for a specific class
         Class<?> myClass = MyClass.class;
         URL classResourceUrl = ClassLoaderUtils.getClassResource(myClass);
         if (classResourceUrl != null) {
             System.out.println("Found class resource at: " + classResourceUrl);
         } else {
             System.out.println("Class resource not found.");
         }
         
        Parameters:
        type - the class for which the resource URL is to be retrieved
        Returns:
        the URL to the class file resource, or null if not found
        Throws:
        java.lang.NullPointerException - if the provided class is null
      • getClassResource

        @Nullable
        public static java.net.URL getClassResource​(@Nullable
                                                    java.lang.ClassLoader classLoader,
                                                    java.lang.Class<?> type)
        Get the Class resource URL under specified Class

        This method constructs the resource path based on the fully qualified name of the class, appending the ".class" extension, and resolving it according to the rules defined by the underlying class loader. If the provided ClassLoader is null, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Get the resource URL using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Class<?> myClass = MyClass.class;
         URL classResourceUrl = ClassLoaderUtils.getClassResource(customClassLoader, myClass);
         if (classResourceUrl != null) {
             System.out.println("Found class resource at: " + classResourceUrl);
         } else {
             System.out.println("Class resource not found.");
         }
         
        
         // Example 2: Use the default ClassLoader to get the class resource
         Class<?> anotherClass = AnotherClass.class;
         URL classResourceUrl = ClassLoaderUtils.getClassResource(null, anotherClass);
         if (classResourceUrl != null) {
             System.out.println("Found class resource at: " + classResourceUrl);
         } else {
             System.out.println("Class resource not found.");
         }
         
        Parameters:
        classLoader - ClassLoader
        type - type
        Returns:
        the resource URL under specified resource name and type
        Throws:
        java.lang.NullPointerException - If type argument is null
      • getInheritableClassLoaders

        @Nonnull
        public static java.util.Set<java.lang.ClassLoader> getInheritableClassLoaders​(@Nullable
                                                                                      java.lang.ClassLoader classLoader)
                                                                               throws java.lang.NullPointerException
        Retrieves a set of inheritable ClassLoader instances starting from the specified ClassLoader, including its parent hierarchy. This method ensures that all ClassLoaders in the chain are included.

        If the provided ClassLoader is null, the default ClassLoader determined by getDefaultClassLoader() will be used as the starting point.

        Example Usage

        
         // Example 1: Get inheritable ClassLoaders from a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Set<ClassLoader> inheritableLoaders = ClassLoaderUtils.getInheritableClassLoaders(customClassLoader);
         System.out.println("Inheritable ClassLoaders: " + inheritableLoaders);
         
        
         // Example 2: Use the default ClassLoader to get inheritable ClassLoaders
         Set<ClassLoader> inheritableLoaders = ClassLoaderUtils.getInheritableClassLoaders(null);
         System.out.println("Inheritable ClassLoaders: " + inheritableLoaders);
         
        Parameters:
        classLoader - the starting ClassLoader to retrieve inheritable ClassLoaders from; may be null
        Returns:
        an unmodifiable set of ClassLoader instances representing the inheritance chain
        Throws:
        java.lang.NullPointerException - if the resolved ClassLoader (from parameter or default) is null
      • getAllLoadedClassesMap

        @Nonnull
        public static java.util.Map<java.lang.ClassLoader,​java.util.Set<java.lang.Class<?>>> getAllLoadedClassesMap​(@Nullable
                                                                                                                          java.lang.ClassLoader classLoader)
                                                                                                                   throws java.lang.UnsupportedOperationException
        Retrieves a map of ClassLoader instances to their corresponding sets of loaded classes. This method traverses the inheritance hierarchy of the provided ClassLoader (or uses the default ClassLoader if none is specified) and collects all classes loaded by each ClassLoader in the hierarchy.

        If the provided ClassLoader is null, this method uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Get loaded classes map using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Map<ClassLoader, Set<Class<?>>> loadedClassesMap = ClassLoaderUtils.getAllLoadedClassesMap(customClassLoader);
         for (Map.Entry<ClassLoader, Set<Class<?>>> entry : loadedClassesMap.entrySet()) {
             System.out.println("ClassLoader: " + entry.getKey());
             System.out.println("Loaded Classes: " + entry.getValue());
         }
         
        
         // Example 2: Use the default ClassLoader to get the loaded classes map
         Map<ClassLoader, Set<Class<?>>> loadedClassesMap = ClassLoaderUtils.getAllLoadedClassesMap(null);
         for (Map.Entry<ClassLoader, Set<Class<?>>> entry : loadedClassesMap.entrySet()) {
             System.out.println("ClassLoader: " + entry.getKey());
             System.out.println("Loaded Classes: " + entry.getValue());
         }
         
        Parameters:
        classLoader - the starting ClassLoader to retrieve loaded classes from; may be null
        Returns:
        an unmodifiable map where keys are ClassLoader instances and values are sets of loaded classes
        Throws:
        java.lang.UnsupportedOperationException - if the JVM does not support introspection of loaded classes
        java.lang.NullPointerException - if the resolved ClassLoader (from parameter or default) is null
      • getAllLoadedClasses

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> getAllLoadedClasses​(@Nullable
                                                                            java.lang.ClassLoader classLoader)
                                                                     throws java.lang.UnsupportedOperationException
        Retrieves a set of all classes that have been loaded by the specified ClassLoader and its parent hierarchy.

        This method aggregates all classes loaded by each ClassLoader in the inheritance chain starting from the provided ClassLoader. If the provided ClassLoader is null, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Get all loaded classes using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Set<Class<?>> loadedClasses = ClassLoaderUtils.getAllLoadedClasses(customClassLoader);
         System.out.println("Loaded Classes: " + loadedClasses);
         
        
         // Example 2: Use the default ClassLoader to get all loaded classes
         Set<Class<?>> loadedClasses = ClassLoaderUtils.getAllLoadedClasses(null);
         System.out.println("Loaded Classes: " + loadedClasses);
         
        Parameters:
        classLoader - the ClassLoader to start retrieving loaded classes from; may be null
        Returns:
        an unmodifiable set containing all classes loaded by the specified ClassLoader and its parents
        Throws:
        java.lang.UnsupportedOperationException - if the JVM does not support introspection of loaded classes
        See Also:
        getAllLoadedClassesMap(ClassLoader)
      • getLoadedClasses

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> getLoadedClasses​(@Nullable
                                                                         java.lang.ClassLoader classLoader)
                                                                  throws java.lang.UnsupportedOperationException
        Retrieves the set of classes that have been loaded by the specified ClassLoader.

        This method accesses the internal list of loaded classes maintained by the ClassLoader. Note that not all JVM implementations expose this list, and in such cases, an UnsupportedOperationException may be thrown.

        Example Usage

        
         // Example 1: Get loaded classes from a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Set<Class<?>> loadedClasses = ClassLoaderUtils.getLoadedClasses(customClassLoader);
         System.out.println("Loaded Classes: " + loadedClasses);
         
        
         // Example 2: Use the default ClassLoader to get loaded classes
         Set<Class<?>> loadedClasses = ClassLoaderUtils.getLoadedClasses(null);
         System.out.println("Loaded Classes: " + loadedClasses);
         
        Parameters:
        classLoader - the ClassLoader to retrieve loaded classes from; may be null
        Returns:
        a read-only set containing the classes loaded by the specified ClassLoader
        Throws:
        java.lang.UnsupportedOperationException - if the JVM does not support introspection of loaded classes
        java.lang.NullPointerException - if the resolved ClassLoader (from parameter or default) is null
      • findLoadedClassesInClassPath

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(@Nullable
                                                                                     java.lang.ClassLoader classLoader)
                                                                              throws java.lang.UnsupportedOperationException
        Retrieves a set of classes that have been loaded from the class path using the specified ClassLoader or the default one.

        This method finds all class names present in the class paths and then attempts to locate and return the corresponding loaded classes. If no ClassLoader is provided, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Find loaded classes using the default ClassLoader
         try {
             Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClassesInClassPath(null);
             System.out.println("Loaded classes from class path: " + loadedClasses);
         } catch (UnsupportedOperationException e) {
             System.err.println("Operation not supported by the current JVM.");
         }
         
        
         // Example 2: Find loaded classes using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         try {
             Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClassesInClassPath(customClassLoader);
             System.out.println("Loaded classes from class path: " + loadedClasses);
         } catch (UnsupportedOperationException e) {
             System.err.println("Operation not supported by the current JVM.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for finding loaded classes; may be null
        Returns:
        an unmodifiable set of classes loaded from the class path
        Throws:
        java.lang.UnsupportedOperationException - if the JVM does not support introspection of loaded classes
      • findLoadedClassesInClassPaths

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPaths​(@Nullable
                                                                                      java.lang.ClassLoader classLoader,
                                                                                      java.util.Set<java.lang.String> classPaths)
                                                                               throws java.lang.UnsupportedOperationException
        Finds and returns a set of classes that have been loaded from the specified class paths using the provided ClassLoader.

        This method iterates through each class path in the given set and attempts to locate and load the corresponding classes using the specified ClassLoader. If a class is found, it is added to the resulting set of loaded classes.

        Example Usage

        
         // Example 1: Find loaded classes from specific class paths using a custom ClassLoader
         Set<String> classPaths = new HashSet<>(Arrays.asList("path/to/classes", "lib/dependency.jar"));
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         try {
             Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClassesInClassPaths(customClassLoader, classPaths);
             System.out.println("Loaded Classes: " + loadedClasses);
         } catch (UnsupportedOperationException e) {
             System.err.println("Operation not supported by the current JVM.");
         }
         
        
         // Example 2: Use the default ClassLoader to find loaded classes from specific class paths
         Set<String> classPaths = new HashSet<>(Arrays.asList("com/example/app", "libs/utils.jar"));
         try {
             Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClassesInClassPaths(null, classPaths);
             System.out.println("Loaded Classes: " + loadedClasses);
         } catch (UnsupportedOperationException e) {
             System.err.println("Operation not supported by the current JVM.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for finding loaded classes; may be null
        classPaths - the set of class paths (directories or JAR files) to search for classes
        Returns:
        an unmodifiable set containing all classes loaded from the specified class paths
        Throws:
        java.lang.UnsupportedOperationException - if the JVM does not support introspection of loaded classes
      • findLoadedClassesInClassPath

        @Nonnull
        public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath​(@Nullable
                                                                                     java.lang.ClassLoader classLoader,
                                                                                     java.lang.String classPath)
                                                                              throws java.lang.UnsupportedOperationException
        Retrieves a set of classes that have been loaded from the specified class path using the provided ClassLoader.

        This method finds all class names present in the given class path and attempts to locate and return the corresponding loaded classes using the specified ClassLoader. If no ClassLoader is provided, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Find loaded classes from a specific class path using a custom ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String classPath = "path/to/classes";
         try {
             Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClassesInClassPath(customClassLoader, classPath);
             System.out.println("Loaded Classes: " + loadedClasses);
         } catch (UnsupportedOperationException e) {
             System.err.println("Operation not supported by the current JVM.");
         }
         
        
         // Example 2: Use the default ClassLoader to find loaded classes from a class path
         String classPath = "com/example/app";
         try {
             Set<Class<?>> loadedClasses = ClassLoaderUtils.findLoadedClassesInClassPath(null, classPath);
             System.out.println("Loaded Classes: " + loadedClasses);
         } catch (UnsupportedOperationException e) {
             System.err.println("Operation not supported by the current JVM.");
         }
         
        Parameters:
        classLoader - the ClassLoader to use for finding loaded classes; may be null
        classPath - the class path (directory or JAR file) to search for classes
        Returns:
        an unmodifiable set containing all classes loaded from the specified class path
        Throws:
        java.lang.UnsupportedOperationException - if the JVM does not support introspection of loaded classes
      • isPresent

        public static boolean isPresent​(@Nullable
                                        java.lang.String className)
        Checks if a class with the specified name is present in the default ClassLoader.

        This method uses the default ClassLoader determined by getDefaultClassLoader() to check whether the class exists and can be resolved. If the class name is blank or resolution fails, it returns false.

        Example Usage

        
         boolean present = ClassLoaderUtils.isPresent("java.lang.String");
         System.out.println("Is java.lang.String present? " + present);
         
        Parameters:
        className - the fully qualified name of the class to check
        Returns:
        true if the class is present and can be resolved; false otherwise
      • isPresent

        public static boolean isPresent​(@Nullable
                                        java.lang.String className,
                                        @Nullable
                                        java.lang.ClassLoader classLoader)
        Checks if a class with the specified name is present in the given ClassLoader or the default ClassLoader.

        This method attempts to resolve the class using the provided ClassLoader. If the ClassLoader is null, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Check if a class is present using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         boolean present = ClassLoaderUtils.isPresent("com.example.MyClass", customClassLoader);
         System.out.println("Is com.example.MyClass present? " + present);
         
        
         // Example 2: Use the default ClassLoader to check if a class is present
         boolean present = ClassLoaderUtils.isPresent("java.lang.String", null);
         System.out.println("Is java.lang.String present? " + present);
         
        Parameters:
        className - the fully qualified name of the class to check
        classLoader - the ClassLoader to use for checking presence, may be null
        Returns:
        true if the class is present and can be resolved; false otherwise
      • resolveClass

        @Nullable
        public static java.lang.Class<?> resolveClass​(@Nullable
                                                      java.lang.String className)
        Resolves a class by its name using the default ClassLoader.

        This method attempts to load and return the Class object for the specified class name. If the class cannot be found or loaded, it returns null. The default ClassLoader is determined by getDefaultClassLoader().

        Example Usage

        
         // Example: Resolve a class using the default ClassLoader
         String className = "java.lang.String";
         Class<?> resolvedClass = ClassLoaderUtils.resolveClass(className);
         if (resolvedClass != null) {
             System.out.println("Class resolved: " + resolvedClass.getName());
         } else {
             System.out.println("Class not found or could not be loaded.");
         }
         
        Parameters:
        className - the fully qualified name of the class to resolve
        Returns:
        the resolved Class object if successful; null if the class cannot be found or loaded
      • resolveClass

        @Nullable
        public static java.lang.Class<?> resolveClass​(@Nullable
                                                      java.lang.String className,
                                                      @Nullable
                                                      java.lang.ClassLoader classLoader)
        Resolves a class by its name using the provided ClassLoader or the default ClassLoader if none is specified.

        This method attempts to load and return the Class object for the specified class name. If the class cannot be found or loaded, it returns null. The actual loading is delegated to the loadClass(ClassLoader, String, boolean) method with caching disabled by default.

        Example Usage

        
         // Example 1: Resolve a class using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String className = "com.example.MyClass";
         Class<?> resolvedClass = ClassLoaderUtils.resolveClass(className, customClassLoader);
         if (resolvedClass != null) {
             System.out.println("Class resolved: " + resolvedClass.getName());
         } else {
             System.out.println("Class not found or could not be loaded.");
         }
         
        
         // Example 2: Use the default ClassLoader to resolve a class
         String className = "java.lang.String";
         Class<?> resolvedClass = ClassLoaderUtils.resolveClass(className, null);
         if (resolvedClass != null) {
             System.out.println("Class resolved: " + resolvedClass.getName());
         } else {
             System.out.println("Class not found or could not be loaded.");
         }
         
        Parameters:
        className - the fully qualified name of the class to resolve; may be blank or null
        classLoader - the ClassLoader to use for resolving the class; may be null
        Returns:
        the resolved Class object if successful; null if the class cannot be found or loaded
      • resolveClass

        @Nullable
        public static java.lang.Class<?> resolveClass​(@Nullable
                                                      java.lang.String className,
                                                      @Nullable
                                                      java.lang.ClassLoader classLoader,
                                                      boolean cached)
        Resolves a class by its name using the provided ClassLoader or the default ClassLoader if none is specified, optionally caching the result for faster subsequent lookups.

        This method attempts to load and return the Class object for the specified class name. If the class cannot be found or loaded, it returns null. The actual loading is delegated to the loadClass(ClassLoader, String, boolean) method, with caching behavior controlled by the cached parameter.

        Example Usage

        
         // Example 1: Resolve a class using a specific ClassLoader without caching
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         String className = "com.example.MyClass";
         Class<?> resolvedClass = ClassLoaderUtils.resolveClass(className, customClassLoader, false);
         if (resolvedClass != null) {
             System.out.println("Class resolved: " + resolvedClass.getName());
         } else {
             System.out.println("Class not found or could not be loaded.");
         }
         
        
         // Example 2: Use the default ClassLoader to resolve a class with caching enabled
         String className = "java.lang.String";
         Class<?> resolvedClass = ClassLoaderUtils.resolveClass(className, null, true);
         if (resolvedClass != null) {
             System.out.println("Class resolved: " + resolvedClass.getName());
         } else {
             System.out.println("Class not found or could not be loaded.");
         }
         
        Parameters:
        className - the fully qualified name of the class to resolve; may be blank or null
        classLoader - the ClassLoader to use for resolving the class; may be null
        cached - whether to cache the loaded class for faster subsequent lookups
        Returns:
        the resolved Class object if successful; null if the class cannot be found or loaded
      • findAllClassPathURLs

        @Nonnull
        public static java.util.Set<java.net.URL> findAllClassPathURLs​(@Nullable
                                                                       java.lang.ClassLoader classLoader)
        Retrieves all URLs from the class path associated with the specified ClassLoader and its parent hierarchy.

        This method aggregates the URLs from the class paths across the entire inheritance chain of the provided ClassLoader. If the provided ClassLoader is null, it uses the default ClassLoader determined by getDefaultClassLoader().

        Example Usage

        
         // Example 1: Get all class path URLs using a specific ClassLoader
         ClassLoader customClassLoader = MyCustomClassLoader.getInstance();
         Set<URL> classPathURLs = ClassLoaderUtils.findAllClassPathURLs(customClassLoader);
         System.out.println("Class-Path URLs: " + classPathURLs);
         
        
         // Example 2: Use the default ClassLoader to get all class path URLs
         Set<URL> classPathURLs = ClassLoaderUtils.findAllClassPathURLs(null);
         System.out.println("Class-Path URLs: " + classPathURLs);
         
        Parameters:
        classLoader - the ClassLoader to retrieve class path URLs from; may be null
        Returns:
        a set of URLs representing the class paths in the inheritance hierarchy of the ClassLoader
      • removeClassPathURL

        public static boolean removeClassPathURL​(@Nullable
                                                 java.lang.ClassLoader classLoader,
                                                 java.net.URL url)
        Removes the specified URL from the class path of the given ClassLoader if it supports such operation.

        This method checks whether the provided ClassLoader is an instance of SecureClassLoader or a compatible subclass, as only those are expected to support dynamic modification of the class path. If it is, the method delegates the removal operation to the configured URLClassPathHandle instance (urlClassPathHandle).

        Example Usage

        
         // Example 1: Remove a URL from a URLClassLoader
         URL urlToRemove = new URL("file:/path/to/removed.jar");
         URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{urlToRemove});
        
         boolean removed = ClassLoaderUtils.removeClassPathURL(urlClassLoader, urlToRemove);
         System.out.println("Was the URL removed? " + removed);
         
        
         // Example 2: Attempt to remove a URL from a non-SecureClassLoader
         ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
         URL urlToRemove = new URL("file:/some/path.jar");
        
         boolean removed = ClassLoaderUtils.removeClassPathURL(systemClassLoader, urlToRemove);
         System.out.println("Was the URL removed? " + removed); // Likely false
         
        Parameters:
        classLoader - the ClassLoader from which the URL should be removed; may be null
        url - the URL to remove from the class path
        Returns:
        true if the URL was successfully removed; false otherwise
      • findURLClassLoader

        @Nullable
        public static java.net.URLClassLoader findURLClassLoader​(@Nullable
                                                                 java.lang.ClassLoader classLoader)
        Attempts to find and return the first URLClassLoader in the hierarchy starting from the provided ClassLoader.

        If the given ClassLoader is an instance of URLClassLoader, it is returned directly. Otherwise, this method traverses up the parent hierarchy to locate a URLClassLoader. If none is found, it returns null.

        Example Usage

        
         // Example 1: Find URLClassLoader from a known URLClassLoader instance
         URLClassLoader urlClassLoader = (URLClassLoader) MyClass.class.getClassLoader();
         URLClassLoader result = ClassLoaderUtils.findURLClassLoader(urlClassLoader);
         System.out.println("Found URLClassLoader: " + result);
         
        
         // Example 2: Attempt to find URLClassLoader from a non-URLClassLoader
         ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
         URLClassLoader result = ClassLoaderUtils.findURLClassLoader(systemClassLoader);
         if (result != null) {
             System.out.println("Found URLClassLoader: " + result);
         } else {
             System.out.println("No URLClassLoader found in the hierarchy.");
         }
         
        Parameters:
        classLoader - the ClassLoader to start searching from; may be null
        Returns:
        the first URLClassLoader found in the hierarchy, or null if none exists
      • newURLClassLoader

        @Nonnull
        public static java.net.URLClassLoader newURLClassLoader​(@Nonnull
                                                                java.lang.Iterable<java.net.URL> urls)
        Creates a new instance of URLClassLoader using the URLs provided in an Iterable.

        This method ensures that the resulting class loader has access to all the URLs specified. If the iterable is empty or contains null elements, an exception will be thrown.

        Example Usage

        
         // Example 1: Create a URLClassLoader from a list of URLs
         List<URL> urls = Arrays.asList(new URL("file:/path/to/jar1.jar"), new URL("file:/path/to/jar2.jar"));
         URLClassLoader classLoader = ClassLoaderUtils.newURLClassLoader(urls);
         System.out.println("ClassLoader created successfully.");
         
        Parameters:
        urls - the URLs from which to create the class loader; must not be null and must not contain null elements
        Returns:
        a non-null instance of URLClassLoader
        Throws:
        java.lang.IllegalArgumentException - if the 'urls' is null or contains null elements
      • newURLClassLoader

        public static java.net.URLClassLoader newURLClassLoader​(@Nonnull
                                                                java.lang.Iterable<java.net.URL> urls,
                                                                @Nullable
                                                                java.lang.ClassLoader classLoader)
        Creates a new instance of URLClassLoader using the URLs provided in an Iterable.

        This method ensures that the resulting class loader has access to all the URLs specified. If the iterable is empty or contains null elements, an exception will be thrown.

        Example Usage

        
         // Example 1: Create a URLClassLoader from a list of URLs
         List<URL> urls = Arrays.asList(new URL("file:/path/to/jar1.jar"), new URL("file:/path/to/jar2.jar"));
         URLClassLoader classLoader = ClassLoaderUtils.newURLClassLoader(urls, parentClassLoader);
         System.out.println("ClassLoader created successfully.");
         
        
         // Example 2: Handle empty URLs list gracefully with try-catch
         try {
             URLClassLoader classLoader = ClassLoaderUtils.newURLClassLoader(Collections.emptyList(), null);
         } catch (IllegalArgumentException e) {
             System.err.println("Failed to create URLClassLoader: " + e.getMessage());
         }
         
        Parameters:
        urls - the URLs from which to create the class loader; must not be null and must not contain null elements
        classLoader - the parent class loader for delegation, may be null
        Returns:
        a non-null instance of URLClassLoader
        Throws:
        java.lang.IllegalArgumentException - if the 'urls' is null or contains null elements
      • newURLClassLoader

        @Nonnull
        public static java.net.URLClassLoader newURLClassLoader​(@Nonnull
                                                                java.net.URL[] urls)
        Create a new instance of URLClassLoader
        Parameters:
        urls - the urls
        Returns:
        non-null URLClassLoader
        Throws:
        java.lang.IllegalArgumentException - if the urls is null or contains null element
      • newURLClassLoader

        @Nonnull
        public static java.net.URLClassLoader newURLClassLoader​(@Nonnull
                                                                java.net.URL[] urls,
                                                                boolean initializedLoaders)
        Create a new instance of URLClassLoader
        Parameters:
        urls - the urls
        initializedLoaders - the loaders of URLClassPath will be initialized or not
        Returns:
        non-null URLClassLoader
        Throws:
        java.lang.IllegalArgumentException - if the urls is null or contains null element
      • newURLClassLoader

        @Nonnull
        public static java.net.URLClassLoader newURLClassLoader​(@Nonnull
                                                                java.net.URL[] urls,
                                                                @Nullable
                                                                java.lang.ClassLoader parent)
                                                         throws java.lang.IllegalArgumentException
        Creates a new instance of URLClassLoader using the provided array of URLs and an optional parent ClassLoader.

        This method ensures that the resulting class loader has access to all the URLs specified. If the 'urls' array is null or contains null elements, an IllegalArgumentException will be thrown.

        Example Usage

        
         // Example 1: Create a URLClassLoader with a custom parent ClassLoader
         URL[] urls = { new URL("file:/path/to/library.jar") };
         ClassLoader parentClassLoader = MyClass.class.getClassLoader();
         URLClassLoader urlClassLoader = ClassLoaderUtils.newURLClassLoader(urls, parentClassLoader);
         System.out.println("ClassLoader created successfully.");
         
        
         // Example 2: Create a URLClassLoader without a parent ClassLoader (uses system ClassLoader as default)
         URL[] urls = { new URL("file:/path/to/another-library.jar") };
         URLClassLoader urlClassLoader = ClassLoaderUtils.newURLClassLoader(urls, null);
         System.out.println("ClassLoader created successfully without a parent.");
         
        
         // Example 3: Handle invalid input gracefully with try-catch
         try {
             URLClassLoader classLoader = ClassLoaderUtils.newURLClassLoader(null, null);
         } catch (IllegalArgumentException e) {
             System.err.println("Caught expected exception: " + e.getMessage());
         }
         
        Parameters:
        urls - the URLs from which to create the class loader; must not be null and must not contain null elements
        parent - the parent class loader for delegation, may be null
        Returns:
        a non-null instance of URLClassLoader
        Throws:
        java.lang.IllegalArgumentException - if the 'urls' is null or contains null elements
      • newURLClassLoader

        @Nonnull
        public static java.net.URLClassLoader newURLClassLoader​(@Nonnull
                                                                java.net.URL[] urls,
                                                                @Nullable
                                                                java.lang.ClassLoader parent,
                                                                boolean initializedLoaders)
                                                         throws java.lang.IllegalArgumentException
        Create a new instance of URLClassLoader
        Parameters:
        urls - the urls
        parent - the ClassLoader as parent
        initializedLoaders - the loaders of URLClassPath will be initialized or not
        Returns:
        non-null URLClassLoader
        Throws:
        java.lang.IllegalArgumentException - if the urls is null or contains null element
      • resolveURLClassLoader

        @Nonnull
        public static java.net.URLClassLoader resolveURLClassLoader​(@Nullable
                                                                    java.lang.ClassLoader classLoader)
        Resolves or creates a URLClassLoader instance from the specified ClassLoader.

        If the provided ClassLoader is already an instance of URLClassLoader, it is returned directly. If not, this method attempts to find a URLClassLoader in the parent hierarchy. If none is found, a new URLClassLoader is created using all URLs from the class path associated with the given ClassLoader.

        Example Usage

        
         // Example 1: Resolve an existing URLClassLoader
         ClassLoader classLoader = MyClass.class.getClassLoader();
         URLClassLoader resolvedLoader = ClassLoaderUtils.resolveURLClassLoader(classLoader);
         System.out.println("Resolved URLClassLoader: " + resolvedLoader);
         
        
         // Example 2: Create a new URLClassLoader when none exists in the hierarchy
         ClassLoader customClassLoader = new MyCustomClassLoader();
         URLClassLoader resolvedLoader = ClassLoaderUtils.resolveURLClassLoader(customClassLoader);
         System.out.println("Created URLClassLoader with URLs: " + Arrays.toString(resolvedLoader.getURLs()));
         
        Parameters:
        classLoader - the ClassLoader to resolve into a URLClassLoader; may be null
        Returns:
        a non-null URLClassLoader instance derived from or wrapping the provided ClassLoader
        Throws:
        java.lang.IllegalArgumentException - if any error occurs during creation of the URLClassLoader