org.apache.bcel.util
public class InstructionFinder extends Object
This class internally uses the Regexp package to search for regular expressions. A typical application would look like this:
InstructionFinder f = new InstructionFinder(il); String pat = "IfInstruction ICONST_0 GOTO ICONST_1 NOP (IFEQ|IFNE)"; for(Iterator i = f.search(pat, constraint); i.hasNext(); ) { InstructionHandle[] match = (InstructionHandle[])i.next(); ... il.delete(match[1], match[5]); ... }
Instruction
,
InstructionList
Modifier and Type | Class and Description |
---|---|
static interface |
InstructionFinder.CodeConstraint
Code patterns found may be checked using an additional user-defined
constraint object whether they really match the needed criterion.
|
Modifier and Type | Field and Description |
---|---|
private InstructionHandle[] |
handles |
private InstructionList |
il |
private String |
il_string |
private static Map |
map |
private static int |
NO_OPCODES |
private static int |
OFFSET |
Constructor and Description |
---|
InstructionFinder(InstructionList il) |
Modifier and Type | Method and Description |
---|---|
private static String |
compilePattern(String pattern)
Replace symbolic names of instructions with the appropiate character and
remove all white space from string.
|
InstructionList |
getInstructionList() |
private InstructionHandle[] |
getMatch(int matched_from,
int match_length) |
private static char |
makeChar(short opcode)
Convert opcode number to char.
|
private static String |
mapName(String pattern)
Map symbolic instruction names like "getfield" to a single character.
|
private static String |
pattern2string(String pattern) |
private static String |
pattern2string(String pattern,
boolean make_string) |
private static String |
precompile(short from,
short to,
short extra) |
void |
reread()
Reread the instruction list, e.g., after you've altered the list upon a
match.
|
Iterator |
search(String pattern)
Start search beginning from the start of the given instruction list.
|
Iterator |
search(String pattern,
InstructionFinder.CodeConstraint constraint)
Start search beginning from the start of the given instruction list.
|
Iterator |
search(String pattern,
InstructionHandle from)
Start search beginning from `from'.
|
Iterator |
search(String pattern,
InstructionHandle from,
InstructionFinder.CodeConstraint constraint)
Search for the given pattern in the instruction list.
|
private static final int OFFSET
private static final int NO_OPCODES
private static final Map map
private InstructionList il
private String il_string
private InstructionHandle[] handles
public InstructionFinder(InstructionList il)
il
- instruction list to search for given patternspublic final void reread()
private static final String mapName(String pattern)
pattern
- instruction pattern in lower caseprivate static final String compilePattern(String pattern)
pattern
- The pattern to compileprivate InstructionHandle[] getMatch(int matched_from, int match_length)
public final Iterator search(String pattern, InstructionHandle from, InstructionFinder.CodeConstraint constraint)
Example pattern:
search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
If you alter the instruction list upon a match such that other matching areas are affected, you should call reread() to update the finder and call search() again, because the matches are cached.
pattern
- the instruction pattern to search for, where case is ignoredfrom
- where to start the search in the instruction listconstraint
- optional CodeConstraint to check the found code pattern for
user-defined constraintspublic final Iterator search(String pattern)
pattern
- the instruction pattern to search for, where case is ignoredpublic final Iterator search(String pattern, InstructionHandle from)
pattern
- the instruction pattern to search for, where case is ignoredfrom
- where to start the search in the instruction listpublic final Iterator search(String pattern, InstructionFinder.CodeConstraint constraint)
pattern
- the instruction pattern to search for, case is ignoredconstraint
- constraints to be checked on matching codeprivate static final char makeChar(short opcode)
public final InstructionList getInstructionList()
private static String precompile(short from, short to, short extra)