1 19 20 package edu.umd.cs.findbugs.detect; 21 22 23 import java.util.BitSet ; 24 25 import org.apache.bcel.classfile.Code; 26 27 import edu.umd.cs.findbugs.BugInstance; 28 import edu.umd.cs.findbugs.BugReporter; 29 import edu.umd.cs.findbugs.BytecodeScanningDetector; 30 import edu.umd.cs.findbugs.LocalVariableAnnotation; 31 import edu.umd.cs.findbugs.StatelessDetector; 32 33 public class FindLocalSelfAssignment2 extends BytecodeScanningDetector implements StatelessDetector { 34 35 private BugReporter bugReporter; 36 private int previousLoadOf = -1; 37 private int previousGotoTarget; 38 private int gotoCount; 39 public FindLocalSelfAssignment2(BugReporter bugReporter) { 40 this.bugReporter = bugReporter; 41 } 42 43 private BitSet previousStores = new BitSet (); 44 45 46 @Override 47 public void visit(Code obj) { 48 previousLoadOf = -1; 49 previousGotoTarget = -1; 50 gotoCount = 0; 51 previousStores.clear(); 52 super.visit(obj); 53 } 54 55 56 @Override 57 public void sawOpcode(int seen) { 58 if (seen == GOTO) { 59 previousGotoTarget = getBranchTarget(); 60 gotoCount++; 61 if (previousGotoTarget < getPC()) 62 previousLoadOf = -1; 63 } else { 64 if (isRegisterLoad()) 65 previousLoadOf = getRegisterOperand(); 66 else { 67 if (isRegisterStore()) { 68 if (previousLoadOf == getRegisterOperand() && gotoCount < 2 && getPC() != previousGotoTarget) { 69 int priority = NORMAL_PRIORITY; 70 String methodName = getMethodName(); 71 if (methodName.equals("<init>") || methodName.startsWith("set") && getCode().getCode().length <= 5 || 72 !previousStores.get(getRegisterOperand())) priority = HIGH_PRIORITY; 73 bugReporter.reportBug( 74 new BugInstance(this, 75 "SA_LOCAL_SELF_ASSIGNMENT", priority) 76 .addClassAndMethod(this) 77 .add(LocalVariableAnnotation.getLocalVariableAnnotation(getMethod(), getRegisterOperand(), getPC(), getPC())) 78 79 .addSourceLine(this)); 80 } 81 previousStores.set(getRegisterOperand()); 82 } 83 84 85 previousLoadOf = -1; 86 gotoCount = 0; 87 } 88 } 89 } 90 } 91 | Popular Tags |