Package net.bytebuddy.dynamic.loading
Class ByteArrayClassLoader
java.lang.Object
java.lang.ClassLoader
net.bytebuddy.dynamic.loading.InjectionClassLoader
net.bytebuddy.dynamic.loading.ByteArrayClassLoader
- Direct Known Subclasses:
ByteArrayClassLoader.ChildFirst
A ClassLoader
that is capable of loading explicitly defined classes. The class loader will free
any binary resources once a class that is defined by its binary data is loaded. This class loader is thread safe since
the class loading mechanics are only called from synchronized context.
Note: Instances of this class loader return URLs for their represented class loaders with the bytebuddy schema. These URLs do not represent URIs as two classes with the same name yield identical URLs but might represents different byte arrays.
Note: Any class and package definition is performed using the creator's java.security.AccessControlContext
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
AByteArrayClassLoader
which applies child-first semantics for the given type definitions.protected class
An action for defining a located class that is not yet loaded.protected static enum
An enumeration without any elements.protected static interface
A package lookup strategy for locating a package by name.static enum
A persistence handler decides on whether the byte array that represents a loaded class is exposed by theClassLoader.getResourceAsStream(String)
method.protected static class
An enumeration that contains a single element.protected static interface
An engine for receiving a class loading lock when loading a class.Nested classes/interfaces inherited from class net.bytebuddy.dynamic.loading.InjectionClassLoader
InjectionClassLoader.Strategy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Object
The access control context to use for loading classes ornull
if this is not supported on the current VM.protected final ClassFilePostProcessor
The class file transformer to apply on loaded classes.private static final int
Indicates that an array should be included from its first index.private static final URL
Indicates that a URL does not exist to improve code readability.private static final ByteArrayClassLoader.PackageLookupStrategy
A strategy for locating a package by name.protected final PackageDefinitionStrategy
The package definer to be queried for package definitions.protected final ByteArrayClassLoader.PersistenceHandler
The persistence handler of this class loader.protected final ProtectionDomain
The protection domain to apply.protected static final ByteArrayClassLoader.SynchronizationStrategy.Initializable
The synchronization engine for the executing JVM.protected final ConcurrentMap
<String, byte[]> A mutable map of type names mapped to their binary representation.static final String
The schema for URLs that represent a class file of byte array class loaders. -
Constructor Summary
ConstructorsConstructorDescriptionByteArrayClassLoader
(ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, Map<String, byte[]> typeDefinitions) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, Map<String, byte[]> typeDefinitions, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, Map<String, byte[]> typeDefinitions, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor) Creates a new class loader for a given definition of classes.ByteArrayClassLoader
(ClassLoader parent, Map<String, byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler) Creates a new class loader for a given definition of classes. -
Method Summary
Modifier and TypeMethodDescriptiondoDefineClasses
(Map<String, byte[]> typeDefinitions) Defines a group of types to be loaded by this class loader.private Package
doGetPackage
(String name) Returns the package for a given name.private static <T> T
doPrivileged
(PrivilegedAction<T> action) A proxy forjava.security.AccessController#doPrivileged
that is activated if available.private static <T> T
doPrivileged
(PrivilegedAction<T> action, Object context) A proxy forjava.security.AccessController#doPrivileged
that is activated if available.private static void
Registers class loader as parallel capable if possible.protected Class
<?> protected URL
findResource
(String name) protected Enumeration
<URL> findResources
(String name) private static Object
A proxy forjava.security.AccessController#getContext
that is activated if available.static Map
<TypeDescription, Class<?>> load
(ClassLoader classLoader, Map<TypeDescription, byte[]> types) Loads a given set of class descriptions and their binary representations.static Map
<TypeDescription, Class<?>> load
(ClassLoader classLoader, Map<TypeDescription, byte[]> types, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, boolean forbidExisting, boolean sealed) Loads a given set of class descriptions and their binary representations.private static Object
Resolves a method handle in the scope of theByteArrayClassLoader
class.Methods inherited from class net.bytebuddy.dynamic.loading.InjectionClassLoader
defineClass, defineClasses, isSealed, seal
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, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
URL_SCHEMA
The schema for URLs that represent a class file of byte array class loaders.- See Also:
-
FROM_BEGINNING
private static final int FROM_BEGINNINGIndicates that an array should be included from its first index. Improves the source code readability.- See Also:
-
NO_URL
Indicates that a URL does not exist to improve code readability. -
PACKAGE_LOOKUP_STRATEGY
A strategy for locating a package by name. -
SYNCHRONIZATION_STRATEGY
protected static final ByteArrayClassLoader.SynchronizationStrategy.Initializable SYNCHRONIZATION_STRATEGYThe synchronization engine for the executing JVM. -
typeDefinitions
A mutable map of type names mapped to their binary representation. -
persistenceHandler
The persistence handler of this class loader. -
protectionDomain
The protection domain to apply. Might benull
when referencing the default protection domain. -
packageDefinitionStrategy
The package definer to be queried for package definitions. -
classFilePostProcessor
The class file transformer to apply on loaded classes. -
accessControlContext
The access control context to use for loading classes ornull
if this is not supported on the current VM.
-
-
Constructor Details
-
ByteArrayClassLoader
Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.sealed
-true
if this class loader is sealed.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, Map<String, byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.persistenceHandler
- The persistence handler of this class loader.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.sealed
-true
if this class loader is sealed.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.persistenceHandler
- The persistence handler of this class loader.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, Map<String, byte[]> typeDefinitions, @MaybeNull ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.protectionDomain
- The protection domain to apply wherenull
references an implicit protection domain.persistenceHandler
- The persistence handler of this class loader.packageDefinitionStrategy
- The package definer to be queried for package definitions.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, @MaybeNull ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.sealed
-true
if this class loader is sealed.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.protectionDomain
- The protection domain to apply wherenull
references an implicit protection domain.persistenceHandler
- The persistence handler of this class loader.packageDefinitionStrategy
- The package definer to be queried for package definitions.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, Map<String, byte[]> typeDefinitions, @MaybeNull ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.protectionDomain
- The protection domain to apply wherenull
references an implicit protection domain.persistenceHandler
- The persistence handler of this class loader.packageDefinitionStrategy
- The package definer to be queried for package definitions.classFilePostProcessor
- A post processor for class files to apply p
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull ClassLoader parent, boolean sealed, Map<String, byte[]> typeDefinitions, @MaybeNull ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor) Creates a new class loader for a given definition of classes.- Parameters:
parent
- TheClassLoader
that is the parent of this class loader.sealed
-true
if this class loader is sealed.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.protectionDomain
- The protection domain to apply wherenull
references an implicit protection domain.persistenceHandler
- The persistence handler of this class loader.packageDefinitionStrategy
- The package definer to be queried for package definitions.classFilePostProcessor
- A post processor for class files to apply p
-
-
Method Details
-
doRegisterAsParallelCapable
private static void doRegisterAsParallelCapable()Registers class loader as parallel capable if possible. -
doPrivileged
A proxy forjava.security.AccessController#doPrivileged
that is activated if available.- Type Parameters:
T
- The type of the action's resolved value.- Parameters:
action
- The action to execute from a privileged context.- Returns:
- The action's resolved value.
-
getContext
A proxy forjava.security.AccessController#getContext
that is activated if available.- Returns:
- The current access control context or
null
if the current VM does not support it.
-
doPrivileged
A proxy forjava.security.AccessController#doPrivileged
that is activated if available.- Type Parameters:
T
- The type of the action's resolved value.- Parameters:
action
- The action to execute from a privileged context.context
- The access control context ornull
if the current VM does not support it.- Returns:
- The action's resolved value.
-
methodHandle
Resolves a method handle in the scope of theByteArrayClassLoader
class.- Returns:
- A method handle for this class.
- Throws:
Exception
- If the method handle facility is not supported by the current virtual machine.
-
load
public static Map<TypeDescription,Class<?>> load(@MaybeNull ClassLoader classLoader, Map<TypeDescription, byte[]> types) Loads a given set of class descriptions and their binary representations.- Parameters:
classLoader
- The parent class loader.types
- The unloaded types to be loaded.- Returns:
- A map of the given type descriptions pointing to their loaded representations.
-
load
public static Map<TypeDescription,Class<?>> load(@MaybeNull ClassLoader classLoader, Map<TypeDescription, byte[]> types, @MaybeNull ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, boolean forbidExisting, boolean sealed) Loads a given set of class descriptions and their binary representations.- Parameters:
classLoader
- The parent class loader.types
- The unloaded types to be loaded.protectionDomain
- The protection domain to apply wherenull
references an implicit protection domain.persistenceHandler
- The persistence handler of the created class loader.packageDefinitionStrategy
- The package definer to be queried for package definitions.forbidExisting
-true
if the class loading should throw an exception if a class was already loaded by a parent class loader.sealed
-true
if the class loader should be sealed.- Returns:
- A map of the given type descriptions pointing to their loaded representations.
-
doDefineClasses
protected Map<String,Class<?>> doDefineClasses(Map<String, byte[]> typeDefinitions) throws ClassNotFoundExceptionDescription copied from class:InjectionClassLoader
Defines a group of types to be loaded by this class loader.- Specified by:
doDefineClasses
in classInjectionClassLoader
- 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.
-
findClass
- Overrides:
findClass
in classClassLoader
- Throws:
ClassNotFoundException
-
findResource
- Overrides:
findResource
in classClassLoader
-
findResources
- Overrides:
findResources
in classClassLoader
-
doGetPackage
Returns the package for a given name.- Parameters:
name
- The name of the package.- Returns:
- A suitable package or
null
if no such package exists.
-