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]); ... }
Version: $Id: InstructionFinder.java 386056 2006-03-15 11:31:56Z tcurdt $
See Also: Instruction InstructionList
Nested Class Summary | |
---|---|
static interface | InstructionFinder.CodeConstraint
Code patterns found may be checked using an additional user-defined
constraint object whether they really match the needed criterion. |
Field Summary | |
---|---|
InstructionHandle[] | handles |
InstructionList | il |
String | il_string |
static Map | map |
static int | NO_OPCODES |
static int | OFFSET |
Constructor Summary | |
---|---|
InstructionFinder(InstructionList il) |
Method Summary | |
---|---|
static String | compilePattern(String pattern)
Replace symbolic names of instructions with the appropiate character and
remove all white space from string. |
InstructionList | getInstructionList() |
InstructionHandle[] | getMatch(int matched_from, int match_length) |
static char | makeChar(short opcode)
Convert opcode number to char. |
static String | mapName(String pattern)
Map symbolic instruction names like "getfield" to a single character.
|
static String | pattern2string(String pattern) |
static String | pattern2string(String pattern, boolean make_string) |
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, InstructionHandle from, InstructionFinder.CodeConstraint constraint)
Search for the given pattern in the instruction list. |
Iterator | search(String pattern)
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, InstructionFinder.CodeConstraint constraint)
Start search beginning from the start of the given instruction list. |
Parameters: il instruction list to search for given patterns
Parameters: pattern The pattern to compile
Returns: translated regular expression string
Returns: the inquired instruction list
Returns: the matched piece of code as an array of instruction (handles)
Parameters: pattern instruction pattern in lower case
Returns: encoded string for a pattern such as "BranchInstruction".
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.
Parameters: pattern the instruction pattern to search for, where case is ignored from where to start the search in the instruction list constraint optional CodeConstraint to check the found code pattern for user-defined constraints
Returns: iterator of matches where e.nextElement() returns an array of instruction handles describing the matched area
Parameters: pattern the instruction pattern to search for, where case is ignored
Returns: iterator of matches where e.nextElement() returns an array of instruction handles describing the matched area
Parameters: pattern the instruction pattern to search for, where case is ignored from where to start the search in the instruction list
Returns: iterator of matches where e.nextElement() returns an array of instruction handles describing the matched area
Parameters: pattern the instruction pattern to search for, case is ignored constraint constraints to be checked on matching code
Returns: instruction handle or `null' if the match failed