Qwylt

java.lang.module.spi
Class ModuleClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.lang.module.spi.ModuleClassLoader

public class ModuleClassLoader
extends java.lang.ClassLoader

A ClassLoader for one or more connected ModuleArchives.

Since:
Jan 15, 2009
Author:
Bryan Atsatt

Constructor Summary
ModuleClassLoader(ClassSpace imports, java.util.List<ModuleArchive> archives, java.util.List<ModuleResources> resources)
          Constructs an instance for the specified module archives and resources.
ModuleClassLoader(ClassSpace imports, ModuleArchive... archives)
          Constructs an instance for the specified module archives.
 
Method Summary
protected  void add(ModuleArchive archive)
           
protected  java.lang.Class<?> defineClass(java.lang.String className, java.nio.ByteBuffer classData, Module module, java.security.ProtectionDomain protectionDomain)
           
protected  java.lang.Class<?> defineClass(java.lang.String className, java.nio.ByteBuffer classData, ModuleResources resources)
           
protected  java.lang.Package definePackage(java.lang.String packageName, java.util.jar.Manifest manifest, java.net.URL url)
          Defines a new package by name in this ClassLoader.
protected  void ensurePackageDefined(java.lang.String packageName, ModuleResources resources)
          Make sure that a package is defined for the specified name.
protected  java.lang.String findLibrary(java.lang.String libraryName)
          Returns the absolute path name of a native library.
protected  java.net.URL getBootResource(java.lang.String name)
           
protected  java.util.Enumeration<java.net.URL> getBootResources(java.lang.String name)
           
protected  ClassSpace getImports()
           
protected  Module getModule()
           
 java.net.URL getResource(java.lang.String resourcePath)
          Returns the first resource with the specified path.
 java.nio.ByteBuffer getResourceData(java.lang.String resourcePath)
          Returns the first resource with the specified path.
protected  java.nio.ByteBuffer getResourceDataInternal(java.lang.String resourcePath, java.lang.String packageName)
          Returns the first resource with the specified path from the internal providers.
protected  java.net.URL getResourceInternal(java.lang.String resourcePath, java.lang.String packageName)
          Returns the first resource with the specified path from the internal providers.
 java.util.Set<java.lang.String> getResourcePaths()
          Returns all the unique resource paths.
protected  java.util.List<ModuleResources> getResources()
           
 java.util.Enumeration<java.net.URL> getResources(java.lang.String resourcePath)
          Returns all resources with the specified path.
protected  java.util.Enumeration<java.net.URL> getResourcesInternal(java.lang.String resourcePath, java.lang.String packageName)
          Returns all resources with the specified path from the internal providers.
protected  java.lang.Class<?> handleClassNotFound(java.lang.String className, java.lang.String packageName)
          TODO: move to 'hook' class? Handle the class not found condition.
protected  java.lang.String handleLibraryNotFound(java.lang.String libraryName)
           
protected  java.nio.ByteBuffer handleResourceDataNotFound(java.lang.String resourcePath, java.lang.String packageName)
          TODO: move to 'hook' class? Handle the resource data not found condition.
protected  java.net.URL handleResourceNotFound(java.lang.String resourcePath, java.lang.String packageName)
          TODO: move to 'hook' class? Handle the resource not found condition.
protected  java.util.Enumeration<java.net.URL> handleResourcesNotFound(java.lang.String resourcePath, java.lang.String packageName)
          TODO: move to 'hook' class? Handle the resources not found condition.
protected  java.lang.Class<?> loadBootClass(java.lang.String name)
           
 java.lang.Class<?> loadClass(java.lang.String className)
          Loads the class with the specified binary name.
 java.lang.Class<?> loadLocalClass(java.lang.String className, java.lang.String packageName)
          Loads the class with the specified binary name from the internal providers.
protected  void reset(java.util.List<ModuleArchive> archives, java.util.List<ModuleResources> resources)
           
protected  void setImports(ClassSpace imports)
           
 java.lang.String toString()
          Returns a string representation of this instance.
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResourceAsStream, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ModuleClassLoader

