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
public class ByteArrayClassLoader extends InjectionClassLoader
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
AccessControlContext
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ByteArrayClassLoader.ChildFirst
AByteArrayClassLoader
which applies child-first semantics for the given type definitions.protected class
ByteArrayClassLoader.ClassDefinitionAction
An action for defining a located class that is not yet loaded.protected static class
ByteArrayClassLoader.EmptyEnumeration
An enumeration without any elements.protected static interface
ByteArrayClassLoader.PackageLookupStrategy
A package lookup strategy for locating a package by name.static class
ByteArrayClassLoader.PersistenceHandler
A persistence handler decides on whether the byte array that represents a loaded class is exposed by theClassLoader.getResourceAsStream(String)
method.protected static class
ByteArrayClassLoader.SingletonEnumeration
An enumeration that contains a single element.protected static interface
ByteArrayClassLoader.SynchronizationStrategy
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
Fields Modifier and Type Field Description protected AccessControlContext
accessControlContext
The access control context to use for loading classes.protected ClassFileTransformer
classFileTransformer
The class file transformer to apply on loaded classes.protected PackageDefinitionStrategy
packageDefinitionStrategy
The package definer to be queried for package definitions.protected ByteArrayClassLoader.PersistenceHandler
persistenceHandler
The persistence handler of this class loader.protected ProtectionDomain
protectionDomain
The protection domain to apply.protected static ByteArrayClassLoader.SynchronizationStrategy.Initializable
SYNCHRONIZATION_STRATEGY
The synchronization engine for the executing JVM.protected ConcurrentMap<String,byte[]>
typeDefinitions
A mutable map of type names mapped to their binary representation.static String
URL_SCHEMA
The schema for URLs that represent a class file of byte array class loaders.
-
Constructor Summary
Constructors Constructor Description ByteArrayClassLoader(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, ClassFileTransformer classFileTransformer)
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, ClassFileTransformer classFileTransformer)
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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Map<String,Class<?>>
doDefineClasses(Map<String,byte[]> typeDefinitions)
Defines a group of types to be loaded by this class loader.protected Class<?>
findClass(String name)
protected URL
findResource(String name)
protected Enumeration<URL>
findResources(String name)
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.-
Methods inherited from class net.bytebuddy.dynamic.loading.InjectionClassLoader
defineClass, defineClasses, isSealed
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Field Detail
-
URL_SCHEMA
public static final String URL_SCHEMA
The schema for URLs that represent a class file of byte array class loaders.- See Also:
- Constant Field Values
-
SYNCHRONIZATION_STRATEGY
protected static final ByteArrayClassLoader.SynchronizationStrategy.Initializable SYNCHRONIZATION_STRATEGY
The synchronization engine for the executing JVM.
-
typeDefinitions
protected final ConcurrentMap<String,byte[]> typeDefinitions
A mutable map of type names mapped to their binary representation.
-
persistenceHandler
protected final ByteArrayClassLoader.PersistenceHandler persistenceHandler
The persistence handler of this class loader.
-
protectionDomain
protected final ProtectionDomain protectionDomain
The protection domain to apply. Might benull
when referencing the default protection domain.
-
packageDefinitionStrategy
protected final PackageDefinitionStrategy packageDefinitionStrategy
The package definer to be queried for package definitions.
-
classFileTransformer
protected final ClassFileTransformer classFileTransformer
The class file transformer to apply on loaded classes.
-
accessControlContext
protected final AccessControlContext accessControlContext
The access control context to use for loading classes.
-
-
Constructor Detail
-
ByteArrayClassLoader
public ByteArrayClassLoader(ClassLoader parent, 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.typeDefinitions
- A map of fully qualified class names pointing to their binary representations.
-
ByteArrayClassLoader
public ByteArrayClassLoader(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(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(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(ClassLoader parent, Map<String,byte[]> typeDefinitions, 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.packageDefinitionStrategy
- The package definer to be queried for package definitions.persistenceHandler
- The persistence handler of this class loader.
-
ByteArrayClassLoader
public 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.- 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.packageDefinitionStrategy
- The package definer to be queried for package definitions.persistenceHandler
- The persistence handler of this class loader.
-
ByteArrayClassLoader
public ByteArrayClassLoader(ClassLoader parent, Map<String,byte[]> typeDefinitions, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFileTransformer classFileTransformer)
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.packageDefinitionStrategy
- The package definer to be queried for package definitions.persistenceHandler
- The persistence handler of this class loader.classFileTransformer
- The class file transformer to apply on loaded classes.
-
ByteArrayClassLoader
public ByteArrayClassLoader(ClassLoader parent, boolean sealed, Map<String,byte[]> typeDefinitions, ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFileTransformer classFileTransformer)
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.packageDefinitionStrategy
- The package definer to be queried for package definitions.persistenceHandler
- The persistence handler of this class loader.classFileTransformer
- The class file transformer to apply on loaded classes.
-
-
Method Detail
-
load
public static Map<TypeDescription,Class<?>> load(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(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.- 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 ClassNotFoundException
Description 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
protected Class<?> findClass(String name) throws ClassNotFoundException
- Overrides:
findClass
in classClassLoader
- Throws:
ClassNotFoundException
-
findResource
protected URL findResource(String name)
- Overrides:
findResource
in classClassLoader
-
findResources
protected Enumeration<URL> findResources(String name)
- Overrides:
findResources
in classClassLoader
-
-