KickJava   Java API By Example, From Geeks To Geeks.

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


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 edu.umd.cs.findbugs.ba.ClassContext;
25 import org.apache.bcel.classfile.*;
26
27 /**
28  * finds public classes that use 'this' as a semaphore, which can cause conflicts if clients of this
29  * class use an instance of this class as their own synchronization point. Frankly, Just calling
30  * synchronized on this, or defining synchronized methods is bad, but since that is so prevalent,
31  * don't warn on that.
32  */

33 public class PublicSemaphores extends BytecodeScanningDetector implements StatelessDetector
34 {
35     private static final int SEEN_NOTHING = 0;
36     private static final int SEEN_ALOAD_0 = 1;
37
38     private BugReporter bugReporter;
39     private int state;
40     private boolean alreadyReported;
41     
42     public PublicSemaphores(BugReporter bugReporter) {
43         this.bugReporter = bugReporter;
44     }
45     
46
47
48     @Override JavaDoc
49          public void visitClassContext(ClassContext classContext) {
50         JavaClass cls = classContext.getJavaClass();
51         if ((!cls.isPublic()) || (cls.getClassName().indexOf("$") >= 0))
52             return;
53         
54         alreadyReported = false;
55         super.visitClassContext(classContext);
56     }
57     
58     @Override JavaDoc
59          public void visit(Code obj) {
60         Method m = getMethod();
61         if (m.isStatic() || alreadyReported)
62             return;
63         
64         state = SEEN_NOTHING;
65         super.visit(obj);
66     }
67     
68     @Override JavaDoc
69          public void sawOpcode(int seen) {
70         if (alreadyReported)
71             return;
72         
73         switch (state) {
74             case SEEN_NOTHING:
75                 if (seen == ALOAD_0)
76                     state = SEEN_ALOAD_0;
77             break;
78             
79             case SEEN_ALOAD_0:
80                 if ((seen == INVOKEVIRTUAL)
81                 && getClassConstantOperand().equals("java/lang/Object")) {
82                     String JavaDoc methodName = getNameConstantOperand();
83                     if ("wait".equals(methodName) || "notify".equals(methodName) || "notifyAll".equals(methodName)) {
84                         bugReporter.reportBug( new BugInstance( this, "PS_PUBLIC_SEMAPHORES", NORMAL_PRIORITY )
85                                 .addClassAndMethod(this)
86                                 .addSourceLine(this));
87                         alreadyReported = true;
88                     }
89                 }
90                 state = SEEN_NOTHING;
91             break;
92         }
93         
94     }
95     
96 }
97
Popular Tags