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
ClassLoaderthat 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 Classes Modifier and Type Class Description static classByteArrayClassLoader.ChildFirstAByteArrayClassLoaderwhich applies child-first semantics for the given type definitions.protected classByteArrayClassLoader.ClassDefinitionActionAn action for defining a located class that is not yet loaded.protected static classByteArrayClassLoader.EmptyEnumerationAn enumeration without any elements.protected static interfaceByteArrayClassLoader.PackageLookupStrategyA package lookup strategy for locating a package by name.static classByteArrayClassLoader.PersistenceHandlerA persistence handler decides on whether the byte array that represents a loaded class is exposed by theClassLoader.getResourceAsStream(String)method.protected static classByteArrayClassLoader.SingletonEnumerationAn enumeration that contains a single element.protected static interfaceByteArrayClassLoader.SynchronizationStrategyAn 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 java.lang.ObjectaccessControlContextThe access control context to use for loading classes ornullif this is not supported on the current VM.protected ClassFilePostProcessorclassFilePostProcessorThe class file transformer to apply on loaded classes.private static intFROM_BEGINNINGIndicates that an array should be included from its first index.private static java.net.URLNO_URLIndicates that a URL does not exist to improve code readability.private static ByteArrayClassLoader.PackageLookupStrategyPACKAGE_LOOKUP_STRATEGYA strategy for locating a package by name.protected PackageDefinitionStrategypackageDefinitionStrategyThe package definer to be queried for package definitions.protected ByteArrayClassLoader.PersistenceHandlerpersistenceHandlerThe persistence handler of this class loader.protected java.security.ProtectionDomainprotectionDomainThe protection domain to apply.protected static ByteArrayClassLoader.SynchronizationStrategy.InitializableSYNCHRONIZATION_STRATEGYThe synchronization engine for the executing JVM.protected java.util.concurrent.ConcurrentMap<java.lang.String,byte[]>typeDefinitionsA mutable map of type names mapped to their binary representation.static java.lang.StringURL_SCHEMAThe schema for URLs that represent a class file of byte array class loaders.
-
Constructor Summary
Constructors Constructor Description ByteArrayClassLoader(java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions, java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions, java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions, java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions, java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor)Creates a new class loader for a given definition of classes.ByteArrayClassLoader(java.lang.ClassLoader parent, java.util.Map<java.lang.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 java.util.Map<java.lang.String,java.lang.Class<?>>doDefineClasses(java.util.Map<java.lang.String,byte[]> typeDefinitions)Defines a group of types to be loaded by this class loader.private java.lang.PackagedoGetPackage(java.lang.String name)Returns the package for a given name.private static <T> TdoPrivileged(java.security.PrivilegedAction<T> action)A proxy forjava.security.AccessController#doPrivilegedthat is activated if available.private static <T> TdoPrivileged(java.security.PrivilegedAction<T> action, java.lang.Object context)A proxy forjava.security.AccessController#doPrivilegedthat is activated if available.private static voiddoRegisterAsParallelCapable()Registers class loader as parallel capable if possible.protected java.lang.Class<?>findClass(java.lang.String name)protected java.net.URLfindResource(java.lang.String name)protected java.util.Enumeration<java.net.URL>findResources(java.lang.String name)private static java.lang.ObjectgetContext()A proxy forjava.security.AccessController#getContextthat is activated if available.static java.util.Map<TypeDescription,java.lang.Class<?>>load(java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types)Loads a given set of class descriptions and their binary representations.static java.util.Map<TypeDescription,java.lang.Class<?>>load(java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types, java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, boolean forbidExisting, boolean sealed)Loads a given set of class descriptions and their binary representations.static java.util.Map<TypeDescription,java.lang.Class<?>>load(java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types, java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassLoaderDecorator.Factory classLoaderDecoratorFactory, boolean forbidExisting, boolean sealed)Loads a given set of class descriptions and their binary representations.private static java.lang.ObjectmethodHandle()Resolves a method handle in the scope of theByteArrayClassLoaderclass.-
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 Detail
-
URL_SCHEMA
public static final java.lang.String URL_SCHEMA
The schema for URLs that represent a class file of byte array class loaders.- See Also:
- Constant Field Values
-
FROM_BEGINNING
private static final int FROM_BEGINNING
Indicates that an array should be included from its first index. Improves the source code readability.- See Also:
- Constant Field Values
-
NO_URL
@AlwaysNull private static final java.net.URL NO_URL
Indicates that a URL does not exist to improve code readability.
-
PACKAGE_LOOKUP_STRATEGY
private static final ByteArrayClassLoader.PackageLookupStrategy PACKAGE_LOOKUP_STRATEGY
A strategy for locating a package by name.
-
SYNCHRONIZATION_STRATEGY
protected static final ByteArrayClassLoader.SynchronizationStrategy.Initializable SYNCHRONIZATION_STRATEGY
The synchronization engine for the executing JVM.
-
typeDefinitions
protected final java.util.concurrent.ConcurrentMap<java.lang.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
@MaybeNull protected final java.security.ProtectionDomain protectionDomain
The protection domain to apply. Might benullwhen referencing the default protection domain.
-
packageDefinitionStrategy
protected final PackageDefinitionStrategy packageDefinitionStrategy
The package definer to be queried for package definitions.
-
classFilePostProcessor
protected final ClassFilePostProcessor classFilePostProcessor
The class file transformer to apply on loaded classes.
-
accessControlContext
@MaybeNull protected final java.lang.Object accessControlContext
The access control context to use for loading classes ornullif this is not supported on the current VM.
-
-
Constructor Detail
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat is the parent of this class loader.typeDefinitions- A map of fully qualified class names pointing to their binary representations.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat is the parent of this class loader.sealed-trueif this class loader is sealed.typeDefinitions- A map of fully qualified class names pointing to their binary representations.
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat 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 java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions, ByteArrayClassLoader.PersistenceHandler persistenceHandler)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat is the parent of this class loader.sealed-trueif 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 java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions, @MaybeNull java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat 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 wherenullreferences 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(@MaybeNull java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions, @MaybeNull java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat is the parent of this class loader.sealed-trueif this class loader is sealed.typeDefinitions- A map of fully qualified class names pointing to their binary representations.protectionDomain- The protection domain to apply wherenullreferences 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(@MaybeNull java.lang.ClassLoader parent, java.util.Map<java.lang.String,byte[]> typeDefinitions, @MaybeNull java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat 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 wherenullreferences an implicit protection domain.packageDefinitionStrategy- The package definer to be queried for package definitions.persistenceHandler- The persistence handler of this class loader.classFilePostProcessor- A post processor for class files to apply p
-
ByteArrayClassLoader
public ByteArrayClassLoader(@MaybeNull java.lang.ClassLoader parent, boolean sealed, java.util.Map<java.lang.String,byte[]> typeDefinitions, @MaybeNull java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassFilePostProcessor classFilePostProcessor)
Creates a new class loader for a given definition of classes.- Parameters:
parent- TheClassLoaderthat is the parent of this class loader.sealed-trueif this class loader is sealed.typeDefinitions- A map of fully qualified class names pointing to their binary representations.protectionDomain- The protection domain to apply wherenullreferences an implicit protection domain.packageDefinitionStrategy- The package definer to be queried for package definitions.persistenceHandler- The persistence handler of this class loader.classFilePostProcessor- A post processor for class files to apply p
-
-
Method Detail
-
doRegisterAsParallelCapable
private static void doRegisterAsParallelCapable()
Registers class loader as parallel capable if possible.
-
doPrivileged
@Enhance private static <T> T doPrivileged(java.security.PrivilegedAction<T> action)
A proxy forjava.security.AccessController#doPrivilegedthat 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
@MaybeNull @Enhance private static java.lang.Object getContext()
A proxy forjava.security.AccessController#getContextthat is activated if available.- Returns:
- The current access control context or
nullif the current VM does not support it.
-
doPrivileged
@Enhance private static <T> T doPrivileged(java.security.PrivilegedAction<T> action, @MaybeNull java.lang.Object context)
A proxy forjava.security.AccessController#doPrivilegedthat 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 ornullif the current VM does not support it.- Returns:
- The action's resolved value.
-
methodHandle
private static java.lang.Object methodHandle() throws java.lang.ExceptionResolves a method handle in the scope of theByteArrayClassLoaderclass.- Returns:
- A method handle for this class.
- Throws:
java.lang.Exception- If the method handle facility is not supported by the current virtual machine.
-
load
public static java.util.Map<TypeDescription,java.lang.Class<?>> load(@MaybeNull java.lang.ClassLoader classLoader, java.util.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 java.util.Map<TypeDescription,java.lang.Class<?>> load(@MaybeNull java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types, @MaybeNull java.security.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 wherenullreferences an implicit protection domain.persistenceHandler- The persistence handler of the created class loader.packageDefinitionStrategy- The package definer to be queried for package definitions.forbidExisting-trueif the class loading should throw an exception if a class was already loaded by a parent class loader.sealed-trueif the class loader should be sealed.- Returns:
- A map of the given type descriptions pointing to their loaded representations.
-
load
public static java.util.Map<TypeDescription,java.lang.Class<?>> load(@MaybeNull java.lang.ClassLoader classLoader, java.util.Map<TypeDescription,byte[]> types, @MaybeNull java.security.ProtectionDomain protectionDomain, ByteArrayClassLoader.PersistenceHandler persistenceHandler, PackageDefinitionStrategy packageDefinitionStrategy, ClassLoaderDecorator.Factory classLoaderDecoratorFactory, 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 wherenullreferences an implicit protection domain.persistenceHandler- The persistence handler of the created class loader.packageDefinitionStrategy- The package definer to be queried for package definitions.classLoaderDecoratorFactory- The class loader decorator factory to use.forbidExisting-trueif the class loading should throw an exception if a class was already loaded by a parent class loader.sealed-trueif the class loader should be sealed.- Returns:
- A map of the given type descriptions pointing to their loaded representations.
-
doDefineClasses
protected java.util.Map<java.lang.String,java.lang.Class<?>> doDefineClasses(java.util.Map<java.lang.String,byte[]> typeDefinitions) throws java.lang.ClassNotFoundExceptionDescription copied from class:InjectionClassLoaderDefines a group of types to be loaded by this class loader.- Specified by:
doDefineClassesin classInjectionClassLoader- Parameters:
typeDefinitions- The types binary representations.- Returns:
- The mapping of defined classes or previously defined classes by their name.
- Throws:
java.lang.ClassNotFoundException- If the class could not be loaded.
-
findClass
protected java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException- Overrides:
findClassin classjava.lang.ClassLoader- Throws:
java.lang.ClassNotFoundException
-
findResource
@MaybeNull protected java.net.URL findResource(java.lang.String name)
- Overrides:
findResourcein classjava.lang.ClassLoader
-
findResources
protected java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
- Overrides:
findResourcesin classjava.lang.ClassLoader
-
doGetPackage
@MaybeNull private java.lang.Package doGetPackage(java.lang.String name)
Returns the package for a given name.- Parameters:
name- The name of the package.- Returns:
- A suitable package or
nullif no such package exists.
-
-