Class InstructionSequenceReplacer

  • All Implemented Interfaces:
    ConstantVisitor, InstructionVisitor

    public class InstructionSequenceReplacer
    extends java.lang.Object
    implements InstructionVisitor, ConstantVisitor
    This InstructionVisitor replaces a given pattern instruction sequence by another given replacement instruction sequence. The arguments of the instruction sequences can be wildcards that are matched and replaced.

    The class also supports labels (label()) and exception handlers (catch_(int,int,int)) in replacement sequences. They provide local branch offsets inside the replacement sequences (InstructionSequenceReplacer.Label.offset()). For example, creating a replacement sequence with the help of InstructionSequenceBuilder:

         final InstructionSequenceReplacer.Label TRY_START = InstructionSequenceReplacer.label();
         final InstructionSequenceReplacer.Label TRY_END   = InstructionSequenceReplacer.label();
         final InstructionSequenceReplacer.Label CATCH_END = InstructionSequenceReplacer.label();
    
         final InstructionSequenceReplacer.Label CATCH_EXCEPTION =
             InstructionSequenceReplacer.catch_(TRY_START.offset(),
                                                TRY_END.offset(),
                                                constantPoolEditor.addClassConstant("java/lang/Exception", null));
    
         Instructions[] replacementInstructions = builder
             .label(TRY_START)
             ......
             .label(TRY_END)
             .goto_(CATCH_END.offset())
             .catch_(CATCH_EXCEPTION)
             ......
             .athrow()
             .label(CATCH_END)
             ......
             .instructions();
     
    See Also:
    InstructionSequenceMatcher
    • Constructor Detail

      • InstructionSequenceReplacer

        public InstructionSequenceReplacer​(Constant[] patternConstants,
                                           Instruction[] patternInstructions,
                                           Constant[] replacementConstants,
                                           Instruction[] replacementInstructions,
                                           BranchTargetFinder branchTargetFinder,
                                           CodeAttributeEditor codeAttributeEditor)
        Creates a new InstructionSequenceReplacer.
        Parameters:
        patternConstants - any constants referenced by the pattern instructions.
        patternInstructions - the pattern instruction sequence.
        replacementConstants - any constants referenced by the replacement instructions.
        replacementInstructions - the replacement instruction sequence.
        branchTargetFinder - a branch target finder that has been initialized to indicate branch targets in the visited code.
        codeAttributeEditor - a code editor that can be used for accumulating changes to the code.
      • InstructionSequenceReplacer

        public InstructionSequenceReplacer​(Constant[] patternConstants,
                                           Instruction[] patternInstructions,
                                           Constant[] replacementConstants,
                                           Instruction[] replacementInstructions,
                                           BranchTargetFinder branchTargetFinder,
                                           CodeAttributeEditor codeAttributeEditor,
                                           InstructionVisitor extraInstructionVisitor)
        Creates a new InstructionSequenceReplacer.
        Parameters:
        patternConstants - any constants referenced by the pattern instructions.
        patternInstructions - the pattern instruction sequence.
        replacementConstants - any constants referenced by the replacement instructions.
        replacementInstructions - the replacement instruction sequence.
        branchTargetFinder - a branch target finder that has been initialized to indicate branch targets in the visited code.
        codeAttributeEditor - a code editor that can be used for accumulating changes to the code.
        extraInstructionVisitor - an optional extra visitor for all deleted load instructions.
      • InstructionSequenceReplacer

        protected InstructionSequenceReplacer​(InstructionSequenceMatcher instructionSequenceMatcher,
                                              Constant[] replacementConstants,
                                              Instruction[] replacementInstructions,
                                              BranchTargetFinder branchTargetFinder,
                                              CodeAttributeEditor codeAttributeEditor,
                                              InstructionVisitor extraInstructionVisitor)
        Creates a new InstructionSequenceReplacer.
        Parameters:
        instructionSequenceMatcher - a suitable instruction sequence matcher.
        replacementConstants - any constants referenced by the replacement instructions.
        replacementInstructions - the replacement instruction sequence.
        branchTargetFinder - a branch target finder that has been initialized to indicate branch targets in the visited code.
        codeAttributeEditor - a code editor that can be used for accumulating changes to the code.
        extraInstructionVisitor - an optional extra visitor for all deleted load instructions.
    • Method Detail

      • matchedArgument

        protected int matchedArgument​(Clazz clazz,
                                      Method method,
                                      CodeAttribute codeAttribute,
                                      int offset,
                                      int argument)
        Returns the matched argument for the given pattern argument.
      • matchedArgument

        protected int matchedArgument​(Clazz clazz,
                                      int argument)
        Returns the matched argument for the given pattern argument.
      • matchedConstantIndex

        protected int matchedConstantIndex​(ProgramClass programClass,
                                           int constantIndex)
        Returns the matched or newly created constant index for the given pattern constant index.
      • matchedBranchOffset

        protected int matchedBranchOffset​(int offset,
                                          int branchOffset)
        Returns the value of the specified matched branch offset.
      • matchedJumpOffsets

        protected int[] matchedJumpOffsets​(int offset,
                                           int[] jumpOffsets)
        Returns the values of the specified matched jump offsets.
      • wasConstant

        protected boolean wasConstant​(int argument)
      • matchedConstant

        protected Constant matchedConstant​(ProgramClass programClass,
                                           int argument)
      • matchedArgument

        protected int matchedArgument​(int argument)