KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004-2006 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 org.apache.bcel.classfile.Method;
25
26 /**
27  * Find comparisons involving values computed with bitwise
28  * operations whose outcomes are fixed at compile time.
29  *
30  * @author Tom Truscott <trt@unx.sas.com>
31  */

32 public class IncompatMask extends BytecodeScanningDetector implements StatelessDetector {
33     int state;
34     long arg0, arg1;
35     int bitop;
36
37     private BugReporter bugReporter;
38
39     public IncompatMask(BugReporter bugReporter) {
40         this.state = 0;
41         this.bugReporter = bugReporter;
42     }
43
44
45
46     @Override JavaDoc
47          public void visit(Method obj) {
48         super.visit(obj);
49         this.state = 0;
50     }
51
52     private void checkState(int expectedState) {
53         if (state == expectedState)
54             state++;
55         else
56             state = 0;
57     }
58
59     private void noteVal(long val) {
60         if (state == 0)
61             arg0 = val;
62         else if (state == 2)
63             arg1 = val;
64         else
65             state = -1;
66         state++;
67     }
68
69     @Override JavaDoc
70          public void sawInt(int val) {
71         noteVal(val);
72     }
73
74     @Override JavaDoc
75          public void sawLong(long val) {
76         noteVal(val);
77     }
78
79     @Override JavaDoc
80          public void sawOpcode(int seen) {
81         // System.out.println("BIT: " + state + ": " + OPCODE_NAMES[seen]);
82

83         switch (seen) {
84         case ICONST_M1:
85             noteVal(-1);
86             return;
87         case ICONST_0:
88             noteVal(0);
89             return;
90         case ICONST_1:
91             noteVal(1);
92             return;
93         case ICONST_2:
94             noteVal(2);
95             return;
96         case ICONST_3:
97             noteVal(3);
98             return;
99         case ICONST_4:
100             noteVal(4);
101             return;
102         case ICONST_5:
103             noteVal(5);
104             return;
105         case LCONST_0:
106             noteVal(0);
107             return;
108         case LCONST_1:
109             noteVal(1);
110             return;
111
112         case BIPUSH:
113             return; /* will pick up value via sawInt */
114         case LDC2_W:
115             return; /* will pick up value via sawLong */
116
117         case SIPUSH:
118             return; /* will pick up value via sawInt */
119         case LDC:
120             return; /* will pick up value via sawInt */
121
122         case IAND:
123         case LAND:
124             bitop = IAND;
125             checkState(1);
126             return;
127         case IOR:
128         case LOR:
129             bitop = IOR;
130             checkState(1);
131             return;
132
133         case LCMP:
134             return; /* Ignore. An 'if' opcode will follow */
135
136         case IFEQ:
137         case IFNE:
138             /* special case: if arg1 is 0 it will not be pushed */
139             if (state == 2) {
140                 arg1 = 0;
141                 state = 3;
142             }
143             /* fallthrough */
144
145         case IF_ICMPEQ:
146         case IF_ICMPNE:
147             checkState(3);
148             if (state != 4)
149                 return;
150             break; /* the only break in this switch! gross */
151
152         case GOTO:
153             state = -1;
154             return;
155
156         default:
157             state = 0;
158             return;
159         }
160
161
162         /* We have matched the instruction pattern, so check the args */
163         long dif;
164         String JavaDoc t;
165
166         if (bitop == IOR) {
167             dif = arg0 & ~arg1;
168             t = "BIT_IOR";
169         } else if (arg0 != 0 || arg1 != 0) {
170             dif = arg1 & ~arg0;
171             t = "BIT_AND";
172         } else {
173             dif = 1;
174             t = "BIT_AND_ZZ";
175         }
176
177         if (dif != 0) {
178             // System.out.println("Match at offset " + getPC());
179
bugReporter.reportBug(new BugInstance(this, t, NORMAL_PRIORITY)
180                     .addClassAndMethod(this)
181                     .addSourceLine(this));
182         }
183         state = 0;
184     }
185 }
186
187 // vim:ts=4
188
Popular Tags