[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect DeclaredRuntimeException.java,NO
Brought to you by:
dbrosius
From: Dave B. <dbr...@us...> - 2005-09-15 04:03:21
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20538/src/com/mebigfatguy/fbcontrib/detect Added Files: DeclaredRuntimeException.java Log Message: Initial Checkin: DRE --- NEW FILE: DeclaredRuntimeException.java --- package com.mebigfatguy.fbcontrib.detect; import java.util.HashSet; import java.util.Set; import org.apache.bcel.Repository; import org.apache.bcel.classfile.ExceptionTable; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.Method; import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; import edu.umd.cs.findbugs.Detector; import edu.umd.cs.findbugs.StatelessDetector; import edu.umd.cs.findbugs.StringAnnotation; import edu.umd.cs.findbugs.ba.ClassContext; import edu.umd.cs.findbugs.visitclass.PreorderVisitor; public class DeclaredRuntimeException extends PreorderVisitor implements Detector, StatelessDetector { private BugReporter bugReporter; private static final Set<String> runtimeExceptions = new HashSet<String>(); private static final Set<String> notFoundExceptions = new HashSet<String>(); private static JavaClass runtimeExceptionClass; { try { runtimeExceptionClass = Repository.lookupClass("java.lang.RuntimeException"); } catch (ClassNotFoundException cnfe) { runtimeExceptionClass = null; } } public DeclaredRuntimeException(BugReporter bugReporter) { this.bugReporter = bugReporter; } public Object clone() throws CloneNotSupportedException { return super.clone(); } public void visitClassContext(ClassContext classContext) { if (runtimeExceptionClass != null) classContext.getJavaClass().accept(this); } public void visitMethod(Method obj) { ExceptionTable et = obj.getExceptionTable(); if (et != null) { String[] exNames = et.getExceptionNames(); Set<String> methodRTExceptions = new HashSet<String>(); boolean foundRuntime = false; for (String ex : exNames) { boolean isRuntime = false; if (runtimeExceptions.contains(ex)) isRuntime = true; else { try { JavaClass exClass = Repository.lookupClass(ex); if (exClass.instanceOf(runtimeExceptionClass)) { runtimeExceptions.add(ex); isRuntime = true; } } catch (ClassNotFoundException cnfe) { if (!notFoundExceptions.contains(ex)) { bugReporter.reportMissingClass(cnfe); notFoundExceptions.add(ex); } } } if (isRuntime) { foundRuntime = true; methodRTExceptions.add(ex); } } if (foundRuntime) { BugInstance bug = new BugInstance(this, "DRE_DECLARED_RUNTIME_EXCEPTION", NORMAL_PRIORITY) .addClass(this) .addMethod(this); for (String ex : methodRTExceptions) { bug.add(new StringAnnotation(ex)); } bugReporter.reportBug(bug); } } } public void report() { } } |