public ModuleClassLoader(ClassSpace imports,
                         ModuleArchive... archives)
Constructs an instance for the specified module archives.

Parameters:
imports - The imports.
archives - The archives.

ModuleClassLoader

public ModuleClassLoader(ClassSpace imports,
                         java.util.List<ModuleArchive> archives,
                         java.util.List<ModuleResources> resources)
Constructs an instance for the specified module archives and resources.

Parameters:
imports - The imports.
archives - The archives.
resources - The resources.
Method Detail

loadClass

public java.lang.Class<?> loadClass(java.lang.String className)
                             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 to be loaded.
Returns:
The resulting Class object.
Throws:
java.lang.ClassNotFoundException - If the class was not found.

loadLocalClass

public java.lang.Class<?> loadLocalClass(java.lang.String className,
                                         java.lang.String packageName)
                                  throws java.lang.ClassNotFoundException
Loads the class with the specified binary name from the internal providers.

Parameters:
className - The binary name of the class to be loaded.
packageName - The '.' separated package name.
Returns:
The class.
Throws:
java.lang.ClassNotFoundException - If not found.

getResourceData

public java.nio.ByteBuffer getResourceData(java.lang.String resourcePath)
Returns the first resource with the specified path.

Parameters:
resourcePath - The '/' separated relative resource path, which must not have a leading '/'.
Returns:
The resource or null if not found.

getResourceDataInternal

protected java.nio.ByteBuffer getResourceDataInternal(java.lang.String resourcePath,
                                                      java.lang.String packageName)
Returns the first resource with the specified path from the internal providers.

Parameters:
resourcePath - The '/' separated relative resource path, which must not have a leading '/'.
packageName - The '.' separated package name.
Returns:
The resource or null if not found.

getResource

public java.net.URL getResource(java.lang.String resourcePath)
Returns the first resource with the specified path.

Overrides:
getResource in class java.lang.ClassLoader
Parameters:
resourcePath - The '/' separated relative resource path, which must not have a leading '/'.
Returns:
The resource or null if not found.

getResourceInternal

protected java.net.URL getResourceInternal(java.lang.String resourcePath,
                                           java.lang.String packageName)
Returns the first resource with the specified path from the internal providers.

Parameters:
resourcePath - The '/' separated relative resource path, which must not have a leading '/'.
packageName - The '.' separated package name.
Returns:
The resource or null if not found.

getResources

public java.util.Enumeration<java.net.URL> getResources(java.lang.String resourcePath)
                                                 throws java.io.IOException
Returns all resources with the specified path.

Overrides:
getResources in class java.lang.ClassLoader
Parameters:
resourcePath - The '/' separated relative resource path, which must not have a leading '/'.
Returns:
The resources, empty if no matching resources found.
Throws:
java.io.IOException - If an I/O error occurs.

getResourcesInternal

protected java.util.Enumeration<java.net.URL> getResourcesInternal(java.lang.String resourcePath,
                                                                   java.lang.String packageName)
                                                            throws java.io.IOException
Returns all resources with the specified path from the internal providers.

Parameters:
resourcePath - The '/' separated relative resource path, which must not have a leading '/'.
packageName - The '.' separated package name.
Returns:
The resources, empty if no matching resources found.
Throws:
java.io.IOException - If an I/O error occurs.

getResourcePaths

public java.util.Set<java.lang.String> getResourcePaths()
Returns all the unique resource paths.

Returns:
the set of all resource paths.

toString

public java.lang.String toString()
Returns a string representation of this instance.

Overrides:
toString in class java.lang.Object
Returns:
The string.

findLibrary

protected java.lang.String findLibrary(java.lang.String libraryName)
Returns the absolute path name of a native library. The VM invokes this method to locate the native libraries that belong to classes loaded with this class loader. If this method returns null, the VM searches the library along the path specified as the "java.library.path" property.

Overrides:
findLibrary in class java.lang.ClassLoader
Parameters:
libraryName - The library name
Returns:
The absolute path of the native library
Since:
1.2
See Also:
System.loadLibrary(String), System.mapLibraryName(String)

