Class InjectionClassLoader

  • Direct Known Subclasses:
    ByteArrayClassLoader, MultipleParentClassLoader

    public abstract class InjectionClassLoader
    extends ClassLoader

    An injection class loader allows for the injection of a class after the class loader was created. Injection is only possible if this class loader is not sealed.

    Important: Not sealing a class loader allows to break package encapsulation for anybody getting hold of a reference to this class loader.

    • Constructor Detail

      • InjectionClassLoader

        protected InjectionClassLoader​(ClassLoader parent,
                                       boolean sealed)
        Creates a new injection class loader.
        Parameters:
        parent - The class loader's parent.
        sealed - Indicates if this class loader is sealed, i.e. forbids runtime injection.
    • Method Detail

      • isSealed

        public boolean isSealed()
        Returns true if this class loader is sealed.
        Returns:
        true if this class loader is sealed.
      • defineClass

        public Class<?> defineClass​(String name,
                                    byte[] binaryRepresentation)
                             throws ClassNotFoundException
        Defines a new type to be loaded by this class loader.
        Parameters:
        name - The name of the type.
        binaryRepresentation - The type's binary representation.
        Returns:
        The defined class or a previously defined class.
        Throws:
        ClassNotFoundException - If the class could not be loaded.
      • defineClasses

        public Map<String,​Class<?>> defineClasses​(Map<String,​byte[]> typeDefinitions)
                                                 throws ClassNotFoundException
        Defines a group of types to be loaded by this class loader. If this class loader is sealed, an IllegalStateException is thrown.
        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.
      • doDefineClasses

        protected abstract Map<String,​Class<?>> doDefineClasses​(Map<String,​byte[]> typeDefinitions)
                                                               throws ClassNotFoundException
        Defines a group of types to be loaded by this class loader.
        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.