KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 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 // 2: astore_1
27
// 3: monitorenter
28
// 4: aload_0
29
// 5: invokevirtual #13; //Method java/lang/Object.notify:()V
30
// 8: aload_1
31
// 9: monitorexit
32

33
34 public class FindUnconditionalWait extends BytecodeScanningDetector implements StatelessDetector {
35     int stage = 0;
36     private BugReporter bugReporter;
37
38     public FindUnconditionalWait(BugReporter bugReporter) {
39         this.bugReporter = bugReporter;
40     }
41
42
43     
44     @Override JavaDoc
45          public void visit(Method obj) {
46         stage = 0;
47     }
48
49     @Override JavaDoc
50          public void sawOffset(int offset) {
51         if (stage == 1) stage = 0;
52     }
53
54     @Override JavaDoc
55          public void sawOpcode(int seen) {
56         switch (stage) {
57         case 0:
58             if (seen == MONITORENTER)
59                 stage = 1;
60             break;
61         case 1:
62             if (seen == INVOKEVIRTUAL && getNameConstantOperand().equals("wait")) {
63                 bugReporter.reportBug(new BugInstance(this, "UW_UNCOND_WAIT",
64                         getSigConstantOperand().equals("()V") ? NORMAL_PRIORITY : LOW_PRIORITY)
65                         .addClassAndMethod(this)
66                         .addSourceLine(this));
67                 stage = 2;
68             }
69             break;
70         }
71     }
72 }
73
Popular Tags