APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.loader

public final Class Loader

extends SecureClassLoader
Class Inheritance
Imports
java.io.IOException, java.lang.module.Configuration, .ModuleDescriptor, .ModuleReader, .ModuleReference, .ResolvedModule, java.net.MalformedURLException, .URI, .URL, java.nio.ByteBuffer, java.security.CodeSigner, .CodeSource, .SecureClassLoader, java.util.ArrayList, .Collection, .Collections, .Enumeration, .HashMap, .Iterator, .List, .Map, .Objects, .Optional, java.util.concurrent.ConcurrentHashMap, java.util.stream.Stream, jdk.internal.access.SharedSecrets, jdk.internal.module.Resources

A class loader that loads classes and resources from a collection of modules, or from a single module where the class loader is a member of a pool of class loaders.

The delegation model used by this ClassLoader differs to the regular delegation model. When requested to load a class then this ClassLoader first maps the class name to its package name. If there a module defined to the Loader containing the package then the class loader attempts to load from that module. If the package is instead defined to a module in a "remote" ClassLoader then this class loader delegates directly to that class loader. The map of package name to remote class loader is created based on the modules read by modules defined to this class loader. If the package is not local or remote then this class loader will delegate to the parent class loader. This allows automatic modules (for example) to link to types in the unnamed module of the parent class loader.

See Also
ModuleLayer#defineModulesWithOneLoader, ModuleLayer#defineModulesWithManyLoaders

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Loader.LoadedModule

A module defined/loaded to a Loader.

private static class
Loader.NullModuleReader

A ModuleReader that doesn't read any resources.

Field Summary

Modifier and TypeField and Description
private final Map<String, Loader.LoadedModule>
private final Map<ModuleReference, ModuleReader>
private final Map<String, ModuleReference>
private final ClassLoader
private final LoaderPool
private final Map<String, ClassLoader>

Constructor Summary

AccessConstructor and Description
public
Loader(ResolvedModule resolvedModule, LoaderPool pool, ClassLoader parent)

Creates a Loader in a loader pool that loads classes/resources from one module.

public
Loader(Collection<ResolvedModule> modules, ClassLoader parent)

Creates a Loader that loads classes/resources from a collection of modules.

Method Summary

Modifier and TypeMethod and Description
private ModuleReader
createModuleReader(ModuleReference mref)

Creates a ModuleReader for the given module.

protected Class<?>
findClass(String cn)

Overrides java.lang.ClassLoader.findClass.

Finds the class with the specified binary name.

protected Class<?>
findClass(String mn, String cn)

Overrides java.lang.ClassLoader.findClass.

Finds the class with the specified binary name in the given module.

private Class<?>

Returns:

the resulting Class or null if not found
findClassInModuleOrNull
(Loader.LoadedModule loadedModule, String cn)

Finds the class with the specified binary name if in a module defined to this ClassLoader.

private Loader.LoadedModule
findLoadedModule(String cn)

Find the candidate module for the given class name.

private Optional<ModuleLayer>
findModuleLayer(ModuleLayer parent, Configuration cf)

Find the layer corresponding to the given configuration in the tree of layers rooted at the given parent.

protected URL
findResource(String mn, String name)

Overrides java.lang.ClassLoader.findResource.

Returns a URL to a resource of the given name in a module defined to this class loader.

public URL
findResource(String name)

Overrides java.lang.ClassLoader.findResource.

Finds the resource with the given name.

public Enumeration<URL>
findResources(String name)

Overrides java.lang.ClassLoader.findResources.

Returns an enumeration of URL objects representing all the resources with the given name.

private List<URL>
findResourcesAsList(String name)

Finds the resources with the given name in this class loader.

public URL
getResource(String name)

Overrides java.lang.ClassLoader.getResource.

Finds the resource with the given name.

public Enumeration<URL>
getResources(String name)

Overrides java.lang.ClassLoader.getResources.

Finds all the resources with the given name.

public Loader
initRemotePackageMap(Configuration
the Configuration containing at least modules to be defined to this class loader
cf
,
List<ModuleLayer>
the parent ModuleLayers
parentModuleLayers
)

Completes initialization of this Loader.

private boolean
isOpen(ModuleReference mref, String pn)

Returns true if the given module opens the given package unconditionally.

protected Class<?>
loadClass(String cn, boolean resolve)

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.

private ModuleReader
moduleReaderFor(ModuleReference mref)

Returns the ModuleReader for the given module.

private String
packageName(String cn)

Returns the package name for the given class name