defineClass

protected java.lang.Class<?> defineClass(java.lang.String className,
                                         java.nio.ByteBuffer classData,
                                         ModuleResources resources)
                                  throws java.lang.ClassFormatError
Throws:
java.lang.ClassFormatError

defineClass

protected final java.lang.Class<?> defineClass(java.lang.String className,
                                               java.nio.ByteBuffer classData,
                                               Module module,
                                               java.security.ProtectionDomain protectionDomain)
                                        throws java.lang.ClassFormatError
Throws:
java.lang.ClassFormatError

handleClassNotFound

protected java.lang.Class<?> handleClassNotFound(java.lang.String className,
                                                 java.lang.String packageName)
                                          throws java.lang.ClassNotFoundException
TODO: move to 'hook' class? Handle the class not found condition.

Parameters:
className - The class that could not be found.
packageName - The package name of the class that could not be found.
Returns:
The class if it can be found.
Throws:
java.lang.ClassNotFoundException - If the class still cannot be found.

handleResourceDataNotFound

protected java.nio.ByteBuffer handleResourceDataNotFound(java.lang.String resourcePath,
                                                         java.lang.String packageName)
TODO: move to 'hook' class? Handle the resource data not found condition.

Parameters:
resourcePath - The path of the resource that could not be found.
packageName - The package name of the resource that could not be found.
Returns:
The resource data or null if it still cannot be found.

handleResourceNotFound

protected java.net.URL handleResourceNotFound(java.lang.String resourcePath,
                                              java.lang.String packageName)
TODO: move to 'hook' class? Handle the resource not found condition.

Parameters:
resourcePath - The path of the resource that could not be found.
packageName - The package name of the resource that could not be found.
Returns:
The resource or null if it still cannot be found.

handleResourcesNotFound

protected java.util.Enumeration<java.net.URL> handleResourcesNotFound(java.lang.String resourcePath,
                                                                      java.lang.String packageName)
                                                               throws java.io.IOException
TODO: move to 'hook' class? Handle the resources not found condition.

Parameters:
resourcePath - The path of the resources that could not be found.
packageName - The package name of the resources that could not be found.
Returns:
The resource enumeration or an empty enumeration if they still cannot be found.
Throws:
java.io.IOException

handleLibraryNotFound

protected java.lang.String handleLibraryNotFound(java.lang.String libraryName)

getModule

protected Module getModule()

loadBootClass

protected java.lang.Class<?> loadBootClass(java.lang.String name)
                                    throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getBootResource

protected java.net.URL getBootResource(java.lang.String name)

getBootResources

protected java.util.Enumeration<java.net.URL> getBootResources(java.lang.String name)
                                                        throws java.io.IOException
Throws:
java.io.IOException

ensurePackageDefined

protected void ensurePackageDefined(java.lang.String packageName,
                                    ModuleResources resources)
Make sure that a package is defined for the specified name.

Parameters:
packageName - The package name.
resources - he resources from which the class is being defined.

definePackage

protected java.lang.Package definePackage(java.lang.String packageName,
                                          java.util.jar.Manifest manifest,
                                          java.net.URL url)
                                   throws java.lang.IllegalArgumentException
Defines a new package by name in this ClassLoader. The attributes contained in the specified Manifest will be used to obtain package version and sealing information. For sealed packages, the additional URL specifies the code source URL from which the package was loaded.

Parameters:
packageName - the package name
manifest - the Manifest containing package version and sealing information
url - the code source url for the package, or null if none
Returns:
the newly defined Package object
Throws:
java.lang.IllegalArgumentException - if the package name duplicates an existing package either in this class loader or one of its ancestors

getResources

protected java.util.List<ModuleResources> getResources()

reset

protected void reset(java.util.List<ModuleArchive> archives,
                     java.util.List<ModuleResources> resources)

add

protected void add(ModuleArchive archive)

setImports

protected void setImports(ClassSpace imports)

getImports

protected ClassSpace getImports()

Qwylt

Send comments or questions to Bryan Atsatt