public final class AsmClassLoader
extends java.lang.ClassLoader
A class is loaded by invoking the loadClass(String, byte[])
method, passing it
the fully qualified name of the class, and the byte code array representing its class file.
TODO: enable class unloading by delegating to individual instances of this class, organized by key, which can be discarded when their loaded classes are no longer needed.
Modifier and Type | Class and Description |
---|---|
private static class |
AsmClassLoader.Data
Association between an assembled class' name and its class file data.
|
Modifier and Type | Field and Description |
---|---|
private static AsmClassLoader |
instance
Singleton instance
|
private java.util.Map<java.lang.String,java.lang.Class<?>> |
loaded
Cache of previously loaded, dynamic classes
|
private java.lang.ThreadLocal<AsmClassLoader.Data> |
local
Thread-local, temporary association of a class' name to its data
|
Constructor and Description |
---|
AsmClassLoader()
Default constructor.
|
AsmClassLoader(java.lang.ClassLoader parent)
Constructor which accepts a parent class loader.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Class<?> |
findClass(java.lang.String name)
Overrides the parent's implementation to find the class file data to first check the cache
of previously loaded classes, and if not found there, to load the dynamically generated
class with the given name.
|
static AsmClassLoader |
getInstance()
Get the singleton instance of this class loader.
|
java.lang.Class<?> |
loadClass(java.lang.String name,
byte[] code)
Store a piece of thread-local data which associates the given class name with the byte
array representing its class file.
|
private static void |
setInstance(AsmClassLoader loader)
Set the singleton instance of this class loader.
|
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
private static AsmClassLoader instance
private final java.lang.ThreadLocal<AsmClassLoader.Data> local
private final java.util.Map<java.lang.String,java.lang.Class<?>> loaded
public AsmClassLoader()
public AsmClassLoader(java.lang.ClassLoader parent)
Class.forName
).parent
- Parent class loader.public static AsmClassLoader getInstance()
private static void setInstance(AsmClassLoader loader)
loader
- Instance which will be stored as the singleton instance.java.lang.IllegalStateException
- if invoked more than once.public java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException
findClass
in class java.lang.ClassLoader
name
- Name of the class to be loaded.java.lang.ClassNotFoundException
- if no thread-local class file data is found, or if its name does not match the
given class name.public java.lang.Class<?> loadClass(java.lang.String name, byte[] code) throws java.lang.ClassNotFoundException
loadClass(String, boolean)
method,
then the thread-local data is immediately removed.name
- Name of the proxy class to be loaded.code
- Byte array representing the proxy's class file.java.lang.ClassNotFoundException
- if something unexpected goes horribly wrong with the
thread-local data mechanism.