Class InitializerEditor

  • All Implemented Interfaces:
    AttributeVisitor

    public class InitializerEditor
    extends java.lang.Object
    implements AttributeVisitor
    This editor allows to build and/or edit classes (ProgramClass instances). It provides methods to easily add fields and methods to classes.
    • Constructor Detail

      • InitializerEditor

        public InitializerEditor​(ProgramClass programClass)
        Creates a new InitializerEditor for the given class.
        Parameters:
        programClass - The class to be edited.
    • Method Detail

      • addStaticInitializerInstructions

        public void addStaticInitializerInstructions​(boolean mergeIntoExistingInitializer,
                                                     InitializerEditor.CodeBuilder codeBuilder)
        Adds the specified static initializer instructions to the edited class. If the class already contains a static initializer, the new instructions will be appended to the existing initializer.
        Parameters:
        mergeIntoExistingInitializer - Indicates whether the instructions should be added to the existing static initializer (if it exists), or if a new method should be created, which is then called from the existing initializer.
        codeBuilder - The provider of a builder to add instructions. This functional interface can conveniently be implemented as a closure.
      • addStaticInitializerInstructions

        public void addStaticInitializerInstructions​(boolean mergeIntoExistingInitializer,
                                                     Instruction[] instructions)
        Adds the given static initializer instructions to the edited class. If the class already contains a static initializer, the new instructions will be appended to the existing initializer.
        Parameters:
        mergeIntoExistingInitializer - Indicates whether the instructions should be added to the existing static initializer (if it exists), or if a new method should be created, which is then called from the existing initializer.
        instructions - The instructions to be added.
      • addInitializerInstructions

        public void addInitializerInstructions​(InitializerEditor.CodeBuilder codeBuilder)
        Adds the specified initialization instructions to the edited class.
        • If the class doesn't contain a constructor yet, it will be created, and the instructions will be added to this constructor.
        • If there is a single super-calling constructor, the instructions will be added at the beginning of its code attribute.
        • If there are multiple super-calling constructors, a new private parameterless helper method will be created, to which the instructions will be added. An invocation to this new method will be added at the beginning of the code attribute of all super-calling constructors.
        Parameters:
        codeBuilder - The provider of a builder to add instructions. This functional interface can conveniently be implemented as a closure.
      • addInitializerInstructions

        public void addInitializerInstructions​(Instruction[] instructions)
        Adds the given initialization instructions to the edited class.
        • If the class doesn't contain a constructor yet, it will be created, and the instructions will be added to this constructor.
        • If there is a single super-calling constructor, the instructions will be added at the beginning of its code attribute.
        • If there are multiple super-calling constructors, a new private parameterless helper method will be created, to which the instructions will be added. An invocation to this new method will be added at the beginning of the code attribute of all super-calling constructors.
        Parameters:
        instructions - The instructions to be added.
      • visitAnyAttribute

        public void visitAnyAttribute​(Clazz clazz,
                                      Attribute attribute)
        Description copied from interface: AttributeVisitor
        Visits any Attribute instance. The more specific default implementations of this interface delegate to this method.
        Specified by:
        visitAnyAttribute in interface AttributeVisitor