KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 package edu.umd.cs.findbugs.detect;
21
22
23 import edu.umd.cs.findbugs.*;
24 import org.apache.bcel.classfile.Code;
25
26 public class QuestionableBooleanAssignment extends BytecodeScanningDetector implements StatelessDetector
27 {
28     public static final int SEEN_NOTHING = 0;
29     public static final int SEEN_ICONST_0_OR_1 = 1;
30     public static final int SEEN_DUP = 2;
31     public static final int SEEN_ISTORE = 3;
32     public static final int SEEN_GOTO = 4;
33     public static final int SEEN_IF = 5;
34     
35     private BugReporter bugReporter;
36     private int state;
37     
38     private BugInstance bug;
39     public QuestionableBooleanAssignment(BugReporter bugReporter) {
40         this.bugReporter = bugReporter;
41     }
42
43
44     
45     @Override JavaDoc
46          public void visitCode(Code obj) {
47         state = SEEN_NOTHING;
48         super.visitCode(obj);
49         bug = null;
50     }
51     
52     @Override JavaDoc
53          public void sawOpcode(int seen) {
54         if (seen == GOTO && getBranchOffset() == 4) {
55             state = SEEN_GOTO;
56         }
57         else switch (state) {
58             case SEEN_NOTHING:
59                 if ((seen == ICONST_1) || (seen == ICONST_0))
60                     state = SEEN_ICONST_0_OR_1;
61             break;
62             
63             case SEEN_ICONST_0_OR_1:
64                 if (seen == DUP)
65                     state = SEEN_DUP;
66                 else
67                     state = SEEN_NOTHING;
68             break;
69             
70             case SEEN_DUP:
71                 if (((seen >= ISTORE_0) && (seen <= ISTORE_3)) || (seen == ISTORE))
72                     state = SEEN_ISTORE;
73                 else
74                     state = SEEN_NOTHING;
75             break;
76             
77             case SEEN_ISTORE:
78                 if (seen == IFEQ || seen == IFNE)
79                 {
80                     bug = new BugInstance( this, "QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT", HIGH_PRIORITY)
81                         .addClassAndMethod(this)
82                         .addSourceLine(this);
83                     state = SEEN_IF;
84                 }
85                 else state = SEEN_NOTHING;
86             break;
87             
88
89             case SEEN_IF:
90                 state = SEEN_NOTHING;
91                 if (seen == NEW) {
92                     String JavaDoc cName = getClassConstantOperand();
93                     if (cName.equals("java/lang/AssertionError")) break;
94                 }
95                 bugReporter.reportBug(bug);
96                 
97                     
98                 break;
99             case SEEN_GOTO:
100                 state = SEEN_NOTHING;
101                 break;
102         }
103     }
104 }
105
Popular Tags