public LoaderPool
pool()

Returns the loader pool that this loader is in or null if this loader is not in a loader pool.

private void
remotePackage(String pn, ClassLoader loader)

Adds to remotePackageToLoader so that an attempt to load a class in the package delegates to the given class loader.

Inherited from java.security.SecureClassLoader:
defineClassdefineClassgetPermissions

Field Detail

localPackageToModuleback to summary
private final Map<String, Loader.LoadedModule> localPackageToModule
moduleToReaderback to summary
private final Map<ModuleReference, ModuleReader> moduleToReader
nameToModuleback to summary
private final Map<String, ModuleReference> nameToModule
parentback to summary
private final ClassLoader parent

Hides java.lang.ClassLoader.parent.

poolback to summary
private final LoaderPool pool
remotePackageToLoaderback to summary
private final Map<String, ClassLoader> remotePackageToLoader

Constructor Detail

Loaderback to summary
public Loader(ResolvedModule resolvedModule, LoaderPool pool, ClassLoader parent)

Creates a Loader in a loader pool that loads classes/resources from one module.

Loaderback to summary
public Loader(Collection<ResolvedModule> modules, ClassLoader parent)

Creates a Loader that loads classes/resources from a collection of modules.

Exceptions
IllegalArgumentException:
If two or more modules have the same package

Method Detail

createModuleReaderback to summary
private ModuleReader createModuleReader(ModuleReference mref)

Creates a ModuleReader for the given module.

findClassback to summary
protected Class<?> findClass(String cn) throws ClassNotFoundException

Overrides java.lang.ClassLoader.findClass.

Finds the class with the specified binary name.

Annotations
@Override
Exceptions
ClassNotFoundException:

Doc from java.lang.ClassLoader.findClass.

If the class could not be found

findClassback to summary
protected Class<?> findClass(String mn, String cn)

Overrides java.lang.ClassLoader.findClass.

Finds the class with the specified binary name in the given module. This method returns null if the class cannot be found.

Annotations
@Override
findClassInModuleOrNullback to summary
private Class<?> findClassInModuleOrNull(Loader.LoadedModule loadedModule, String cn)

Finds the class with the specified binary name if in a module defined to this ClassLoader.

Returns:Class<?>

the resulting Class or null if not found

findLoadedModuleback to summary
private Loader.LoadedModule findLoadedModule(String cn)

Find the candidate module for the given class name. Returns null if none of the modules defined to this class loader contain the API package for the class.

findModuleLayerback to summary
private Optional<ModuleLayer> findModuleLayer(ModuleLayer parent, Configuration cf)

Find the layer corresponding to the given configuration in the tree of layers rooted at the given parent.

findResourceback to summary
protected URL findResource(String mn, String name) throws IOException

Overrides java.lang.ClassLoader.findResource.

Returns a URL to a resource of the given name in a module defined to this class loader.

Annotations
@Override
Exceptions
IOException:

Doc from java.lang.ClassLoader.findResource.

If I/O errors occur

findResourceback to summary
public URL findResource(String name)

Overrides java.lang.ClassLoader.findResource.

Doc from java.lang.ClassLoader.findResource.

Finds the resource with the given name. Class loader implementations should override this method.

For resources in named modules then the method must implement the rules for encapsulation specified in the Module getResourceAsStream method. Additionally, it must not find non-".class" resources in packages of named modules unless the package is opened unconditionally.

Annotations
@Override
findResourcesback to summary
public Enumeration<URL> findResources(String name) throws IOException

Overrides java.lang.ClassLoader.findResources.

Doc from java.lang.ClassLoader.findResources.

Returns an enumeration of URL objects representing all the resources with the given name. Class loader implementations should override this method.

For resources in named modules then the method must implement the rules for encapsulation specified in the Module getResourceAsStream method. Additionally, it must not find non-".class" resources in packages of named modules unless the package is opened unconditionally.

Annotations
@Override
Exceptions
IOException:
If I/O errors occur
findResourcesAsListback to summary
private List<URL> findResourcesAsList(String name) throws IOException

Finds the resources with the given name in this class loader.

getResourceback to summary
public URL getResource(String name)

Overrides java.lang.ClassLoader.getResource.

Doc from java.lang.ClassLoader.getResource.

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a '/'-separated path name that identifies the resource.

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).

Annotations
@Override
getResourcesback to summary
public Enumeration<URL> getResources(String name) throws IOException

Overrides java.lang.ClassLoader.getResources.

