Class InstructionSequenceReplacer

java.lang.Object
proguard.classfile.editor.InstructionSequenceReplacer
All Implemented Interfaces:
ConstantVisitor, InstructionVisitor

public class InstructionSequenceReplacer extends 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:
  • Field Details

  • Constructor Details

    • 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

      @Deprecated public InstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher, Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor)
      Creates a new InstructionSequenceReplacer.
      Parameters:
      instructionSequenceMatcher - a suitable instruction sequence matcher.
      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 Details

    • visitAnyInstruction

      public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
      Description copied from interface: InstructionVisitor
      Visits any Instruction instance. The more specific default implementations of this interface delegate to this method.
      Specified by:
      visitAnyInstruction in interface InstructionVisitor
    • 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.
    • getInstructionSequenceMatcher

      protected InstructionSequenceMatcher getInstructionSequenceMatcher()
    • wasConstant

      protected boolean wasConstant(int argument)
    • matchedConstant

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

      protected int matchedArgument(int argument)
    • label

      public static InstructionSequenceReplacer.Label label()
      Creates a new label that can be used as a pseudo-instruction to mark a local offset. Its offset can be used as a branch target in replacement instructions (InstructionSequenceReplacer.Label.offset()).
    • catch_

      public static InstructionSequenceReplacer.Label catch_(int startOffset, int endOffset, int catchType)
      Creates a new catch instance that can be used as a pseudo-instruction to mark the start of an exception handler. Its offset can be used as a branch target in replacement instructions (InstructionSequenceReplacer.Label.offset()).