Package net.bytebuddy.dynamic.loading
Class InjectionClassLoader
java.lang.Object
java.lang.ClassLoader
net.bytebuddy.dynamic.loading.InjectionClassLoader
- Direct Known Subclasses:
ByteArrayClassLoader
,MultipleParentClassLoader
An injection class loader allows for the injection of a class after the class loader was created. Injection is only possible if this class loader is not sealed.
Important: Not sealing a class loader allows to break package encapsulation for anybody getting hold of a reference to this class loader.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
A class loading strategy for adding a type to an injection class loader. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicBoolean
Indicates if this class loader is sealed, i.e. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
InjectionClassLoader
(ClassLoader parent, boolean sealed) Creates a new injection class loader. -
Method Summary
Modifier and TypeMethodDescriptionClass
<?> defineClass
(String name, byte[] binaryRepresentation) Defines a new type to be loaded by this class loader.defineClasses
(Map<String, byte[]> typeDefinitions) Defines a group of types to be loaded by this class loader.doDefineClasses
(Map<String, byte[]> typeDefinitions) Defines a group of types to be loaded by this class loader.private static void
Registers class loader as parallel capable if possible.boolean
isSealed()
Returnstrue
if this class loader is sealed.boolean
seal()
Seals the class loader and returnstrue
if the class loader was not sealed previously.Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
sealed
Indicates if this class loader is sealed, i.e. forbids runtime injection.
-
-
Constructor Details
-
InjectionClassLoader
Creates a new injection class loader.- Parameters:
parent
- The class loader's parent.sealed
- Indicates if this class loader is sealed, i.e. forbids runtime injection.
-
-
Method Details
-
doRegisterAsParallelCapable
private static void doRegisterAsParallelCapable()Registers class loader as parallel capable if possible. -
isSealed
public boolean isSealed()Returnstrue
if this class loader is sealed.- Returns:
true
if this class loader is sealed.
-
seal
public boolean seal()Seals the class loader and returnstrue
if the class loader was not sealed previously.- Returns:
true
if the class loader was not sealed previously.
-
defineClass
Defines a new type to be loaded by this class loader.- Parameters:
name
- The name of the type.binaryRepresentation
- The type's binary representation.- Returns:
- The defined class or a previously defined class.
- Throws:
ClassNotFoundException
- If the class could not be loaded.
-
defineClasses
public Map<String,Class<?>> defineClasses(Map<String, byte[]> typeDefinitions) throws ClassNotFoundExceptionDefines a group of types to be loaded by this class loader. If this class loader is sealed, anIllegalStateException
is thrown.- Parameters:
typeDefinitions
- The types binary representations.- Returns:
- The mapping of defined classes or previously defined classes by their name.
- Throws:
ClassNotFoundException
- If the class could not be loaded.
-
doDefineClasses
protected abstract Map<String,Class<?>> doDefineClasses(Map<String, byte[]> typeDefinitions) throws ClassNotFoundExceptionDefines a group of types to be loaded by this class loader.- Parameters:
typeDefinitions
- The types binary representations.- Returns:
- The mapping of defined classes or previously defined classes by their name.
- Throws:
ClassNotFoundException
- If the class could not be loaded.
-