Doc from java.lang.ClassLoader.getResources.

Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a /-separated path name that identifies the resource.

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).

Annotations
@Override
Exceptions
IOException:
If I/O errors occur
initRemotePackageMapback to summary
public Loader initRemotePackageMap(Configuration cf, List<ModuleLayer> parentModuleLayers)

Completes initialization of this Loader. This method populates remotePackageToLoader with the packages of the remote modules, where "remote modules" are the modules read by modules defined to this loader.

Parameters
cf:Configuration

the Configuration containing at least modules to be defined to this class loader

parentModuleLayers:List<ModuleLayer>

the parent ModuleLayers

isOpenback to summary
private boolean isOpen(ModuleReference mref, String pn)

Returns true if the given module opens the given package unconditionally.

Implementation Note

This method currently iterates over each of the open packages. This will be replaced once the ModuleDescriptor.Opens API is updated.

loadClassback to summary
protected Class<?> loadClass(String cn, boolean resolve) throws ClassNotFoundException

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.

Annotations
@Override
Exceptions
ClassNotFoundException:

Doc from java.lang.ClassLoader.loadClass.

If the class could not be found

moduleReaderForback to summary
private ModuleReader moduleReaderFor(ModuleReference mref)

Returns the ModuleReader for the given module.

packageNameback to summary
private String packageName(String cn)

Returns the package name for the given class name

poolback to summary
public LoaderPool pool()

Returns the loader pool that this loader is in or null if this loader is not in a loader pool.

remotePackageback to summary
private void remotePackage(String pn, ClassLoader loader)

Adds to remotePackageToLoader so that an attempt to load a class in the package delegates to the given class loader.

Exceptions
IllegalStateException:
if the package is already mapped to a different class loader
jdk.internal.loader back to summary

private Class Loader.LoadedModule

extends Object
Class Inheritance

A module defined/loaded to a Loader.

Field Summary

Modifier and TypeField and Description
private final CodeSource
private final ModuleReference

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv CodeSource
pack-priv ModuleReference
mref()

pack-priv String
name()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

csback to summary
private final CodeSource cs
mrefback to summary
private final ModuleReference mref

Constructor Detail

LoadedModuleback to summary
pack-priv LoadedModule(ModuleReference mref)

Method Detail

codeSourceback to summary
pack-priv CodeSource codeSource()
mrefback to summary
pack-priv ModuleReference mref()
nameback to summary
pack-priv String name()
jdk.internal.loader back to summary

private Class Loader.NullModuleReader

extends Object
implements ModuleReader
Class Inheritance
All Implemented Interfaces
java.lang.module.ModuleReader, java.io.Closeable, java.lang.AutoCloseable

A ModuleReader that doesn't read any resources.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.module.ModuleReader.close.

Closes the module reader.

public Optional<URI>
find(String name)

Implements java.lang.module.ModuleReader.find.

Finds a resource, returning a URI to the resource in the module.

public Stream<String>
list()

Implements java.lang.module.ModuleReader.list.

Lists the contents of the module, returning a stream of elements that are the names of all resources in the module.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

NullModuleReaderback to summary
private NullModuleReader()

Method Detail

closeback to summary
public void close()

Implements java.lang.module.ModuleReader.close.

Doc from java.lang.module.ModuleReader.close.

Closes the module reader. Once closed then subsequent calls to locate or read a resource will fail by throwing IOException.

A module reader is not required to be asynchronously closeable. If a thread is reading a resource and another thread invokes the close method, then the second thread may block until the read operation is complete.

Annotations
@Override
findback to summary
public Optional<URI> find(String name)

Implements java.lang.module.ModuleReader.find.

Doc from java.lang.module.ModuleReader.find.

Finds a resource, returning a URI to the resource in the module.

If the module reader can determine that the name locates a directory then the resulting URI will end with a slash ('/').

Annotations
@Override
listback to summary
public Stream<String> list()

Implements java.lang.module.ModuleReader.list.

Doc from java.lang.module.ModuleReader.list.

Lists the contents of the module, returning a stream of elements that are the names of all resources in the module. Whether the stream of elements includes names corresponding to directories in the module is module reader specific.

In lazy implementations then an IOException may be thrown when using the stream to list the module contents. If this occurs then the IOException will be wrapped in an java.io.UncheckedIOException and thrown from the method that caused the access to be attempted.

The returned stream may contain references to one or more open directories in the module. The directories are closed by closing the stream.

The behavior of the stream when used after the module reader is closed is implementation specific and therefore not specified.

Annotations
@Override