public class InstructionSequenceReplacer extends java.lang.Object implements InstructionVisitor, ConstantVisitor
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();
InstructionSequenceMatcher
Modifier and Type | Class and 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.
|
Modifier and Type | Field and 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 |
Modifier | Constructor and 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.
|
protected |
InstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher,
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)
|
Modifier and Type | Method and 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(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 |
matchedArgument(int 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) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
visitAnySwitchInstruction, visitBranchInstruction, visitConstantInstruction, visitLookUpSwitchInstruction, visitSimpleInstruction, visitTableSwitchInstruction, visitVariableInstruction
visitAnyConstant, visitAnyMethodrefConstant, visitAnyRefConstant, visitClassConstant, visitDoubleConstant, visitDynamicConstant, visitFieldrefConstant, visitFloatConstant, visitIntegerConstant, visitInterfaceMethodrefConstant, visitInvokeDynamicConstant, visitLongConstant, visitMethodHandleConstant, visitMethodrefConstant, visitMethodTypeConstant, visitModuleConstant, visitNameAndTypeConstant, visitPackageConstant, visitPrimitiveArrayConstant, visitStringConstant, visitUtf8Constant
public static final int X
public static final int Y
public static final int Z
public static final int A
public static final int B
public static final int C
public static final int D
public static final int E
public static final int F
public static final int G
public static final int H
public static final int I
public static final int J
public static final int K
public static final int L
public static final int M
public static final int N
public static final int O
public static final int P
public static final int Q
public static final int R
public static final int STRING_A_LENGTH
public static final int CLASS_A_NAME
public static final int CLASS_A_SIMPLE_NAME
public static final int BOOLEAN_A_STRING
public static final int CHAR_A_STRING
public static final int INT_A_STRING
public static final int LONG_A_STRING
public static final int FLOAT_A_STRING
public static final int DOUBLE_A_STRING
public static final int STRING_A_STRING
public static final int BOOLEAN_B_STRING
public static final int CHAR_B_STRING
public static final int INT_B_STRING
public static final int LONG_B_STRING
public static final int FLOAT_B_STRING
public static final int DOUBLE_B_STRING
public static final int STRING_B_STRING
public InstructionSequenceReplacer(Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor)
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.public InstructionSequenceReplacer(Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor)
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.@Deprecated public InstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher, Constant[] patternConstants, Instruction[] patternInstructions, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor)
InstructionSequenceReplacer(InstructionSequenceMatcher,
Constant[], Instruction[], BranchTargetFinder, CodeAttributeEditor, InstructionVisitor)
instead.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.protected InstructionSequenceReplacer(InstructionSequenceMatcher instructionSequenceMatcher, Constant[] replacementConstants, Instruction[] replacementInstructions, BranchTargetFinder branchTargetFinder, CodeAttributeEditor codeAttributeEditor, InstructionVisitor extraInstructionVisitor)
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.public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
InstructionVisitor
visitAnyInstruction
in interface InstructionVisitor
protected int matchedArgument(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, int argument)
protected int matchedArgument(Clazz clazz, int argument)
protected int matchedConstantIndex(ProgramClass programClass, int constantIndex)
protected int matchedBranchOffset(int offset, int branchOffset)
protected int[] matchedJumpOffsets(int offset, int[] jumpOffsets)
protected InstructionSequenceMatcher getInstructionSequenceMatcher()
protected boolean wasConstant(int argument)
protected Constant matchedConstant(ProgramClass programClass, int argument)
protected int matchedArgument(int argument)
public static InstructionSequenceReplacer.Label label()
InstructionSequenceReplacer.Label.offset()
).public static InstructionSequenceReplacer.Label catch_(int startOffset, int endOffset, int catchType)
InstructionSequenceReplacer.Label.offset()
).