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

public Class BuiltinClassLoader

extends SecureClassLoader
Class Inheritance
Known Direct Subclasses
jdk.internal.loader.ClassLoaders.BootClassLoader, jdk.internal.loader.ClassLoaders.PlatformClassLoader, jdk.internal.loader.ClassLoaders.AppClassLoader
Imports
java.io.IOException, .InputStream, java.lang.module.ModuleDescriptor, .ModuleReference, .ModuleReader, java.lang.ref.SoftReference, java.net.MalformedURLException, .URI, .URL, java.nio.ByteBuffer, java.security.CodeSigner, .CodeSource, .SecureClassLoader, java.util.ArrayList, .Collections, .Enumeration, .Iterator, .List, .Map, .NoSuchElementException, .Optional, java.util.concurrent.ConcurrentHashMap, java.util.function.Function, java.util.jar.Attributes, .Manifest, java.util.stream.Stream, jdk.internal.access.SharedSecrets, jdk.internal.misc.VM, jdk.internal.module.ModulePatcher.PatchedModuleReader, .Resources, jdk.internal.vm.annotation.Stable

The platform or application class loader. Resources loaded from modules defined to the boot class loader are also loaded via an instance of this ClassLoader type.

This ClassLoader supports loading of classes and resources from modules. Modules are defined to the ClassLoader by invoking the loadModule method. Defining a module to this ClassLoader has the effect of making the types in the module visible.

This ClassLoader also supports loading of classes and resources from a class path of URLs that are specified to the ClassLoader at construction time. The class path may expand at runtime (the Class-Path attribute in JAR files or via instrumentation agents).

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 is a module defined to a BuiltinClassLoader containing this package then the class loader delegates directly to that class loader. If there isn't a module containing the package then it delegates the search to the parent class loader and if not found in the parent then it searches the class path. The main difference between this and the usual delegation model is that it allows the platform class loader to delegate to the application class loader, important with upgraded modules defined to the platform class loader.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
BuiltinClassLoader.LoadedModule

A module defined/loaded by a built-in class loader.

private static class
BuiltinClassLoader.NullModuleReader

A ModuleReader that doesn't read any resources.

Field Summary

Modifier and TypeField and Description
private final Map<ModuleReference, ModuleReader>
private final Map<String, ModuleReference>
private static final Map<String, BuiltinClassLoader.LoadedModule>
private final BuiltinClassLoader
private volatile SoftReference<Map<String, List<URL>>>
private URLClassPath

Constructor Summary

AccessConstructor and Description
pack-priv
BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp)

Create a new instance.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
appendClassPath(String path)

Appends to the given file path to the class path.

private Class<?>

Returns:

the resulting Class or null if an I/O error occurs
defineClass
(String cn, BuiltinClassLoader.LoadedModule loadedModule)

Defines the given binary class name to the VM, loading the class bytes from the given module.

private Class<?>

Returns:

the resulting Class
defineClass
(String cn, Resource res)

Defines the given binary class name to the VM, loading the class bytes via the given Resource object.

protected Package
defineOrCheckPackage(String pn, Manifest man, URL url)

Defines a package in this ClassLoader.

private Package
definePackage(String pn, Manifest man, URL url)

Defines a new package in this ClassLoader.

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 a module.

private Class<?>

Returns:

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

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

private Class<?>

Returns:

the resulting Class or null if not found
findClassOnClassPathOrNull
(String cn)

Finds the class with the specified binary name on the class path.

private BuiltinClassLoader.LoadedModule
findLoadedModule(String cn)

Finds the candidate loaded module for the given class name.

private BuiltinClassLoader.LoadedModule
findLoadedModule(String mn, String cn)

Finds the candidate loaded module for the given class name in the named module.

private List<URL>
findMiscResource(String name)

Returns the list of URLs to a "miscellaneous" resource in modules defined to this loader.

protected ModuleReference
findModule(String
The name of the module to find
name
)

Returns the ModuleReference for the named module defined to this class loader; or null if not defined.

public 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 a resource with the given name in the modules defined to this class loader or its class path.

private URL
findResource(ModuleReference mref, String name)

Returns the URL to a resource in a module or null if not found.

public InputStream
findResourceAsStream(String mn, String name)

Returns an input stream to a resource of the given name in a module defined to this class loader.

private URL
findResourceOnClassPath(String name)

Returns a URL to a resource on the class path.

public Enumeration<URL>
findResources(String name)

