KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.bcel.classfile.*;
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 FindNakedNotify extends BytecodeScanningDetector implements StatelessDetector {
35     int stage = 0;
36     private BugReporter bugReporter;
37     boolean synchronizedMethod;
38     private int notifyPC;
39
40     public FindNakedNotify(BugReporter bugReporter) {
41         this.bugReporter = bugReporter;
42     }
43
44
45
46     @Override JavaDoc
47          public void visit(Method obj) {
48         int flags = obj.getAccessFlags();
49         synchronizedMethod = (flags & ACC_SYNCHRONIZED) != 0;
50     }
51
52     @Override JavaDoc
53          public void visit(Code obj) {
54         stage = synchronizedMethod ? 1 : 0;
55         super.visit(obj);
56         if (synchronizedMethod && stage == 4)
57             bugReporter.reportBug(new BugInstance(this, "NN_NAKED_NOTIFY", NORMAL_PRIORITY)
58                     .addClassAndMethod(this)
59                     .addSourceLine(this, notifyPC));
60     }
61
62     @Override JavaDoc
63          public void sawOpcode(int seen) {
64         switch (stage) {
65         case 0:
66             if (seen == MONITORENTER)
67                 stage = 1;
68             break;
69         case 1:
70             stage = 2;
71             break;
72         case 2:
73             if (seen == INVOKEVIRTUAL
74                     && (getNameConstantOperand().equals("notify")
75                     || getNameConstantOperand().equals("notifyAll"))
76                     && getSigConstantOperand().equals("()V")) {
77                 stage = 3;
78                 notifyPC = getPC();
79             } else
80                 stage = 0;
81             break;
82         case 3:
83             stage = 4;
84             break;
85         case 4:
86             if (seen == MONITOREXIT) {
87                 bugReporter.reportBug(new BugInstance(this, "NN_NAKED_NOTIFY", NORMAL_PRIORITY)
88                         .addClassAndMethod(this)
89                         .addSourceLine(this, notifyPC));
90                 stage = 5;
91             } else
92                 stage = 0;
93             break;
94         case 5:
95             break;
96         default:
97             assert false;
98         }
99
100     }
101 }
102
Popular Tags