APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.sun.tools.javac.launcher

pack-priv final Class MemoryClassLoader

extends ClassLoader
Class Inheritance
Imports
java.io.ByteArrayInputStream, .FileNotFoundException, .IOError, .IOException, .InputStream, .UncheckedIOException, java.lang.module.ModuleDescriptor, java.net.MalformedURLException, .URI, .URISyntaxException, .URL, .URLConnection, .URLStreamHandler, java.nio.file.Files, java.security.CodeSigner, .CodeSource, .ProtectionDomain, java.util.ArrayList, .Collections, .Enumeration, .List, .Map, .NoSuchElementException, java.util.function.Function, jdk.internal.module.Resources

An in-memory classloader, that uses an in-memory cache of classes written by MemoryFileManager.

The classloader inverts the standard parent-delegation model, giving preference to classes defined in the source file before classes known to the parent (such as any like-named classes that might be found on the application class path.)

This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MemoryClassLoader.MemoryURLConnection

A URLConnection for use with URLs returned by MemoryClassLoader.getResource.

private class
MemoryClassLoader.MemoryURLStreamHandler

A URLStreamHandler for use with URLs returned by MemoryClassLoader.getResource.

Field Summary

Modifier and TypeField and Description
private final Function<String, byte[]>
private final ProtectionDomain
domain

A minimal protection domain, specifying a code source of the source file itself, used for classes found in the source file and defined by this loader.

private static final int
private URLStreamHandler
private final ModuleDescriptor
private final ClassLoader
parentClassLoader

The parent class loader instance.

private final ProgramDescriptor
private final String
private final Map<String, byte[]>
sourceFileClasses

The map of all classes found in the source file, indexed by Class#getName() binary name.

Constructor Summary

AccessConstructor and Description
pack-priv
MemoryClassLoader(Map<String, byte[]> sourceFileClasses, ClassLoader parentClassLoader, ModuleDescriptor moduleDescriptor, ProgramDescriptor programDescriptor, Function<String, byte[]> compileSourceFile)

Method Summary

Modifier and TypeMethod and Description
protected Class<?>
findClass(String name)

Overrides java.lang.ClassLoader.findClass.

Finds the class with the specified binary name.

protected Class<?>
findClass(String moduleName, String name)

Overrides java.lang.ClassLoader.findClass.

Finds the class with the given binary name in a module defined to this class loader.

private Class<?>
protected URL
findResource(String moduleName, String name)

Overrides java.lang.ClassLoader.findResource.

Returns a URL to a resource 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.

public URL

Returns:

a URL for the resource, or null if not found
getResource
(String
the name of the resource
name
)

Overrides java.lang.ClassLoader.getResource.

Override getResource to check for resources (i.e. class files) defined in the source file before checking resources in the parent class loader, including those on the class path.

public Enumeration<URL>

Returns:

an enumeration of the resources in this loader and in the application class loader
getResources
(String
the name of the resource
name
)

Overrides java.lang.ClassLoader.getResources.

Override getResources to check for resources (i.e. class files) defined in the source file before checking resources in the parent class loader, including those on the class path.

protected Class<?>

Returns:

