Package org.dellroad.stuff.java
Class MemoryClassLoader
java.lang.Object
java.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
org.dellroad.stuff.java.MemoryClassLoader
- All Implemented Interfaces:
Closeable
,AutoCloseable
An
URLClassLoader
that maintains an updatable cache of class files (as byte[]
arrays)
in memory from which classes may be loaded.
The internal cache of classes is initially empty. Binary class files may be added (or removed) via
putClass()
, and retrieved via getClass()
. The byte[]
arrays passed to and from these methods are copied to ensure immutability.
Classes defined by this loader are assigned URL
s that look like memory:/com.example.MyClass
.
This class is thread safe and parallel capable.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected Class<?>
findResource
(String resourceName) findResources
(String resourceName) byte[]
Get the class from this loader that was previously added viaputClass()
, if any.void
Add a class to this loader, making it available for class resolution.Methods inherited from class java.net.URLClassLoader
close, definePackage, getPermissions, getResourceAsStream, getURLs, newInstance, newInstance
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
MEMORY_URL_SCHEME
- See Also:
-
-
Constructor Details
-
MemoryClassLoader
public MemoryClassLoader()Default constructor.Uses the current thread's context loader as the parent loader.
-
MemoryClassLoader
Constructor.- Parameters:
parent
- parent class loader, possibly null
-
-
Method Details
-
putClass
Add a class to this loader, making it available for class resolution.If an existing class already exists under
className
, it will be replaced. Theclassbytes
array is copied to ensure immutability.If
classbytes
is null, any existing class will be removed.- Parameters:
className
- Java class nameclassbytes
- Java class file data- Throws:
IllegalArgumentException
- if either parameter is null
-
getClass
Get the class from this loader that was previously added viaputClass()
, if any.The returned array is a copy to ensure immutability.
- Parameters:
className
- Java class name- Returns:
- class file previously added under the name
className
, or null if none exists
-
addURL
- Overrides:
addURL
in classURLClassLoader
-
findClass
- Overrides:
findClass
in classURLClassLoader
- Throws:
ClassNotFoundException
-
findResource
- Overrides:
findResource
in classURLClassLoader
-
findResources
- Overrides:
findResources
in classURLClassLoader
- Throws:
IOException
-