Package proguard.classfile.editor
Class InstructionSequenceReplacer
- java.lang.Object
-
- proguard.classfile.editor.InstructionSequenceReplacer
-
- All Implemented Interfaces:
ConstantVisitor
,InstructionVisitor
public class InstructionSequenceReplacer extends java.lang.Object implements InstructionVisitor, ConstantVisitor
ThisInstructionVisitor
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 ofInstructionSequenceBuilder
: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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InstructionSequenceReplacer.Label
This pseudo-instruction represents a label that marks an instruction offset, for use in the context of the sequence replacer only.
-
Field Summary
Fields Modifier and Type Field Description static int
A
static int
B
static int
BOOLEAN_A_STRING
static int
BOOLEAN_B_STRING
static int
C
static int
CHAR_A_STRING
static int
CHAR_B_STRING
static int
CLASS_A_NAME
static int
CLASS_A_SIMPLE_NAME
static int
D
static int
DOUBLE_A_STRING
static int
DOUBLE_B_STRING
static int
E
static int
F
static int
FLOAT_A_STRING
static int
FLOAT_B_STRING
static int
G
static int
H
static int
I
static int
INT_A_STRING
static int
INT_B_STRING
static int
J
static int
K
static int
L
static int
LONG_A_STRING
static int
LONG_B_STRING
static int
M
static int
N
static int
O
static int
P
static int
Q
static int
R
static int
STRING_A_LENGTH
static int
STRING_A_STRING
static int
STRING_B_STRING
static int
X
static int
Y
static int
Z
-
Constructor Summary
Constructors Modifier Constructor Description InstructionSequenceReplacer(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)
protected
InstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor)
Creates a new InstructionSequenceReplacer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.protected InstructionSequenceMatcher
getInstructionSequenceMatcher()
static InstructionSequenceReplacer.Label
label()
Creates a new label that can be used as a pseudo-instruction to mark a local offset.protected int
matchedArgument(int argument)
protected int
matchedArgument(Clazz clazz, int argument)
Returns the matched argument for the given pattern argument.protected int
matchedArgument(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, int argument)
Returns the matched argument for the given pattern argument.protected int
matchedBranchOffset(int offset, int branchOffset)
Returns the value of the specified matched branch offset.protected Constant
matchedConstant(ProgramClass programClass, int argument)
protected int
matchedConstantIndex(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.void
visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
Visits any Instruction instance.protected boolean
wasConstant(int argument)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods 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, visitUtf8Constant
-
Methods inherited from interface proguard.classfile.instruction.visitor.InstructionVisitor
visitAnySwitchInstruction, visitBranchInstruction, visitConstantInstruction, visitLookUpSwitchInstruction, visitSimpleInstruction, visitTableSwitchInstruction, visitVariableInstruction
-
-
-
-
Field Detail
-
X
public static final int X
- See Also:
- Constant Field Values
-
Y
public static final int Y
- See Also:
- Constant Field Values
-
Z
public static final int Z
- See Also:
- Constant Field Values
-
A
public static final int A
- See Also:
- Constant Field Values
-
B
public static final int B
- See Also:
- Constant Field Values
-
C
public static final int C
- See Also:
- Constant Field Values
-
D
public static final int D
- See Also:
- Constant Field Values
-
E
public static final int E
- See Also:
- Constant Field Values
-
F
public static final int F
- See Also:
- Constant Field Values
-
G
public static final int G
- See Also:
- Constant Field Values
-
H
public static final int H
- See Also:
- Constant Field Values
-
I
public static final int I
- See Also:
- Constant Field Values
-
J
public static final int J
- See Also:
- Constant Field Values
-
K
public static final int K
- See Also:
- Constant Field Values
-
L
public static final int L
- See Also:
- Constant Field Values
-
M
public static final int M
- See Also:
- Constant Field Values
-
N
public static final int N
- See Also:
- Constant Field Values
-
O
public static final int O
- See Also:
- Constant Field Values
-
P
public static final int P
- See Also:
- Constant Field Values
-
Q
public static final int Q
- See Also:
- Constant Field Values
-
R
public static final int R
- See Also:
- Constant Field Values
-
STRING_A_LENGTH
public static final int STRING_A_LENGTH
- See Also:
- Constant Field Values
-
CLASS_A_NAME
public static final int CLASS_A_NAME
- See Also:
- Constant Field Values
-
CLASS_A_SIMPLE_NAME
public static final int CLASS_A_SIMPLE_NAME
- See Also:
- Constant Field Values
-
BOOLEAN_A_STRING
public static final int BOOLEAN_A_STRING
- See Also:
- Constant Field Values
-
CHAR_A_STRING
public static final int CHAR_A_STRING
- See Also:
- Constant Field Values
-
INT_A_STRING
public static final int INT_A_STRING
- See Also:
- Constant Field Values
-
LONG_A_STRING
public static final int LONG_A_STRING
- See Also:
- Constant Field Values
-
FLOAT_A_STRING
public static final int FLOAT_A_STRING
- See Also:
- Constant Field Values
-
DOUBLE_A_STRING
public static final int DOUBLE_A_STRING
- See Also:
- Constant Field Values
-
STRING_A_STRING
public static final int STRING_A_STRING
- See Also:
- Constant Field Values
-
BOOLEAN_B_STRING
public static final int BOOLEAN_B_STRING
- See Also:
- Constant Field Values
-
CHAR_B_STRING
public static final int CHAR_B_STRING
- See Also:
- Constant Field Values
-
INT_B_STRING
public static final int INT_B_STRING
- See Also:
- Constant Field Values
-
LONG_B_STRING
public static final int LONG_B_STRING
- See Also:
- Constant Field Values
-
FLOAT_B_STRING
public static final int FLOAT_B_STRING
- See Also:
- Constant Field Values
-
DOUBLE_B_STRING
public static final int DOUBLE_B_STRING
- See Also:
- Constant Field Values
-
STRING_B_STRING
public static final int STRING_B_STRING
- See Also:
- Constant Field Values
-
-
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
@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 Detail
-
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 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
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()
).
-
-