KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > FindUselessControlFlow


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.detect;
21
22
23 import edu.umd.cs.findbugs.*;
24 import java.util.BitSet JavaDoc;
25 import org.apache.bcel.Constants;
26 import org.apache.bcel.classfile.LineNumberTable;
27
28 /**
29  * A Detector to look for useless control flow. For example,
30  * <pre>
31  * if (argv.length == 1);
32  * System.out.println("Hello, " + argv[0]);
33  * </pre>
34  * In this kind of bug, we'll see an ifcmp instruction where the IF
35  * target is the same as the fall-through target.
36  * <p/>
37  * <p> The idea for this detector came from Richard P. King,
38  * and the idea of looking for if instructions with identical
39  * branch and fall-through targets is from Mike Fagan.
40  *
41  * @author David Hovemeyer
42  */

43 public class FindUselessControlFlow extends BytecodeScanningDetector implements StatelessDetector {
44     private static final BitSet JavaDoc ifInstructionSet = new BitSet JavaDoc();
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 JavaDoc
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 // vim:ts=4
94
Popular Tags