1 19 20 package edu.umd.cs.findbugs.detect; 21 22 23 import edu.umd.cs.findbugs.*; 24 import java.util.BitSet ; 25 import org.apache.bcel.Constants; 26 import org.apache.bcel.classfile.LineNumberTable; 27 28 43 public class FindUselessControlFlow extends BytecodeScanningDetector implements StatelessDetector { 44 private static final BitSet ifInstructionSet = new BitSet (); 45 46 static { 47 ifInstructionSet.set(Constants.IF_ACMPEQ); 48 ifInstructionSet.set(Constants.IF_ACMPNE); 49 ifInstructionSet.set(Constants.IF_ICMPEQ); 50 ifInstructionSet.set(Constants.IF_ICMPNE); 51 ifInstructionSet.set(Constants.IF_ICMPLT); 52 ifInstructionSet.set(Constants.IF_ICMPLE); 53 ifInstructionSet.set(Constants.IF_ICMPGT); 54 ifInstructionSet.set(Constants.IF_ICMPGE); 55 ifInstructionSet.set(Constants.IFEQ); 56 ifInstructionSet.set(Constants.IFNE); 57 ifInstructionSet.set(Constants.IFLT); 58 ifInstructionSet.set(Constants.IFLE); 59 ifInstructionSet.set(Constants.IFGT); 60 ifInstructionSet.set(Constants.IFGE); 61 ifInstructionSet.set(Constants.IFNULL); 62 ifInstructionSet.set(Constants.IFNONNULL); 63 } 64 65 private BugReporter bugReporter; 66 67 public FindUselessControlFlow(BugReporter bugReporter) { 68 this.bugReporter = bugReporter; 69 } 70 71 72 @Override 73 public void sawOpcode(int seen) { 74 if (ifInstructionSet.get(seen)) { 75 if (getBranchTarget() == getBranchFallThrough()) { 76 int priority = NORMAL_PRIORITY; 77 78 LineNumberTable lineNumbers = getCode().getLineNumberTable(); 79 if (lineNumbers != null) { 80 int branchLineNumber = lineNumbers.getSourceLine(getPC()); 81 int targetLineNumber = lineNumbers.getSourceLine(getBranchFallThrough()); 82 if (branchLineNumber +1 == targetLineNumber || branchLineNumber == targetLineNumber ) priority = HIGH_PRIORITY; 83 else if (branchLineNumber +2 < targetLineNumber) priority = LOW_PRIORITY; 84 } else priority = LOW_PRIORITY; 85 bugReporter.reportBug(new BugInstance(this, priority == HIGH_PRIORITY ? "UCF_USELESS_CONTROL_FLOW_NEXT_LINE" : "UCF_USELESS_CONTROL_FLOW", priority) 86 .addClassAndMethod(this) 87 .addSourceLine(this)); 88 } 89 } 90 } 91 } 92 93 | Popular Tags |