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 AccessControlContext.

    • 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
      • UNLOADED_TYPE

        private static final java.lang.Class<?> UNLOADED_TYPE
        Indicates a type that is not loaded.
      • NO_URL

        private static final java.net.URL NO_URL
        Indicates that a URL does not exist to improve code readability.
      • typeDefinitions

        protected final java.util.concurrent.ConcurrentMap<java.lang.String,​byte[]> typeDefinitions
        A mutable map of type names mapped to their binary representation.
      • protectionDomain

        protected final java.security.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.
      • classFileTransformer

        protected final java.lang.instrument.ClassFileTransformer classFileTransformer
        The class file transformer to apply on loaded classes.
      • accessControlContext

        protected final java.security.AccessControlContext accessControlContext
        The access control context to use for loading classes.
    • Constructor Detail

      • ByteArrayClassLoader

        public ByteArrayClassLoader​(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 - 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​(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 - 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​(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 - 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​(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 - 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​(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.
        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.
        packageDefinitionStrategy - The package definer to be queried for package definitions.
        persistenceHandler - The persistence handler of this class loader.
      • ByteArrayClassLoader

        public 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.
        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.
        packageDefinitionStrategy - The package definer to be queried for package definitions.
        persistenceHandler - The persistence handler of this class loader.
      • ByteArrayClassLoader

        public ByteArrayClassLoader​(java.lang.ClassLoader parent,
                                    java.util.Map<java.lang.String,​byte[]> typeDefinitions,
                                    java.security.ProtectionDomain protectionDomain,
                                    ByteArrayClassLoader.PersistenceHandler persistenceHandler,
                                    PackageDefinitionStrategy packageDefinitionStrategy,
                                    java.lang.instrument.ClassFileTransformer classFileTransformer)
        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.
        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​(java.lang.ClassLoader parent,
                                    boolean sealed,
                                    java.util.Map<java.lang.String,​byte[]> typeDefinitions,
                                    java.security.ProtectionDomain protectionDomain,
                                    ByteArrayClassLoader.PersistenceHandler persistenceHandler,
                                    PackageDefinitionStrategy packageDefinitionStrategy,
                                    java.lang.instrument.ClassFileTransformer classFileTransformer)
        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.
        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

      • methodHandle

        private static java.lang.Object methodHandle()
                                              throws java.lang.Exception
        Resolves a method handle in the scope of the ByteArrayClassLoader class.
        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​(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​(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.
        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.
      • doDefineClasses

        protected java.util.Map<java.lang.String,​java.lang.Class<?>> doDefineClasses​(java.util.Map<java.lang.String,​byte[]> typeDefinitions)
                                                                                    throws java.lang.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:
        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:
        findClass in class java.lang.ClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • findResource

        protected java.net.URL findResource​(java.lang.String name)
        Overrides:
        findResource in class java.lang.ClassLoader
      • findResources

        protected java.util.Enumeration<java.net.URL> findResources​(java.lang.String name)
        Overrides:
        findResources in class java.lang.ClassLoader
      • doGetPackage

        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 null if no such package exists.