Overrides java.lang.ClassLoader.findResources.

Returns an enumeration of URL objects to all the resources with the given name in modules defined to this class loader or on the class path of this loader.

private Enumeration<URL>
findResourcesOnClassPath(String name)

Returns the URLs of all resources of the given name on the class path.

private Package
getAndVerifyPackage(String pn, Manifest man, URL url)

Gets the Package with the specified package name.

pack-priv boolean
hasClassPath()

Returns true if there is a class path associated with this class loader.

private boolean
isOpen(ModuleReference mref, String pn)

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

private boolean
isSealed(String pn, Manifest man)

Returns true if the specified package name is sealed according to the given manifest.

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

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.

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

A variation of loadClass to load a class with the specified binary name.

protected final Class<?>
loadClassOrNull(String cn)

A variation of loadClass to load a class with the specified binary name.

public void
loadModule(ModuleReference mref)

Register a module this class loader.

private ModuleReader
moduleReaderFor(ModuleReference mref)

Returns the ModuleReader for the given module, creating it if needed.

pack-priv static Map<String, ?>
packageToModule()

Invoked by ArchivedClassLoaders to archive the package-to-module map.

private void
resetArchivedStates()

Hides java.security.SecureClassLoader.resetArchivedStates.

Called by the VM, during -Xshare:dump

pack-priv void
setClassPath(URLClassPath ucp)

Sets the class path, called to reset the class path during -Xshare:dump

Inherited from java.security.SecureClassLoader:
defineClassdefineClassgetPermissions

Field Detail

moduleToReaderback to summary
private final Map<ModuleReference, ModuleReader> moduleToReader
nameToModuleback to summary
private final Map<String, ModuleReference> nameToModule
packageToModuleback to summary
private static final Map<String, BuiltinClassLoader.LoadedModule> packageToModule
parentback to summary
private final BuiltinClassLoader parent

Hides java.lang.ClassLoader.parent.

resourceCacheback to summary
private volatile SoftReference<Map<String, List<URL>>> resourceCache
ucpback to summary
private URLClassPath ucp
Annotations
@Stable

Constructor Detail

BuiltinClassLoaderback to summary
pack-priv BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp)

Create a new instance.

Method Detail

appendClassPathback to summary
pack-priv void appendClassPath(String path)

Appends to the given file path to the class path.

defineClassback to summary
private Class<?> defineClass(String cn, BuiltinClassLoader.LoadedModule loadedModule)

Defines the given binary class name to the VM, loading the class bytes from the given module.

Returns:Class<?>

the resulting Class or null if an I/O error occurs

defineClassback to summary
private Class<?> defineClass(String cn, Resource res) throws IOException

Defines the given binary class name to the VM, loading the class bytes via the given Resource object.

Returns:Class<?>

the resulting Class

Exceptions
IOException:
if reading the resource fails
SecurityException:
if there is a sealing violation (JAR spec)
defineOrCheckPackageback to summary
protected Package defineOrCheckPackage(String pn, Manifest man, URL url)

Defines a package in this ClassLoader. If the package is already defined then its sealing needs to be checked if sealed by the legacy sealing mechanism.

Exceptions
SecurityException:
if there is a sealing violation (JAR spec)
definePackageback to summary
private Package definePackage(String pn, Manifest man, URL url)

Defines a new package in this ClassLoader. The attributes in the specified Manifest are used to get the package version and sealing information.

Exceptions
IllegalArgumentException:
if the package name duplicates an existing package either in this class loader or one of its ancestors
SecurityException:
if the package name is untrusted in the manifest
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 a module. This method returns null if the class cannot be found or not defined in the specified module.

