KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.bcel.classfile.CodeException;
26 import org.apache.bcel.classfile.ExceptionTable;
27
28 import edu.umd.cs.findbugs.BugInstance;
29 import edu.umd.cs.findbugs.BugReporter;
30 import edu.umd.cs.findbugs.Detector;
31 import edu.umd.cs.findbugs.StatelessDetector;
32 import edu.umd.cs.findbugs.SystemProperties;
33 import edu.umd.cs.findbugs.ba.ClassContext;
34 import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
35
36 public class DontCatchIllegalMonitorStateException
37         extends PreorderVisitor implements Detector {
38
39     private static final boolean DEBUG = SystemProperties.getBoolean("dcimse.debug");
40
41     BugReporter bugReporter;
42     Set JavaDoc<String JavaDoc> msgs = null;
43     ClassContext classContext;
44
45     public DontCatchIllegalMonitorStateException(BugReporter bugReporter) {
46         this.bugReporter = bugReporter;
47         if (DEBUG)
48             msgs = new HashSet JavaDoc<String JavaDoc>();
49     }
50     
51
52
53     @Override JavaDoc
54          public void visit(ExceptionTable obj) {
55         if (DEBUG) {
56             String JavaDoc names[] = obj.getExceptionNames();
57             for (String JavaDoc name : names)
58                 if (name.equals("java.lang.Exception")
59                         || name.equals("java.lang.Throwable"))
60                     System.out.println(name + " thrown by " + getFullyQualifiedMethodName());
61         }
62     }
63
64     @Override JavaDoc
65          public void visit(CodeException obj) {
66         int type = obj.getCatchType();
67         if (type == 0) return;
68         String JavaDoc name = getConstantPool().constantToString(getConstantPool().getConstant(type));
69         if (DEBUG) {
70             String JavaDoc msg = "Catching " + name + " in " + getFullyQualifiedMethodName();
71             if (msgs.add(msg))
72                 System.out.println(msg);
73         }
74         if (name.equals("java.lang.IllegalMonitorStateException"))
75             bugReporter.reportBug(new BugInstance(this, "IMSE_DONT_CATCH_IMSE", HIGH_PRIORITY)
76                     .addClassAndMethod(this)
77                     .addSourceLine(this.classContext, this, obj.getHandlerPC()));
78
79     }
80
81     public void visitClassContext(ClassContext classContext) {
82         this.classContext = classContext;
83         classContext.getJavaClass().accept(this);
84     }
85
86     public void report() {
87     }
88 }
89
Popular Tags