Package proguard.classfile.util
Class InstructionSequenceMatcher
- java.lang.Object
-
- proguard.classfile.util.InstructionSequenceMatcher
-
- All Implemented Interfaces:
ConstantVisitor
,InstructionVisitor
public class InstructionSequenceMatcher extends java.lang.Object implements InstructionVisitor, ConstantVisitor
ThisInstructionVisitor
checks whether a given pattern instruction sequence occurs in the instructions that are visited. The arguments of the instruction sequence can be wildcards that are matched.
-
-
Field Summary
Fields Modifier and Type Field Description static int
A
static int
B
static int
C
static int
D
static int
E
static int
F
static int
G
static int
H
static int
I
static int
J
static int
K
static int
L
static int
M
protected boolean
matchingConstant
static int
N
static int
O
static int
P
protected Constant
patternConstant
protected Constant[]
patternConstants
protected Instruction[]
patternInstructions
static int
Q
static int
R
static int
X
static int
Y
static int
Z
-
Constructor Summary
Constructors Constructor Description InstructionSequenceMatcher(Constant[] patternConstants, Instruction[] patternInstructions)
Creates a new InstructionSequenceMatcher.InstructionSequenceMatcher(Constant[] patternConstants, Instruction[] patternInstructions, boolean matchSubclasses)
Creates a new InstructionSequenceMatcher.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
finalMatch(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
Performs a final check on the candidate sequence to match, after the pattern has been successfully fully matched with the sequence.int
instructionCount()
Returns the number of instructions in the pattern sequence.boolean
isMatching()
Returns whether the complete pattern sequence has been matched.static void
main(java.lang.String[] args)
Small sample application that illustrates the use of this class.int
matchedArgument(int argument)
Returns the value of the specified matched argument (wildcard or not).int[]
matchedArguments(int[] arguments)
Returns the values of the specified matched arguments (wildcard or not).int
matchedBranchOffset(int offset, int branchOffset)
Returns the value of the specified matched branch offset (wildcard or not).int
matchedConstantIndex(int constantIndex)
Returns the index of the specified matched constant (wildcard or not).int
matchedInstructionOffset(int index)
Returns the matched instruction offset of the specified pattern instruction.int[]
matchedJumpOffsets(int offset, int[] jumpOffsets)
Returns the values of the specified matched jump offsets (wildcard or not).protected boolean
matchingArguments(int[] arguments1, int[] arguments2)
protected boolean
matchingArguments(int argument1, int argument2)
protected boolean
matchingBranchOffsets(int offset, int branchOffset1, int branchOffset2)
protected boolean
matchingConstantIndices(Clazz clazz, int constantIndex1, int constantIndex2)
protected boolean
matchingJumpOffsets(int offset, int[] jumpOffsets1, int[] jumpOffsets2)
protected boolean
matchingOpcodes(Instruction instruction1, Instruction instruction2)
void
reset()
Starts matching from the first instruction again next time.void
visitAnyRefConstant(Clazz clazz, RefConstant refConstant)
Visits any RefConstant instance.void
visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
void
visitClassConstant(Clazz clazz, ClassConstant classConstant)
void
visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
void
visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant)
void
visitDynamicConstant(Clazz clazz, DynamicConstant dynamicConstant)
void
visitFloatConstant(Clazz clazz, FloatConstant floatConstant)
void
visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant)
void
visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant)
void
visitLongConstant(Clazz clazz, LongConstant longConstant)
void
visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
void
visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant)
void
visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant)
void
visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant)
void
visitPrimitiveArrayConstant(Clazz clazz, PrimitiveArrayConstant primitiveArrayConstant)
void
visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
void
visitStringConstant(Clazz clazz, StringConstant stringConstant)
void
visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction)
void
visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant)
void
visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
boolean
wasConstant(int argument)
Returns whether the specified wildcard argument was a constant from the constant pool in the most recent match.-
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, visitFieldrefConstant, visitInterfaceMethodrefConstant, visitMethodrefConstant, visitModuleConstant, visitPackageConstant
-
Methods inherited from interface proguard.classfile.instruction.visitor.InstructionVisitor
visitAnyInstruction, visitAnySwitchInstruction
-
-
-
-
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
-
patternConstants
protected final Constant[] patternConstants
-
patternInstructions
protected final Instruction[] patternInstructions
-
patternConstant
protected Constant patternConstant
-
matchingConstant
protected boolean matchingConstant
-
-
Constructor Detail
-
InstructionSequenceMatcher
public InstructionSequenceMatcher(Constant[] patternConstants, Instruction[] patternInstructions)
Creates a new InstructionSequenceMatcher.- Parameters:
patternConstants
- any constants referenced by the pattern instruction.patternInstructions
- the pattern instruction sequence.
-
InstructionSequenceMatcher
public InstructionSequenceMatcher(Constant[] patternConstants, Instruction[] patternInstructions, boolean matchSubclasses)
Creates a new InstructionSequenceMatcher.- Parameters:
patternConstants
- any constants referenced by the pattern instruction.patternInstructions
- the pattern instruction sequence.matchSubclasses
- whether constant references on subclasses are matching when the pattern contains a constant of a superclass. (default: false)
-
-
Method Detail
-
reset
public void reset()
Starts matching from the first instruction again next time.
-
isMatching
public boolean isMatching()
Returns whether the complete pattern sequence has been matched.
-
instructionCount
public int instructionCount()
Returns the number of instructions in the pattern sequence.
-
matchedInstructionOffset
public int matchedInstructionOffset(int index)
Returns the matched instruction offset of the specified pattern instruction.
-
wasConstant
public boolean wasConstant(int argument)
Returns whether the specified wildcard argument was a constant from the constant pool in the most recent match.
-
matchedArgument
public int matchedArgument(int argument)
Returns the value of the specified matched argument (wildcard or not).
-
matchedArguments
public int[] matchedArguments(int[] arguments)
Returns the values of the specified matched arguments (wildcard or not).
-
matchedConstantIndex
public int matchedConstantIndex(int constantIndex)
Returns the index of the specified matched constant (wildcard or not).
-
matchedBranchOffset
public int matchedBranchOffset(int offset, int branchOffset)
Returns the value of the specified matched branch offset (wildcard or not).
-
matchedJumpOffsets
public int[] matchedJumpOffsets(int offset, int[] jumpOffsets)
Returns the values of the specified matched jump offsets (wildcard or not).
-
visitSimpleInstruction
public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
- Specified by:
visitSimpleInstruction
in interfaceInstructionVisitor
-
visitVariableInstruction
public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
- Specified by:
visitVariableInstruction
in interfaceInstructionVisitor
-
visitConstantInstruction
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
- Specified by:
visitConstantInstruction
in interfaceInstructionVisitor
-
visitBranchInstruction
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
- Specified by:
visitBranchInstruction
in interfaceInstructionVisitor
-
visitTableSwitchInstruction
public void visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction)
- Specified by:
visitTableSwitchInstruction
in interfaceInstructionVisitor
-
visitLookUpSwitchInstruction
public void visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
- Specified by:
visitLookUpSwitchInstruction
in interfaceInstructionVisitor
-
visitIntegerConstant
public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant)
- Specified by:
visitIntegerConstant
in interfaceConstantVisitor
-
visitLongConstant
public void visitLongConstant(Clazz clazz, LongConstant longConstant)
- Specified by:
visitLongConstant
in interfaceConstantVisitor
-
visitFloatConstant
public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant)
- Specified by:
visitFloatConstant
in interfaceConstantVisitor
-
visitDoubleConstant
public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant)
- Specified by:
visitDoubleConstant
in interfaceConstantVisitor
-
visitPrimitiveArrayConstant
public void visitPrimitiveArrayConstant(Clazz clazz, PrimitiveArrayConstant primitiveArrayConstant)
- Specified by:
visitPrimitiveArrayConstant
in interfaceConstantVisitor
-
visitStringConstant
public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
- Specified by:
visitStringConstant
in interfaceConstantVisitor
-
visitUtf8Constant
public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant)
- Specified by:
visitUtf8Constant
in interfaceConstantVisitor
-
visitDynamicConstant
public void visitDynamicConstant(Clazz clazz, DynamicConstant dynamicConstant)
- Specified by:
visitDynamicConstant
in interfaceConstantVisitor
-
visitInvokeDynamicConstant
public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant)
- Specified by:
visitInvokeDynamicConstant
in interfaceConstantVisitor
-
visitMethodHandleConstant
public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant)
- Specified by:
visitMethodHandleConstant
in interfaceConstantVisitor
-
visitAnyRefConstant
public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant)
Description copied from interface:ConstantVisitor
Visits any RefConstant instance. The more specific default implementations of this interface delegate to this method.- Specified by:
visitAnyRefConstant
in interfaceConstantVisitor
-
visitClassConstant
public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
- Specified by:
visitClassConstant
in interfaceConstantVisitor
-
visitMethodTypeConstant
public void visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant)
- Specified by:
visitMethodTypeConstant
in interfaceConstantVisitor
-
visitNameAndTypeConstant
public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant)
- Specified by:
visitNameAndTypeConstant
in interfaceConstantVisitor
-
matchingOpcodes
protected boolean matchingOpcodes(Instruction instruction1, Instruction instruction2)
-
matchingArguments
protected boolean matchingArguments(int argument1, int argument2)
-
matchingArguments
protected boolean matchingArguments(int[] arguments1, int[] arguments2)
-
matchingConstantIndices
protected boolean matchingConstantIndices(Clazz clazz, int constantIndex1, int constantIndex2)
-
matchingBranchOffsets
protected boolean matchingBranchOffsets(int offset, int branchOffset1, int branchOffset2)
-
matchingJumpOffsets
protected boolean matchingJumpOffsets(int offset, int[] jumpOffsets1, int[] jumpOffsets2)
-
finalMatch
protected boolean finalMatch(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
Performs a final check on the candidate sequence to match, after the pattern has been successfully fully matched with the sequence. Subclasses may override this method to implement additional constraints on the matched sequences.
-
main
public static void main(java.lang.String[] args)
Small sample application that illustrates the use of this class.
-
-