the class
loadClass
(String
the name of the class to load
name
,
boolean
whether to resolve the class
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Override loadClass to check for classes defined in the source file before checking for classes in the parent class loader, including those on the classpath.

private String

Returns:

the binary name
toBinaryName
(String
the resource name
name
)

Converts a "resource name" (as used in the getResource* methods) to a binary name if the name identifies a class, or null otherwise.

private URL

Returns:

the URL of the resource, or null
toResourceInRootPath
(String
the resource name
name
)

Resolves a "resource name" (as used in the getResource* methods) to an existing file relative to source root path, or null otherwise.

Inherited from java.lang.ClassLoader:
clearAssertionStatusdefineClassdefineClassdefineClassdefineClassdefinePackagefindLibraryfindLoadedClassfindSystemClassgetClassLoadingLockgetDefinedPackagegetDefinedPackagesgetNamegetPackagegetPackagesgetParentgetPlatformClassLoadergetResourceAsStreamgetSystemClassLoadergetSystemResourcegetSystemResourceAsStreamgetSystemResourcesgetUnnamedModuleisRegisteredAsParallelCapableloadClassregisterAsParallelCapableresolveClassresourcessetClassAssertionStatussetDefaultAssertionStatussetPackageAssertionStatussetSigners

Field Detail

compileSourceFileback to summary
private final Function<String, byte[]> compileSourceFile
domainback to summary
private final ProtectionDomain domain

A minimal protection domain, specifying a code source of the source file itself, used for classes found in the source file and defined by this loader.

DOT_CLASS_LENGTHback to summary
private static final int DOT_CLASS_LENGTH
handlerback to summary
private URLStreamHandler handler
moduleDescriptorback to summary
private final ModuleDescriptor moduleDescriptor
parentClassLoaderback to summary
private final ClassLoader parentClassLoader

The parent class loader instance.

programDescriptorback to summary
private final ProgramDescriptor programDescriptor
PROTOCOLback to summary
private final String PROTOCOL
sourceFileClassesback to summary
private final Map<String, byte[]> sourceFileClasses

The map of all classes found in the source file, indexed by Class#getName() binary name.

Constructor Detail

MemoryClassLoaderback to summary
pack-priv MemoryClassLoader(Map<String, byte[]> sourceFileClasses, ClassLoader parentClassLoader, ModuleDescriptor moduleDescriptor, ProgramDescriptor programDescriptor, Function<String, byte[]> compileSourceFile)

Method Detail

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

Overrides java.lang.ClassLoader.findClass.

Doc from java.lang.ClassLoader.findClass.

Finds the class with the specified binary name. This method should be overridden by class loader implementations that follow the delegation model for loading classes, and will be invoked by the loadClass method after checking the parent class loader for the requested class.

Annotations
@Override
Exceptions
ClassNotFoundException:
If the class could not be found
findClassback to summary
protected Class<?> findClass(String moduleName, String name)

Overrides java.lang.ClassLoader.findClass.

Doc from java.lang.ClassLoader.findClass.

Finds the class with the given binary name in a module defined to this class loader. Class loader implementations that support loading from modules should override this method.

Annotations
@Override
findOrCompileClassback to summary
private Class<?> findOrCompileClass(String name)
findResourceback to summary
protected URL findResource(String moduleName, String name) throws IOException

Overrides java.lang.ClassLoader.findResource.

Doc from java.lang.ClassLoader.findResource.

Returns a URL to a resource in a module defined to this class loader. Class loader implementations that support loading from modules should override this method.

Annotations
@Override
Exceptions
IOException:
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)

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
getResourceback to summary
public URL getResource(String name)

Overrides java.lang.ClassLoader.getResource.

Override getResource to check for resources (i.e. class files) defined in the source file before checking resources in the parent class loader, including those on the class path.

getResourceAsStream(String name) calls this method, and so will have the same behavior.

Parameters
name:String

the name of the resource

Returns:URL

a URL for the resource, or null if not found

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

Overrides java.lang.ClassLoader.getResources.

Override getResources to check for resources (i.e. class files) defined in the source file before checking resources in the parent class loader, including those on the class path.

Parameters
name:String

the name of the resource

Returns:Enumeration<URL>

an enumeration of the resources in this loader and in the application class loader

Annotations
@Override
Exceptions
IOException:

Doc from java.lang.ClassLoader.getResources.

If I/O errors occur

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

Overrides java.lang.ClassLoader.loadClass.

Override loadClass to check for classes defined in the source file before checking for classes in the parent class loader, including those on the classpath.

loadClass(String name) calls this method, and so will have the same behavior.

Parameters
name:String

the name of the class to load

resolve:boolean

whether to resolve the class

Returns:Class<?>

the class

Annotations
@Override
Exceptions
ClassNotFoundException:
if the class is not found
toBinaryNameback to summary
private String toBinaryName(String name)

Converts a "resource name" (as used in the getResource* methods) to a binary name if the name identifies a class, or null otherwise.

Parameters
name:String

the resource name

Returns:String

the binary name

toResourceInRootPathback to summary
private URL toResourceInRootPath(String name)

Resolves a "resource name" (as used in the getResource* methods) to an existing file relative to source root path, or null otherwise.

Parameters
name:String

the resource name

Returns:URL

the URL of the resource, or null

