Package proguard.classfile.editor
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis pseudo-instruction represents a label that marks an instruction offset, for use in the context of the sequence replacer only. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Constructor Summary
ConstructorsModifierConstructorDescriptionInstructionSequenceReplacer(Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor) Creates a new InstructionSequenceReplacer.InstructionSequenceReplacer(Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor) Creates a new InstructionSequenceReplacer.InstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher, Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor) Deprecated.protectedInstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor) Creates a new InstructionSequenceReplacer. -
Method Summary
Modifier and TypeMethodDescriptioncatch_(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.protected InstructionSequenceMatcherlabel()Creates a new label that can be used as a pseudo-instruction to mark a local offset.protected intmatchedArgument(int argument) protected intmatchedArgument(Clazz clazz, int argument) Returns the matched argument for the given pattern argument.protected intmatchedArgument(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, int argument) Returns the matched argument for the given pattern argument.protected intmatchedBranchOffset(int offset, int branchOffset) Returns the value of the specified matched branch offset.protected ConstantmatchedConstant(ProgramClass programClass, int argument) protected intmatchedConstantIndex(ProgramClass programClass, int constantIndex) Returns the matched or newly created constant index for the given pattern constant index.protected int[]matchedJumpOffsets(int offset, int[] jumpOffsets) Returns the values of the specified matched jump offsets.voidvisitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) Visits any Instruction instance.protected booleanwasConstant(int argument) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface proguard.classfile.constant.visitor.ConstantVisitor
visitAnyConstant, visitAnyMethodrefConstant, visitAnyRefConstant, visitClassConstant, visitDoubleConstant, visitDynamicConstant, visitFieldrefConstant, visitFloatConstant, visitIntegerConstant, visitInterfaceMethodrefConstant, visitInvokeDynamicConstant, visitLongConstant, visitMethodHandleConstant, visitMethodrefConstant, visitMethodTypeConstant, visitModuleConstant, visitNameAndTypeConstant, visitPackageConstant, visitPrimitiveArrayConstant, visitStringConstant, visitUtf8ConstantMethods inherited from interface proguard.classfile.instruction.visitor.InstructionVisitor
visitAnySwitchInstruction, visitBranchInstruction, visitConstantInstruction, visitLookUpSwitchInstruction, visitSimpleInstruction, visitTableSwitchInstruction, visitVariableInstruction
-
Field Details
-
X
public static final int X- See Also:
-
Y
public static final int Y- See Also:
-
Z
public static final int Z- See Also:
-
A
public static final int A- See Also:
-
B
public static final int B- See Also:
-
C
public static final int C- See Also:
-
D
public static final int D- See Also:
-
E
public static final int E- See Also:
-
F
public static final int F- See Also:
-
G
public static final int G- See Also:
-
H
public static final int H- See Also:
-
I
public static final int I- See Also:
-
J
public static final int J- See Also:
-
K
public static final int K- See Also:
-
L
public static final int L- See Also:
-
M
public static final int M- See Also:
-
N
public static final int N- See Also:
-
O
public static final int O- See Also:
-
P
public static final int P- See Also:
-
Q
public static final int Q- See Also:
-
R
public static final int R- See Also:
-
STRING_A_LENGTH
public static final int STRING_A_LENGTH- See Also:
-
CLASS_A_NAME
public static final int CLASS_A_NAME- See Also:
-
CLASS_A_SIMPLE_NAME
public static final int CLASS_A_SIMPLE_NAME- See Also:
-
BOOLEAN_A_STRING
public static final int BOOLEAN_A_STRING- See Also:
-
CHAR_A_STRING
public static final int CHAR_A_STRING- See Also:
-
INT_A_STRING
public static final int INT_A_STRING- See Also:
-
LONG_A_STRING
public static final int LONG_A_STRING- See Also:
-
FLOAT_A_STRING
public static final int FLOAT_A_STRING- See Also:
-
DOUBLE_A_STRING
public static final int DOUBLE_A_STRING- See Also:
-
STRING_A_STRING
public static final int STRING_A_STRING- See Also:
-
BOOLEAN_B_STRING
public static final int BOOLEAN_B_STRING- See Also:
-
CHAR_B_STRING
public static final int CHAR_B_STRING- See Also:
-
INT_B_STRING
public static final int INT_B_STRING- See Also:
-
LONG_B_STRING
public static final int LONG_B_STRING- See Also:
-
FLOAT_B_STRING
public static final int FLOAT_B_STRING- See Also:
-
DOUBLE_B_STRING
public static final int DOUBLE_B_STRING- See Also:
-
STRING_B_STRING
public static final int STRING_B_STRING- See Also:
-
-
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) Deprecated.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:InstructionVisitorVisits any Instruction instance. The more specific default implementations of this interface delegate to this method.- Specified by:
visitAnyInstructionin interfaceInstructionVisitor
-
matchedArgument
protected int matchedArgument(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, int argument) Returns the matched argument for the given pattern argument. -
matchedArgument
Returns the matched argument for the given pattern argument. -
matchedConstantIndex
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
-
wasConstant
protected boolean wasConstant(int argument) -
matchedConstant
-
matchedArgument
protected int matchedArgument(int argument) -
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()).
-
InstructionSequenceReplacer(InstructionSequenceMatcher, Constant[], Instruction[], BranchTargetFinder, CodeAttributeEditor, InstructionVisitor)instead.