Class ModuleClassLoader


  • public class ModuleClassLoader
    extends ConcurrentClassLoader
    A module classloader. Instances of this class implement the complete view of classes and resources available in a module. Contrast with Module, which has API methods to access the exported view of classes and resources.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ModuleClassLoader.Configuration
      An opaque configuration used internally to create a module class loader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearAssertionStatus()
      protected java.lang.Object clone()
      private java.lang.Class<?> defineClass​(java.lang.String name, ClassSpec classSpec, ResourceLoader resourceLoader)
      Define a class from a class name and class spec.
      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.
      private java.lang.Package definePackage​(java.lang.String name, PackageSpec spec)
      Define a package from a package spec.
      private java.lang.Class<?> doDefineOrLoadClass​(java.lang.String className, byte[] bytes, int off, int len, java.security.ProtectionDomain protectionDomain)  
      boolean equals​(java.lang.Object obj)
      protected void finalize()
      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.String findLibrary​(java.lang.String libname)
      Find a library from one of the resource loaders.
      java.net.URL findResource​(java.lang.String name, boolean exportsOnly)
      Find the resource with the given name and exported status.
      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.
      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.String getClassNotFoundExceptionMessage​(java.lang.String className, Module fromModule)
      Returns an exception message used when producing instances of ClassNotFoundException.
      (package private) LocalLoader getLocalLoader()
      Get the local loader which refers to this module class loader.
      java.util.Set<java.lang.String> getLocalPaths()
      Get the (unmodifiable) set of paths which are locally available in this module class loader.
      Module getModule()
      Get the module for this class loader.
      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.
      (package private) java.util.Set<java.lang.String> getPaths()  
      private java.security.ProtectionDomain getProtectionDomain​(java.security.CodeSource codeSource)  
      (package private) ResourceLoader[] getResourceLoaders()  
      int hashCode()
      java.util.Iterator<Resource> iterateResources​(java.lang.String startName, boolean recurse)
      Iterate the resources within this module class loader.
      java.lang.Class<?> loadClassLocal​(java.lang.String className)
      Load a class from this class loader.
      java.lang.Class<?> loadClassLocal​(java.lang.String className, boolean resolve)
      Load a local class from this class loader.
      java.util.List<Resource> loadResourceLocal​(java.lang.String name)
      Load a local resource from this class loader.
      (package private) Resource loadResourceLocal​(java.lang.String root, java.lang.String name)
      Load a local resource from a specific root from this module class loader.
      protected void postDefine​(ClassSpec classSpec, java.lang.Class<?> definedClass)
      A hook which is invoked after a class is defined.
      protected void preDefine​(ClassSpec classSpec, java.lang.String className)
      A hook which is invoked before a class is defined.
      (package private) boolean recalculate()
      Recalculate the path maps for this module class loader.
      void setClassAssertionStatus​(java.lang.String className, boolean enabled)
      void setDefaultAssertionStatus​(boolean enabled)
      void setPackageAssertionStatus​(java.lang.String packageName, boolean enabled)
      private boolean setResourceLoaders​(Paths<ResourceLoader,​ResourceLoaderSpec> paths, ResourceLoaderSpec[] resourceLoaders)  
      (package private) boolean setResourceLoaders​(ResourceLoaderSpec[] resourceLoaders)
      Change the set of resource loaders for this module class loader, and recalculate the path maps.
      java.lang.String toString()
      Get a string representation of this class loader.
      • Methods inherited from class java.lang.ClassLoader

        defineClass, defineClass, defineClass, defineClass, findClass, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, registerAsParallelCapable, resolveClass, resources, setSigners
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • module

        private final Module module
      • transformer

        private final java.lang.instrument.ClassFileTransformer transformer
      • protectionDomains

        private final java.util.IdentityHashMap<java.security.CodeSource,​java.security.ProtectionDomain> protectionDomains
    • Constructor Detail

      • ModuleClassLoader

        protected ModuleClassLoader​(ModuleClassLoader.Configuration configuration)
        Construct a new instance.
        Parameters:
        configuration - the module class loader configuration to use
    • Method Detail

      • recalculate

        boolean recalculate()
        Recalculate the path maps for this module class loader.
        Returns:
        true if the paths were recalculated, or false if another thread finished recalculating before the calling thread
      • setResourceLoaders

        boolean setResourceLoaders​(ResourceLoaderSpec[] resourceLoaders)
        Change the set of resource loaders for this module class loader, and recalculate the path maps.
        Parameters:
        resourceLoaders - the new resource loaders
        Returns:
        true if the paths were recalculated, or false if another thread finished recalculating before the calling thread
      • getLocalLoader

        LocalLoader getLocalLoader()
        Get the local loader which refers to this module class loader.
        Returns:
        the local loader
      • findClass

        protected final 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.

        Overrides:
        findClass in class ConcurrentClassLoader
        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
      • getClassNotFoundExceptionMessage

        protected java.lang.String getClassNotFoundExceptionMessage​(java.lang.String className,
                                                                    Module fromModule)
        Returns an exception message used when producing instances of ClassNotFoundException. This can be overridden by subclasses to customise the error message.
        Parameters:
        className - the name of the class which is missing
        fromModule - the module from which the class could not be found
        Returns:
        an exception message used when producing instances of ClassNotFoundException
      • loadClassLocal

        public java.lang.Class<?> loadClassLocal​(java.lang.String className)
                                          throws java.lang.ClassNotFoundException
        Load a class from this class loader.
        Parameters:
        className - the class name to load
        Returns:
        the loaded class or null if it was not found
        Throws:
        java.lang.ClassNotFoundException - if an exception occurs while loading the class or its dependencies
      • loadClassLocal

        public java.lang.Class<?> loadClassLocal​(java.lang.String className,
                                                 boolean resolve)
                                          throws java.lang.ClassNotFoundException
        Load a local class from this class loader.
        Parameters:
        className - the class name
        resolve - true to resolve the loaded class
        Returns:
        the loaded class or null if it was not found
        Throws:
        java.lang.ClassNotFoundException - if an error occurs while loading the class
      • loadResourceLocal

        Resource loadResourceLocal​(java.lang.String root,
                                   java.lang.String name)
        Load a local resource from a specific root from this module class loader.
        Parameters:
        root - the root name
        name - the resource name
        Returns:
        the resource, or null if it was not found
      • loadResourceLocal

        public java.util.List<Resource> loadResourceLocal​(java.lang.String name)
        Load a local resource from this class loader.
        Parameters:
        name - the resource name
        Returns:
        the list of resources
      • doDefineOrLoadClass

        private java.lang.Class<?> doDefineOrLoadClass​(java.lang.String className,
                                                       byte[] bytes,
                                                       int off,
                                                       int len,
                                                       java.security.ProtectionDomain protectionDomain)
      • getProtectionDomain

        private java.security.ProtectionDomain getProtectionDomain​(java.security.CodeSource codeSource)
      • defineClass

        private java.lang.Class<?> defineClass​(java.lang.String name,
                                               ClassSpec classSpec,
                                               ResourceLoader resourceLoader)
        Define a class from a class name and class spec. Also defines any enclosing Package instances, and performs any sealed-package checks.
        Parameters:
        name - the class name
        classSpec - the class spec
        resourceLoader - the resource loader of the class spec
        Returns:
        the new class
      • preDefine

        protected void preDefine​(ClassSpec classSpec,
                                 java.lang.String className)
        A hook which is invoked before a class is defined.
        Parameters:
        classSpec - the class spec of the defined class
        className - the class to be defined
      • postDefine

        protected void postDefine​(ClassSpec classSpec,
                                  java.lang.Class<?> definedClass)
        A hook which is invoked after a class is defined.
        Parameters:
        classSpec - the class spec of the defined class
        definedClass - the class that was defined
      • definePackage

        private java.lang.Package definePackage​(java.lang.String name,
                                                PackageSpec spec)
        Define a package from a package spec.
        Parameters:
        name - the package name
        spec - the package specification
        Returns:
        the new package
      • findLibrary

        protected final java.lang.String findLibrary​(java.lang.String libname)
        Find a library from one of the resource loaders.
        Overrides:
        findLibrary in class java.lang.ClassLoader
        Parameters:
        libname - the library name
        Returns:
        the full absolute path to the library
      • findResource

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

        public final 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.
        Overrides:
        findResources in class ConcurrentClassLoader
        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:
        ConcurrentClassLoader.getResources(String)
      • findResourceAsStream

        public final 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.
        Overrides:
        findResourceAsStream in class ConcurrentClassLoader
        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
      • getModule

        public final Module getModule()
        Get the module for this class loader.
        Returns:
        the module
      • toString

        public final java.lang.String toString()
        Get a string representation of this class loader.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string
      • getPaths

        java.util.Set<java.lang.String> getPaths()
      • definePackage

        protected final 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 ConcurrentClassLoader
        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
      • getPackageByName

        protected final 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.
        Overrides:
        getPackageByName in class ConcurrentClassLoader
        Parameters:
        name - the package name
        Returns:
        the package, or null if no such package is visible to this class loader
      • getPackages

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

        public final void setDefaultAssertionStatus​(boolean enabled)
        Overrides:
        setDefaultAssertionStatus in class java.lang.ClassLoader
      • setPackageAssertionStatus

        public final void setPackageAssertionStatus​(java.lang.String packageName,
                                                    boolean enabled)
        Overrides:
        setPackageAssertionStatus in class java.lang.ClassLoader
      • setClassAssertionStatus

        public final void setClassAssertionStatus​(java.lang.String className,
                                                  boolean enabled)
        Overrides:
        setClassAssertionStatus in class java.lang.ClassLoader
      • clearAssertionStatus

        public final void clearAssertionStatus()
        Overrides:
        clearAssertionStatus in class java.lang.ClassLoader
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public final boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • clone

        protected final java.lang.Object clone()
                                        throws java.lang.CloneNotSupportedException
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • finalize

        protected final void finalize()
                               throws java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable
      • iterateResources

        public final java.util.Iterator<Resource> iterateResources​(java.lang.String startName,
                                                                   boolean recurse)
        Iterate the resources within this module class loader. Only resource roots which are inherently iterable will be checked, thus the result of this method may only be a subset of the actual loadable resources. The returned resources are not sorted or grouped in any particular way.
        Parameters:
        startName - the directory name to search
        recurse - true to recurse into subdirectories, false otherwise
        Returns:
        the resource iterator
      • getLocalPaths

        public final java.util.Set<java.lang.String> getLocalPaths()
        Get the (unmodifiable) set of paths which are locally available in this module class loader. The set will include all paths defined by the module's resource loaders, minus any paths excluded by filters. The set will generally always contain an empty entry (""). The set is unordered and unsorted, and is iterable in O(n) time and accessible in O(1) time.
        Returns:
        the set of local paths