This class internally uses the java.util.regex 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]);
...
}
com.sun.org.apache.bcel.internal.generic.Instruction , InstructionList
| Modifier and Type | Class and Description |
|---|---|
| public static interface | InstructionFinder.
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[] | |
| private final InstructionList | |
| private String | |
| private static final Map | |
| private static final int | |
| private static final int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static String | Returns: translated regular expression stringThe pattern to compile pattern)Replace symbolic names of instructions with the appropriate character and remove all white space from string. |
| public final InstructionList | |
| private InstructionHandle[] | getMatch(final int matchedFrom, final int matchLength)
Returns: the matched piece of code as an array of instruction (handles) |
| private static char | |
| private static String | |
| private static String | |
| public final void | |
| public final Iterator | |
| public final Iterator | Returns: instruction handle or 'null' if the match failedthe instruction pattern to search for, case is ignored pattern, final InstructionFinder.constraints to be checked on matching code constraintStart search beginning from the start of the given instruction list. |
| public final Iterator | Returns: iterator of matches where e.nextElement() returns an array of instruction handles describing the matched areathe instruction pattern to search for, where case is ignored pattern, final InstructionHandle where to start the search in the instruction list from)Start search beginning from 'from'. |
| public final Iterator | Returns: iterator of matches where e.nextElement() returns an array of instruction handles describing the matched areathe instruction pattern to search for, where case is ignored pattern, final InstructionHandle where to start the search in the instruction list from, final InstructionFinder.optional CodeConstraint to check the found code pattern for user-defined constraints constraintSearch for the given pattern in the instruction list. |
| handles | back to summary |
|---|---|
| private InstructionHandle[] handles | |
| il | back to summary |
|---|---|
| private final InstructionList il | |
| ilString | back to summary |
|---|---|
| private String ilString | |
| map | back to summary |
|---|---|
| private static final Map<String, String> map | |
| NO_OPCODES | back to summary |
|---|---|
| private static final int NO_OPCODES | |
| OFFSET | back to summary |
|---|---|
| private static final int OFFSET | |
| InstructionFinder | back to summary |
|---|---|
public InstructionFinder(final InstructionList il)
| |
| compilePattern | back to summary |
|---|---|
| private static String compilePattern(final String pattern) Replace symbolic names of instructions with the appropriate character and remove all white space from string. Meta characters such as +, * are ignored. | |
| getInstructionList | back to summary |
|---|---|
public final InstructionList getInstructionList()
| |
| getMatch | back to summary |
|---|---|
private InstructionHandle[] getMatch(final int matchedFrom, final int matchLength)
| |
| makeChar | back to summary |
|---|---|
| private static char makeChar(final short opcode) Convert opcode number to char. | |
| mapName | back to summary |
|---|---|
| private static String mapName(final String pattern) Map symbolic instruction names like "getfield" to a single character. | |
| precompile | back to summary |
|---|---|
| private static String precompile(final short from, final short to, final short extra) | |
| reread | back to summary |
|---|---|
| public final void reread() Reread the instruction list, e.g., after you've altered the list upon a match. | |
| search | back to summary |
|---|---|
| public final Iterator Start search beginning from the start of the given instruction list.
| |
| search | back to summary |
|---|---|
| public final Iterator Start search beginning from the start of the given instruction list. Check found matches with the constraint object.
| |
| search | back to summary |
|---|---|
| public final Iterator Start search beginning from 'from'.
| |
| search | back to summary |
|---|---|
| public final Iterator Search for the given pattern in the instruction list. You can search for any valid opcode via its symbolic name, e.g. "istore". You can also use a super class or an interface name to match a whole set of instructions, e.g. "BranchInstruction" or "LoadInstruction". "istore" is also an alias for all "istore_x" instructions. Additional aliases are "if" for "ifxx", "if_icmp" for "if_icmpxx", "if_acmp" for "if_acmpxx". Consecutive instruction names must be separated by white space which will be removed during the compilation of the pattern. For the rest the usual pattern matching rules for regular expressions apply. 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.
| |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | checkCode(InstructionHandle[]
array of instructions matching the requested pattern match)Returns: true if the matched area is really useful |
| checkCode | back to summary |
|---|---|
public boolean checkCode(InstructionHandle[] match)
| |