Class 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 java.security.AccessControlContext.

  • Field Details

    • URL_SCHEMA

      public static final String URL_SCHEMA
      The schema for URLs that represent a class file of byte array class loaders.
      See Also:
    • 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

      @MaybeNull protected final ProtectionDomain protectionDomain
      The protection domain to apply. Might be null when 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 Object accessControlContext
      The access control context to use for loading classes or null if this is not supported on the current VM.
  • Constructor Details

    • ByteArrayClassLoader

      public ByteArrayClassLoader(@MaybeNull ClassLoader parent, Map<String,byte[]> typeDefinitions)
      Creates a new class loader for a given definition of classes.
      Parameters:
      parent - The ClassLoader 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 - The ClassLoader 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 - The ClassLoader 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 - The ClassLoader 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 - The ClassLoader 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 where null 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 - The ClassLoader 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 where null 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 - The ClassLoader 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 where null 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 - The ClassLoader 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 where null 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

    • 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 where null 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.
    • load

      public static Map<TypeDescription, Class<?>> load(@MaybeNull ClassLoader classLoader, Map<TypeDescription, byte[]> types, @MaybeNull 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 where null references 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 - 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 class InjectionClassLoader
      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 class ClassLoader
      Throws:
      ClassNotFoundException
    • findResource

      @MaybeNull protected URL findResource(String name)
      Overrides:
      findResource in class ClassLoader
    • findResources

      protected Enumeration<URL> findResources(String name)
      Overrides:
      findResources in class ClassLoader