1 20 21 package edu.umd.cs.findbugs.detect; 22 23 24 import edu.umd.cs.findbugs.*; 25 import edu.umd.cs.findbugs.visitclass.LVTHelper; 26 import org.apache.bcel.classfile.*; 27 28 35 public class SuperfluousInstanceOf extends BytecodeScanningDetector implements StatelessDetector { 36 37 private static final int SEEN_NOTHING = 0; 38 private static final int SEEN_ALOAD = 1; 39 40 private BugReporter bugReporter; 41 private LocalVariableTable varTable; 42 private int state; 43 private int register; 44 45 public SuperfluousInstanceOf(BugReporter bugReporter) { 46 this.bugReporter = bugReporter; 47 } 48 49 50 51 @Override 52 public void visit(Method obj) { 53 state = SEEN_NOTHING; 54 varTable = obj.getLocalVariableTable(); 55 if (varTable != null) 56 super.visit(obj); 57 } 58 59 @Override 60 public void visit(Code obj) { 61 if (varTable != null) 62 super.visit(obj); 63 } 64 65 66 @Override 67 public void sawOpcode(int seen) { 68 switch (state) { 69 case SEEN_NOTHING: 70 if (seen == ALOAD) 71 register = getRegisterOperand(); 72 else if ((seen >= ALOAD_0) && (seen <= ALOAD_3)) 73 register = seen - ALOAD_0; 74 else 75 return; 76 state = SEEN_ALOAD; 77 break; 78 79 case SEEN_ALOAD: 80 try { 81 if (seen == INSTANCEOF) { 82 LocalVariable lv = LVTHelper.getLocalVariableAtPC(varTable, register, getPC()); 83 if (lv != null) { 84 String objSignature = lv.getSignature(); 85 if (objSignature.charAt(0) == 'L') { 86 objSignature = objSignature.substring(1, objSignature.length()-1).replace('/', '.'); 87 String clsSignature = getDottedClassConstantOperand(); 88 89 if (clsSignature.charAt(0) != '[') { 90 if (org.apache.bcel.Repository.instanceOf( objSignature, clsSignature )) { 91 bugReporter.reportBug(new BugInstance(this, "SIO_SUPERFLUOUS_INSTANCEOF", LOW_PRIORITY) 92 .addClassAndMethod(this) 93 .addSourceLine(this)); 94 } 95 } 96 } 97 } 98 } 99 } catch (ClassNotFoundException cnfe) { 100 bugReporter.reportMissingClass(cnfe); 101 } 102 103 state = SEEN_NOTHING; 104 break; 105 } 106 107 } 108 } 109 110 | Popular Tags |