Class ConcurrentClassLoader

  • Direct Known Subclasses:
    ModuleClassLoader

    public abstract class ConcurrentClassLoader
    extends NamedClassLoader
    A classloader which can delegate to multiple other classloaders without risk of deadlock. A concurrent class loader should only ever be delegated to by another concurrent class loader; however a concurrent class loader may delegate to a standard hierarchical class loader. In other words, holding a lock on another class loader while invoking a method on this class loader may cause an unexpected deadlock.
    Author:
    David M. Lloyd
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.util.Enumeration<java.net.URL> EMPTY_ENUMERATION
      Deprecated.
      Use Collections.emptyEnumeration() instead.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ConcurrentClassLoader()
      Construct a new instance, using our class loader as the parent.
      protected ConcurrentClassLoader​(java.lang.String name)
      Construct a new instance, using our class loader as the parent.
      protected ConcurrentClassLoader​(ConcurrentClassLoader parent)
      Construct a new instance with the given parent class loader, which must be a concurrent class loader, or null to create a root concurrent class loader.
      protected ConcurrentClassLoader​(ConcurrentClassLoader parent, java.lang.String name)
      Construct a new instance with the given parent class loader, which must be a concurrent class loader, or null to create a root concurrent class loader.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      protected java.lang.Class<?> defineOrLoadClass​(java.lang.String className, byte[] bytes, int off, int len)
      Atomically define or load the named class.
      protected java.lang.Class<?> defineOrLoadClass​(java.lang.String className, byte[] bytes, int off, int len, java.security.ProtectionDomain protectionDomain)
      Atomically define or load the named class.
      protected java.lang.Package definePackage​(java.lang.String name, java.lang.String specTitle, java.lang.String specVersion, java.lang.String specVendor, java.lang.String implTitle, java.lang.String implVersion, java.lang.String implVendor, java.net.URL sealBase)
      Defines a package by name in this ConcurrentClassLoader.
      protected java.lang.Class<?> findClass​(java.lang.String className)
      Implementation of ClassLoader.findClass(String).
      protected java.lang.Class<?> findClass​(java.lang.String className, boolean exportsOnly, boolean resolve)
      Find a class, possibly delegating to other loader(s).
      protected java.lang.Class<?> findClass​(java.lang.String moduleName, java.lang.String className)
      Implementation of ClassLoader.findClass(String, String).
      protected java.lang.Package findLoadedPackage​(java.lang.String name)
      Load a package from this class loader only.
      protected java.net.URL findResource​(java.lang.String name)
      Never used.
      protected java.net.URL findResource​(java.lang.String name, boolean exportsOnly)
      Find the resource with the given name and exported status.
      protected java.net.URL findResource​(java.lang.String moduleName, java.lang.String name)
      Find the resource with the given name in specified java module.
      protected java.io.InputStream findResourceAsStream​(java.lang.String name, boolean exportsOnly)
      Finds the resource with the given name and exported status, returning the resource content as a stream.
      protected java.util.Enumeration<java.net.URL> findResources​(java.lang.String name)
      Never used.
      protected java.util.Enumeration<java.net.URL> findResources​(java.lang.String name, boolean exportsOnly)
      Finds the resources with the given name and exported status.
      protected java.lang.Package getPackage​(java.lang.String name)
      Deprecated.
      Deprecated in ClassLoader.getPackage(String).
      protected java.lang.Package getPackageByName​(java.lang.String name)
      Perform the actual work to load a package which is visible to this class loader.
      protected java.lang.Package[] getPackages()
      Get all defined packages which are visible to this class loader.
      java.net.URL getResource​(java.lang.String name)
      Finds the resource with the given name.
      java.io.InputStream getResourceAsStream​(java.lang.String name)
      Returns an input stream for reading the specified resource.
      java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)
      Finds all available resources with the given name.
      java.lang.Class<?> loadClass​(java.lang.String className)
      Loads the class with the specified binary name.
      java.lang.Class<?> loadClass​(java.lang.String className, boolean resolve)
      Loads the class with the specified binary name.
      java.lang.Class<?> loadExportedClass​(java.lang.String className)
      Same as loadClass(String), except only exported classes will be considered.
      java.lang.Class<?> loadExportedClass​(java.lang.String className, boolean resolve)
      Same as loadClass(String,boolean), except only exported classes will be considered.
      • Methods inherited from class java.lang.ClassLoader

        clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
      • Methods inherited from class java.lang.Object

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

      • EMPTY_ENUMERATION

        @Deprecated
        protected static final java.util.Enumeration<java.net.URL> EMPTY_ENUMERATION
        Deprecated.
        Use Collections.emptyEnumeration() instead.
        An empty enumeration, for subclasses to use if desired.
    • Constructor Detail

      • ConcurrentClassLoader

        protected ConcurrentClassLoader​(ConcurrentClassLoader parent)
        Construct a new instance with the given parent class loader, which must be a concurrent class loader, or null to create a root concurrent class loader.
        Parameters:
        parent - the parent class loader
      • ConcurrentClassLoader

        protected ConcurrentClassLoader()
        Construct a new instance, using our class loader as the parent.
      • ConcurrentClassLoader

        protected ConcurrentClassLoader​(ConcurrentClassLoader parent,
                                        java.lang.String name)
        Construct a new instance with the given parent class loader, which must be a concurrent class loader, or null to create a root concurrent class loader.
        Parameters:
        parent - the parent class loader
        name - the name of this class loader, or null if it is unnamed
      • ConcurrentClassLoader

        protected ConcurrentClassLoader​(java.lang.String name)
        Construct a new instance, using our class loader as the parent.
        Parameters:
        name - the name of this class loader, or null if it is unnamed
    • Method Detail

      • loadClass

        public final java.lang.Class<?> loadClass​(java.lang.String className)
                                           throws java.lang.ClassNotFoundException
        Loads the class with the specified binary name. Equivalent to calling loadClass(className, false).
        Overrides:
        loadClass in class java.lang.ClassLoader
        Parameters:
        className - The binary name of the class
        Returns:
        the resulting Class instance
        Throws:
        java.lang.ClassNotFoundException - if the class was not found
      • loadClass

        public final java.lang.Class<?> loadClass​(java.lang.String className,
                                                  boolean resolve)
                                           throws java.lang.ClassNotFoundException
        Loads the class with the specified binary name.
        Overrides:
        loadClass in class java.lang.ClassLoader
        Parameters:
        className - The binary name of the class
        resolve - true if the class should be linked after loading
        Returns:
        the resulting Class instance
        Throws:
        java.lang.ClassNotFoundException
      • loadExportedClass

        public final java.lang.Class<?> loadExportedClass​(java.lang.String className)
                                                   throws java.lang.ClassNotFoundException
        Same as loadClass(String), except only exported classes will be considered.
        Parameters:
        className - the class name
        Returns:
        the class
        Throws:
        java.lang.ClassNotFoundException - if the class isn't found
      • loadExportedClass

        public final java.lang.Class<?> loadExportedClass​(java.lang.String className,
                                                          boolean resolve)
                                                   throws java.lang.ClassNotFoundException
        Same as loadClass(String,boolean), except only exported classes will be considered.
        Parameters:
        className - the class name
        resolve - true if the class should be linked after loading
        Returns:
        the class
        Throws:
        java.lang.ClassNotFoundException - if the class isn't found
      • findClass

        protected java.lang.Class<?> findClass​(java.lang.String className,
                                               boolean exportsOnly,
                                               boolean resolve)
                                        throws java.lang.ClassNotFoundException
        Find a class, possibly delegating to other loader(s). This method should never synchronize across a delegation method call of any sort. The default implementation always throws ClassNotFoundException.

        If a class is to be defined by this method, it should be done via one of the atomic defineOrLoadClass methods rather than defineClass() in order to avoid spurious exceptions.

        Parameters:
        className - the class name
        exportsOnly - true if only exported classes should be considered
        resolve - true if the class should be linked after loading
        Returns:
        the class
        Throws:
        java.lang.ClassNotFoundException - if the class is not found
      • defineOrLoadClass

        protected final java.lang.Class<?> defineOrLoadClass​(java.lang.String className,
                                                             byte[] bytes,
                                                             int off,
                                                             int len)
        Atomically define or load the named class. If the class is already defined, the existing class is returned.
        Parameters:
        className - the class name to define or load
        bytes - the bytes to use to define the class
        off - the offset into the byte array at which the class bytes begin
        len - the number of bytes in the class
        Returns:
        the class
      • defineOrLoadClass

        protected final java.lang.Class<?> defineOrLoadClass​(java.lang.String className,
                                                             byte[] bytes,
                                                             int off,
                                                             int len,
                                                             java.security.ProtectionDomain protectionDomain)
        Atomically define or load the named class. If the class is already defined, the existing class is returned.
        Parameters:
        className - the class name to define or load
        bytes - the bytes to use to define the class
        off - the offset into the byte array at which the class bytes begin
        len - the number of bytes in the class
        protectionDomain - the protection domain for the defined class
        Returns:
        the class
      • findClass

        protected final java.lang.Class<?> findClass​(java.lang.String className)
                                              throws java.lang.ClassNotFoundException
        Implementation of ClassLoader.findClass(String).
        Overrides:
        findClass in class java.lang.ClassLoader
        Parameters:
        className - the class name
        Returns:
        the result of findClass(className, false, false)
        Throws:
        java.lang.ClassNotFoundException
      • findClass

        protected final java.lang.Class<?> findClass​(java.lang.String moduleName,
                                                     java.lang.String className)
        Implementation of ClassLoader.findClass(String, String).
        Overrides:
        findClass in class java.lang.ClassLoader
        Parameters:
        moduleName - the Java module name
        className - the class name
        Returns:
        the result of findClass(className, false, false)
      • getResource

        public final java.net.URL getResource​(java.lang.String name)
        Finds the resource with the given name. The name of a resource is a '/'-separated path name that identifies the resource. If the resource name starts with "java/" then the parent class loader is used. Otherwise, this method delegates to findResource(String, boolean).
        Overrides:
        getResource in class java.lang.ClassLoader
        Parameters:
        name - the name of the resource
        Returns:
        the resource URL, or null if no such resource exists or the invoker does not have adequate permission to access it
      • getResources

        public final java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)
                                                               throws java.io.IOException
        Finds all available resources with the given name.
        Overrides:
        getResources in class java.lang.ClassLoader
        Parameters:
        name - the resource name
        Returns:
        an enumeration over all the resource URLs; if no resources could be found, the enumeration will be empty
        Throws:
        java.io.IOException - if an I/O error occurs
        See Also:
        getResource(String)
      • findResource

        protected java.net.URL findResource​(java.lang.String name,
                                            boolean exportsOnly)
        Find the resource with the given name and exported status.
        Parameters:
        name - the resource name
        exportsOnly - true to consider only exported resources or false to consider all resources
        Returns:
        the resource URL
        See Also:
        getResource(String)
      • findResource

        protected final java.net.URL findResource​(java.lang.String name)
        Never used. ClassLoader.getResource(String) and related methods can cause a loop condition when this method is implemented; use findResource(String, boolean) instead.
        Overrides:
        findResource in class java.lang.ClassLoader
        Parameters:
        name - ignored
        Returns:
        null always
      • findResource

        protected final java.net.URL findResource​(java.lang.String moduleName,
                                                  java.lang.String name)
                                           throws java.io.IOException
        Find the resource with the given name in specified java module.
        Overrides:
        findResource in class java.lang.ClassLoader
        Parameters:
        moduleName - java module name
        name - the resource name
        Returns:
        the resource URL
        Throws:
        java.io.IOException
        See Also:
        getResource(String)
      • findResources

        protected java.util.Enumeration<java.net.URL> findResources​(java.lang.String name,
                                                                    boolean exportsOnly)
                                                             throws java.io.IOException
        Finds the resources with the given name and exported status.
        Parameters:
        name - the resource name
        exportsOnly - true to consider only exported resources or false to consider all resources
        Returns:
        the resource enumeration
        Throws:
        java.io.IOException - if an I/O error occurs
        See Also:
        getResources(String)
      • findResources

        protected final java.util.Enumeration<java.net.URL> findResources​(java.lang.String name)
        Never used. ClassLoader.getResources(String) and related methods can cause a loop condition when this method is implemented; use findResources(String, boolean) instead. By default, returns an empty enumeration.
        Overrides:
        findResources in class java.lang.ClassLoader
        Parameters:
        name - ignored
        Returns:
        an empty enumeration
      • findResourceAsStream

        protected java.io.InputStream findResourceAsStream​(java.lang.String name,
                                                           boolean exportsOnly)
        Finds the resource with the given name and exported status, returning the resource content as a stream.
        Parameters:
        name - the resource name
        exportsOnly - true to consider only exported resources or false to consider all resources
        Returns:
        the resource stream, or null if the resource is not found
      • getResourceAsStream

        public final java.io.InputStream getResourceAsStream​(java.lang.String name)
        Returns an input stream for reading the specified resource. This method delegates to findResourceAsStream(String, boolean).
        Overrides:
        getResourceAsStream in class java.lang.ClassLoader
        Parameters:
        name - the resource name
        Returns:
        the resource stream, or null if the resource is not found
      • getPackage

        @Deprecated
        protected final java.lang.Package getPackage​(java.lang.String name)
        Deprecated.
        Deprecated in ClassLoader.getPackage(String).
        Load a package which is visible to this class loader.
        Overrides:
        getPackage in class java.lang.ClassLoader
        Parameters:
        name - the package name
        Returns:
        the package, or null if no such package is visible to this class loader
      • getPackageByName

        protected java.lang.Package getPackageByName​(java.lang.String name)
        Perform the actual work to load a package which is visible to this class loader. By default, uses a simple parent-first delegation strategy.
        Parameters:
        name - the package name
        Returns:
        the package, or null if no such package is visible to this class loader
      • getPackages

        protected java.lang.Package[] getPackages()
        Get all defined packages which are visible to this class loader.
        Overrides:
        getPackages in class java.lang.ClassLoader
        Returns:
        the packages
      • findLoadedPackage

        protected final java.lang.Package findLoadedPackage​(java.lang.String name)
        Load a package from this class loader only.
        Parameters:
        name - the package name
        Returns:
        the package or null if no such package is defined by this class loader
      • definePackage

        protected java.lang.Package definePackage​(java.lang.String name,
                                                  java.lang.String specTitle,
                                                  java.lang.String specVersion,
                                                  java.lang.String specVendor,
                                                  java.lang.String implTitle,
                                                  java.lang.String implVersion,
                                                  java.lang.String implVendor,
                                                  java.net.URL sealBase)
                                           throws java.lang.IllegalArgumentException
        Defines a package by name in this ConcurrentClassLoader. If the package was already defined, the existing package is returned instead.
        Overrides:
        definePackage in class java.lang.ClassLoader
        Parameters:
        name - the package name
        specTitle - the specification title
        specVersion - the specification version
        specVendor - the specification vendor
        implTitle - the implementation title
        implVersion - the implementation version
        implVendor - the implementation vendor
        sealBase - if not null, then this package is sealed with respect to the given code source URL
        Returns:
        the newly defined package, or the existing one if one was already defined
        Throws:
        java.lang.IllegalArgumentException