com.sun.tools.javac.launcher back to summary

private Class MemoryClassLoader.MemoryURLConnection

extends URLConnection
Class Inheritance

A URLConnection for use with URLs returned by MemoryClassLoader.getResource.

Field Summary

Modifier and TypeField and Description
private final byte[]
private InputStream
Inherited from java.net.URLConnection:
allowUserInteractionconnecteddoInputdoOutputifModifiedSinceurluseCaches

Constructor Summary

AccessConstructor and Description
pack-priv
MemoryURLConnection(URL u, byte[] bytes)

Method Summary

Modifier and TypeMethod and Description
public void
connect()

Implements abstract java.net.URLConnection.connect.

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

public long
getContentLengthLong()

Overrides java.net.URLConnection.getContentLengthLong.

Returns the value of the content-length header field as a long.

public String
getContentType()

Overrides java.net.URLConnection.getContentType.

Returns the value of the content-type header field.

public InputStream
getInputStream()

Overrides java.net.URLConnection.getInputStream.

Returns an input stream that reads from this open connection.

Inherited from java.net.URLConnection:
addRequestPropertygetAllowUserInteractiongetConnectTimeoutgetContentgetContentgetContentEncodinggetContentLengthgetDategetDefaultAllowUserInteractiongetDefaultRequestPropertygetDefaultUseCachesgetDefaultUseCachesgetDoInputgetDoOutputgetExpirationgetFileNameMapgetHeaderFieldgetHeaderFieldgetHeaderFieldDategetHeaderFieldIntgetHeaderFieldKeygetHeaderFieldLonggetHeaderFieldsgetIfModifiedSincegetLastModifiedgetOutputStreamgetPermissiongetReadTimeoutgetRequestPropertiesgetRequestPropertygetURLgetUseCachesguessContentTypeFromNameguessContentTypeFromStreamsetAllowUserInteractionsetConnectTimeoutsetContentHandlerFactorysetDefaultAllowUserInteractionsetDefaultRequestPropertysetDefaultUseCachessetDefaultUseCachessetDoInputsetDoOutputsetFileNameMapsetIfModifiedSincesetReadTimeoutsetRequestPropertysetUseCachestoString

Field Detail

bytesback to summary
private final byte[] bytes
inback to summary
private InputStream in

Constructor Detail

MemoryURLConnectionback to summary
pack-priv MemoryURLConnection(URL u, byte[] bytes)

Method Detail

connectback to summary
public void connect() throws IOException

Implements abstract java.net.URLConnection.connect.

Doc from java.net.URLConnection.connect.

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored.

URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while opening the connection.
getContentLengthLongback to summary
public long getContentLengthLong()

Overrides java.net.URLConnection.getContentLengthLong.

Doc from java.net.URLConnection.getContentLengthLong.

Returns the value of the content-length header field as a long.

Annotations
@Override
getContentTypeback to summary
public String getContentType()

Overrides java.net.URLConnection.getContentType.

Doc from java.net.URLConnection.getContentType.

Returns the value of the content-type header field.

Annotations
@Override
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Overrides java.net.URLConnection.getInputStream.

Doc from java.net.URLConnection.getInputStream.

Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while creating the input stream.
com.sun.tools.javac.launcher back to summary

private Class MemoryClassLoader.MemoryURLStreamHandler

extends URLStreamHandler
Class Inheritance

A URLStreamHandler for use with URLs returned by MemoryClassLoader.getResource.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public URLConnection
openConnection(URL u)

Implements abstract java.net.URLStreamHandler.openConnection.

Opens a connection to the object referenced by the URL argument.

Inherited from java.net.URLStreamHandler:
equalsgetDefaultPortgetHostAddresshashCodehostsEqualopenConnectionparseURLsameFilesetURLsetURLtoExternalForm

Constructor Detail

MemoryURLStreamHandlerback to summary
private MemoryURLStreamHandler()

Method Detail

openConnectionback to summary
public URLConnection openConnection(URL u)

Implements abstract java.net.URLStreamHandler.openConnection.

Doc from java.net.URLStreamHandler.openConnection.

Opens a connection to the object referenced by the URL argument. This method should be overridden by a subclass.

If for the handler's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.

Annotations
@Override