1 19 20 package edu.umd.cs.findbugs.detect; 21 22 import java.util.HashSet ; 23 import java.util.Set ; 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 <String > msgs = null; 43 ClassContext classContext; 44 45 public DontCatchIllegalMonitorStateException(BugReporter bugReporter) { 46 this.bugReporter = bugReporter; 47 if (DEBUG) 48 msgs = new HashSet <String >(); 49 } 50 51 52 53 @Override 54 public void visit(ExceptionTable obj) { 55 if (DEBUG) { 56 String names[] = obj.getExceptionNames(); 57 for (String 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 65 public void visit(CodeException obj) { 66 int type = obj.getCatchType(); 67 if (type == 0) return; 68 String name = getConstantPool().constantToString(getConstantPool().getConstant(type)); 69 if (DEBUG) { 70 String 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 |