Annotations
@Override
findClassInModuleOrNullback to summary
private Class<?> findClassInModuleOrNull(BuiltinClassLoader.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

findClassOnClassPathOrNullback to summary
private Class<?> findClassOnClassPathOrNull(String cn)

Finds the class with the specified binary name on the class path.

Returns:Class<?>

the resulting Class or null if not found

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

Finds the candidate loaded 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.

findLoadedModuleback to summary
private BuiltinClassLoader.LoadedModule findLoadedModule(String mn, String cn)

Finds the candidate loaded module for the given class name in the named module. Returns null if the named module is not defined to this class loader or does not contain the API package for the class.

findMiscResourceback to summary
private List<URL> findMiscResource(String name) throws IOException

Returns the list of URLs to a "miscellaneous" resource in modules defined to this loader. A miscellaneous resource is not in a module package, e.g. META-INF/services/p.S. The cache used by this method avoids repeated searching of all modules.

findModuleback to summary
protected ModuleReference findModule(String name)

Returns the ModuleReference for the named module defined to this class loader; or null if not defined.

Parameters
name:String

The name of the module to find

findResourceback to summary
public 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.

Finds a resource with the given name in the modules defined to this class loader or its class path.

Annotations
@Override
findResourceback to summary
private URL findResource(ModuleReference mref, String name) throws IOException

Returns the URL to a resource in a module or null if not found.

findResourceAsStreamback to summary
public InputStream findResourceAsStream(String mn, String name) throws IOException

Returns an input stream to a resource of the given name in a module defined to this class loader.

findResourceOnClassPathback to summary
private URL findResourceOnClassPath(String name)

Returns a URL to a resource on the class path.

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

Overrides java.lang.ClassLoader.findResources.

Returns an enumeration of URL objects to all the resources with the given name in modules defined to this class loader or on the class path of this loader.

Annotations
@Override
Exceptions
IOException:

Doc from java.lang.ClassLoader.findResources.

If I/O errors occur

findResourcesOnClassPathback to summary
private Enumeration<URL> findResourcesOnClassPath(String name)

Returns the URLs of all resources of the given name on the class path.

getAndVerifyPackageback to summary
private Package getAndVerifyPackage(String pn, Manifest man, URL url)

Gets the Package with the specified package name. If defined then verifies it against the manifest and code source.

Exceptions
SecurityException:
if there is a sealing violation (JAR spec)
hasClassPathback to summary
pack-priv boolean hasClassPath()

Returns true if there is a class path associated with this class loader.

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.

isSealedback to summary
private boolean isSealed(String pn, Manifest man)

Returns true if the specified package name is sealed according to the given manifest.

Exceptions
SecurityException:
if the package name is untrusted in the manifest
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

loadClassOrNullback to summary
protected Class<?> loadClassOrNull(String cn, boolean resolve)

A variation of loadClass to load a class with the specified binary name. This method returns null when the class is not found.

loadClassOrNullback to summary
protected final Class<?> loadClassOrNull(String cn)

A variation of loadClass to load a class with the specified binary name. This method returns null when the class is not found.

loadModuleback to summary
public void loadModule(ModuleReference mref)

Register a module this class loader. This has the effect of making the types in the module visible.

moduleReaderForback to summary
private ModuleReader moduleReaderFor(ModuleReference mref)

Returns the ModuleReader for the given module, creating it if needed.

packageToModuleback to summary
pack-priv static Map<String, ?> packageToModule()

Invoked by ArchivedClassLoaders to archive the package-to-module map.

resetArchivedStatesback to summary
private void resetArchivedStates()

Hides java.security.SecureClassLoader.resetArchivedStates.

Doc from java.security.SecureClassLoader.resetArchivedStates.

Called by the VM, during -Xshare:dump

setClassPathback to summary
pack-priv void setClassPath(URLClassPath ucp)

Sets the class path, called to reset the class path during -Xshare:dump

jdk.internal.loader back to summary

private Class BuiltinClassLoader.LoadedModule

extends Object
Class Inheritance

A module defined/loaded by a built-in class loader. A LoadedModule encapsulates a ModuleReference along with its CodeSource URL to avoid needing to create this URL when defining classes.

Field Summary

Modifier and TypeField and Description
private URL
private final BuiltinClassLoader
private final ModuleReference
private final URI

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv URL
private URL
pack-priv BuiltinClassLoader
pack-priv ModuleReference
mref()

pack-priv String
name()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

codeSourceURLback to summary
private URL codeSourceURL
Annotations
@Stable
loaderback to summary
private final BuiltinClassLoader loader
mrefback to summary
private final ModuleReference mref
uriback to summary
private final URI uri

Constructor Detail

LoadedModuleback to summary
pack-priv LoadedModule(BuiltinClassLoader loader, ModuleReference mref)

Method Detail

codeSourceURLback to summary
pack-priv URL codeSourceURL()
createURLback to summary
private URL createURL(URI uri)
loaderback to summary
pack-priv BuiltinClassLoader loader()
mrefback to summary
pack-priv ModuleReference mref()
nameback to summary
pack-priv String name()
jdk.internal.loader back to summary

private Class BuiltinClassLoader.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