1 17 package org.apache.bcel.util; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 import org.apache.bcel.Constants; 28 import org.apache.bcel.generic.ClassGenException; 29 import org.apache.bcel.generic.Instruction; 30 import org.apache.bcel.generic.InstructionHandle; 31 import org.apache.bcel.generic.InstructionList; 32 33 67 public class InstructionFinder { 68 69 private static final int OFFSET = 32767; private static final int NO_OPCODES = 256; private static final Map map = new HashMap (); private InstructionList il; 76 private String il_string; private InstructionHandle[] handles; 80 81 86 public InstructionFinder(InstructionList il) { 87 this.il = il; 88 reread(); 89 } 90 91 92 96 public final void reread() { 97 int size = il.getLength(); 98 char[] buf = new char[size]; handles = il.getInstructionHandles(); 101 for (int i = 0; i < size; i++) { 103 buf[i] = makeChar(handles[i].getInstruction().getOpcode()); 104 } 105 il_string = new String (buf); 106 } 107 108 109 116 private static final String mapName( String pattern ) { 117 String result = (String ) map.get(pattern); 118 if (result != null) { 119 return result; 120 } 121 for (short i = 0; i < NO_OPCODES; i++) { 122 if (pattern.equals(Constants.OPCODE_NAMES[i])) { 123 return "" + makeChar(i); 124 } 125 } 126 throw new RuntimeException ("Instruction unknown: " + pattern); 127 } 128 129 130 139 private static final String compilePattern( String pattern ) { 140 String lower = pattern.toLowerCase(Locale.ENGLISH); 142 StringBuffer buf = new StringBuffer (); 143 int size = pattern.length(); 144 for (int i = 0; i < size; i++) { 145 char ch = lower.charAt(i); 146 if (Character.isLetterOrDigit(ch)) { 147 StringBuffer name = new StringBuffer (); 148 while ((Character.isLetterOrDigit(ch) || ch == '_') && i < size) { 149 name.append(ch); 150 if (++i < size) { 151 ch = lower.charAt(i); 152 } else { 153 break; 154 } 155 } 156 i--; 157 buf.append(mapName(name.toString())); 158 } else if (!Character.isWhitespace(ch)) { 159 buf.append(ch); 160 } 161 } 162 return buf.toString(); 163 } 164 165 166 169 private InstructionHandle[] getMatch( int matched_from, int match_length ) { 170 InstructionHandle[] match = new InstructionHandle[match_length]; 171 System.arraycopy(handles, matched_from, match, 0, match_length); 172 return match; 173 } 174 175 176 211 public final Iterator search( String pattern, InstructionHandle from, CodeConstraint constraint ) { 212 String search = compilePattern(pattern); 213 int start = -1; 214 for (int i = 0; i < handles.length; i++) { 215 if (handles[i] == from) { 216 start = i; break; 218 } 219 } 220 if (start == -1) { 221 throw new ClassGenException("Instruction handle " + from 222 + " not found in instruction list."); 223 } 224 Pattern regex = Pattern.compile(search); 225 List matches = new ArrayList (); 226 Matcher matcher = regex.matcher(il_string); 227 while (start < il_string.length() && matcher.find(start)) { 228 int startExpr = matcher.start(); 229 int endExpr = matcher.end(); 230 int lenExpr = (endExpr - startExpr) + 1; 231 InstructionHandle[] match = getMatch(startExpr, lenExpr); 232 if ((constraint == null) || constraint.checkCode(match)) { 233 matches.add(match); 234 } 235 start = endExpr; 236 } 237 return matches.iterator(); 238 } 239 240 241 249 public final Iterator search( String pattern ) { 250 return search(pattern, il.getStart(), null); 251 } 252 253 254 264 public final Iterator search( String pattern, InstructionHandle from ) { 265 return search(pattern, from, null); 266 } 267 268 269 279 public final Iterator search( String pattern, CodeConstraint constraint ) { 280 return search(pattern, il.getStart(), constraint); 281 } 282 283 284 287 private static final char makeChar( short opcode ) { 288 return (char) (opcode + OFFSET); 289 } 290 291 292 295 public final InstructionList getInstructionList() { 296 return il; 297 } 298 299 305 public static interface CodeConstraint { 306 307 312 public boolean checkCode( InstructionHandle[] match ); 313 } 314 315 static { 317 map 318 .put( 319 "arithmeticinstruction", 320 "(irem|lrem|iand|ior|ineg|isub|lneg|fneg|fmul|ldiv|fadd|lxor|frem|idiv|land|ixor|ishr|fsub|lshl|fdiv|iadd|lor|dmul|lsub|ishl|imul|lmul|lushr|dneg|iushr|lshr|ddiv|drem|dadd|ladd|dsub)"); 321 map.put("invokeinstruction", "(invokevirtual|invokeinterface|invokestatic|invokespecial)"); 322 map 323 .put( 324 "arrayinstruction", 325 "(baload|aastore|saload|caload|fastore|lastore|iaload|castore|iastore|aaload|bastore|sastore|faload|laload|daload|dastore)"); 326 map.put("gotoinstruction", "(goto|goto_w)"); 327 map.put("conversioninstruction", 328 "(d2l|l2d|i2s|d2i|l2i|i2b|l2f|d2f|f2i|i2d|i2l|f2d|i2c|f2l|i2f)"); 329 map.put("localvariableinstruction", 330 "(fstore|iinc|lload|dstore|dload|iload|aload|astore|istore|fload|lstore)"); 331 map.put("loadinstruction", "(fload|dload|lload|iload|aload)"); 332 map.put("fieldinstruction", "(getfield|putstatic|getstatic|putfield)"); 333 map 334 .put( 335 "cpinstruction", 336 "(ldc2_w|invokeinterface|multianewarray|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|ldc_w|invokestatic|invokevirtual|putfield|ldc|new|anewarray)"); 337 map.put("stackinstruction", "(dup2|swap|dup2_x2|pop|pop2|dup|dup2_x1|dup_x2|dup_x1)"); 338 map 339 .put( 340 "branchinstruction", 341 "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)"); 342 map.put("returninstruction", "(lreturn|ireturn|freturn|dreturn|areturn|return)"); 343 map.put("storeinstruction", "(istore|fstore|dstore|astore|lstore)"); 344 map.put("select", "(tableswitch|lookupswitch)"); 345 map 346 .put( 347 "ifinstruction", 348 "(ifeq|ifgt|if_icmpne|if_icmpeq|ifge|ifnull|ifne|if_icmple|if_icmpge|if_acmpeq|if_icmplt|if_acmpne|ifnonnull|iflt|if_icmpgt|ifle)"); 349 map.put("jsrinstruction", "(jsr|jsr_w)"); 350 map.put("variablelengthinstruction", "(tableswitch|jsr|goto|lookupswitch)"); 351 map.put("unconditionalbranch", "(goto|jsr|jsr_w|athrow|goto_w)"); 352 map.put("constantpushinstruction", "(dconst|bipush|sipush|fconst|iconst|lconst)"); 353 map 354 .put( 355 "typedinstruction", 356 "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dastore|ret|f2d|f2i|drem|iinc|i2c|checkcast|frem|lreturn|astore|lushr|daload|dneg|fastore|istore|lshl|ldiv|lstore|areturn|ishr|ldc_w|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|faload|sipush|iushr|caload|instanceof|invokespecial|putfield|fmul|ireturn|laload|d2f|lneg|ixor|i2l|fdiv|lastore|multianewarray|i2b|getstatic|i2d|putstatic|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|freturn|ldc|aconst_null|castore|lmul|ldc2_w|dadd|iconst|f2l|ddiv|dstore|land|jsr|anewarray|dmul|bipush|dsub|sastore|d2i|i2s|lshr|iadd|l2i|lload|bastore|fstore|fneg|iload|fadd|baload|fconst|ior|ineg|dreturn|l2f|lconst|getfield|invokevirtual|invokestatic|iastore)"); 357 map.put("popinstruction", "(fstore|dstore|pop|pop2|astore|putstatic|istore|lstore)"); 358 map.put("allocationinstruction", "(multianewarray|new|anewarray|newarray)"); 359 map 360 .put( 361 "indexedinstruction", 362 "(lload|lstore|fload|ldc2_w|invokeinterface|multianewarray|astore|dload|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|dstore|istore|iinc|ldc_w|ret|fstore|invokestatic|iload|putfield|invokevirtual|ldc|new|aload|anewarray)"); 363 map 364 .put( 365 "pushinstruction", 366 "(dup|lload|dup2|bipush|fload|ldc2_w|sipush|lconst|fconst|dload|getstatic|ldc_w|aconst_null|dconst|iload|ldc|iconst|aload)"); 367 map 368 .put( 369 "stackproducer", 370 "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dup|f2d|f2i|drem|i2c|checkcast|frem|lushr|daload|dneg|lshl|ldiv|ishr|ldc_w|invokeinterface|lxor|ishl|l2d|i2f|faload|sipush|iushr|caload|instanceof|invokespecial|fmul|laload|d2f|lneg|ixor|i2l|fdiv|getstatic|i2b|swap|i2d|dup2|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|ldc|arraylength|aconst_null|tableswitch|lmul|ldc2_w|iconst|dadd|f2l|ddiv|land|jsr|anewarray|dmul|bipush|dsub|d2i|newarray|i2s|lshr|iadd|lload|l2i|fneg|iload|fadd|baload|fconst|lookupswitch|ior|ineg|lconst|l2f|getfield|invokevirtual|invokestatic)"); 371 map 372 .put( 373 "stackconsumer", 374 "(imul|lsub|lor|iflt|fcmpg|if_icmpgt|iand|ifeq|if_icmplt|lrem|ifnonnull|idiv|d2l|isub|dcmpg|dastore|if_icmpeq|f2d|f2i|drem|i2c|checkcast|frem|lreturn|astore|lushr|pop2|monitorexit|dneg|fastore|istore|lshl|ldiv|lstore|areturn|if_icmpge|ishr|monitorenter|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|iushr|instanceof|invokespecial|fmul|ireturn|d2f|lneg|ixor|pop|i2l|ifnull|fdiv|lastore|i2b|if_acmpeq|ifge|swap|i2d|putstatic|fcmpl|ladd|irem|dcmpl|fsub|freturn|ifgt|castore|lmul|dadd|f2l|ddiv|dstore|land|if_icmpne|if_acmpne|dmul|dsub|sastore|ifle|d2i|i2s|lshr|iadd|l2i|bastore|fstore|fneg|fadd|ior|ineg|ifne|dreturn|l2f|if_icmple|getfield|invokevirtual|invokestatic|iastore)"); 375 map 376 .put( 377 "exceptionthrower", 378 "(irem|lrem|laload|putstatic|baload|dastore|areturn|getstatic|ldiv|anewarray|iastore|castore|idiv|saload|lastore|fastore|putfield|lreturn|caload|getfield|return|aastore|freturn|newarray|instanceof|multianewarray|athrow|faload|iaload|aaload|dreturn|monitorenter|checkcast|bastore|arraylength|new|invokevirtual|sastore|ldc_w|ireturn|invokespecial|monitorexit|invokeinterface|ldc|invokestatic|daload)"); 379 map 380 .put( 381 "loadclass", 382 "(multianewarray|invokeinterface|instanceof|invokespecial|putfield|checkcast|putstatic|invokevirtual|new|getstatic|invokestatic|getfield|anewarray)"); 383 map 384 .put( 385 "instructiontargeter", 386 "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)"); 387 map.put("if_icmp", "(if_icmpne|if_icmpeq|if_icmple|if_icmpge|if_icmplt|if_icmpgt)"); 389 map.put("if_acmp", "(if_acmpeq|if_acmpne)"); 390 map.put("if", "(ifeq|ifne|iflt|ifge|ifgt|ifle)"); 391 map.put("iconst", precompile(Constants.ICONST_0, Constants.ICONST_5, Constants.ICONST_M1)); 393 map.put("lconst", new String (new char[] { 394 '(', makeChar(Constants.LCONST_0), '|', makeChar(Constants.LCONST_1), ')' 395 })); 396 map.put("dconst", new String (new char[] { 397 '(', makeChar(Constants.DCONST_0), '|', makeChar(Constants.DCONST_1), ')' 398 })); 399 map.put("fconst", new String (new char[] { 400 '(', makeChar(Constants.FCONST_0), '|', makeChar(Constants.FCONST_1), ')' 401 })); 402 map.put("iload", precompile(Constants.ILOAD_0, Constants.ILOAD_3, Constants.ILOAD)); 403 map.put("dload", precompile(Constants.DLOAD_0, Constants.DLOAD_3, Constants.DLOAD)); 404 map.put("fload", precompile(Constants.FLOAD_0, Constants.FLOAD_3, Constants.FLOAD)); 405 map.put("aload", precompile(Constants.ALOAD_0, Constants.ALOAD_3, Constants.ALOAD)); 406 map.put("istore", precompile(Constants.ISTORE_0, Constants.ISTORE_3, Constants.ISTORE)); 407 map.put("dstore", precompile(Constants.DSTORE_0, Constants.DSTORE_3, Constants.DSTORE)); 408 map.put("fstore", precompile(Constants.FSTORE_0, Constants.FSTORE_3, Constants.FSTORE)); 409 map.put("astore", precompile(Constants.ASTORE_0, Constants.ASTORE_3, Constants.ASTORE)); 410 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 412 String key = (String ) i.next(); 413 String value = (String ) map.get(key); 414 char ch = value.charAt(1); if (ch < OFFSET) { 416 map.put(key, compilePattern(value)); } 418 } 419 StringBuffer buf = new StringBuffer ("("); 421 for (short i = 0; i < NO_OPCODES; i++) { 422 if (Constants.NO_OF_OPERANDS[i] != Constants.UNDEFINED) { buf.append(makeChar(i)); 426 if (i < NO_OPCODES - 1) { 427 buf.append('|'); 428 } 429 } 430 } 431 buf.append(')'); 432 map.put("instruction", buf.toString()); 433 } 434 435 436 private static String precompile( short from, short to, short extra ) { 437 StringBuffer buf = new StringBuffer ("("); 438 for (short i = from; i <= to; i++) { 439 buf.append(makeChar(i)); 440 buf.append('|'); 441 } 442 buf.append(makeChar(extra)); 443 buf.append(")"); 444 return buf.toString(); 445 } 446 447 448 451 private static final String pattern2string( String pattern ) { 452 return pattern2string(pattern, true); 453 } 454 455 456 private static final String pattern2string( String pattern, boolean make_string ) { 457 StringBuffer buf = new StringBuffer (); 458 for (int i = 0; i < pattern.length(); i++) { 459 char ch = pattern.charAt(i); 460 if (ch >= OFFSET) { 461 if (make_string) { 462 buf.append(Constants.OPCODE_NAMES[ch - OFFSET]); 463 } else { 464 buf.append((ch - OFFSET)); 465 } 466 } else { 467 buf.append(ch); 468 } 469 } 470 return buf.toString(); 471 } 472 } 473 | Popular Tags |