[Fb-contrib-commit] SF.net SVN: fb-contrib:[1129] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-03-01 08:54:38
|
Revision: 1129
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1129&view=rev
Author: dbrosius
Date: 2009-03-01 08:54:28 +0000 (Sun, 01 Mar 2009)
Log Message:
-----------
new BED Detector
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
Added Paths:
-----------
trunk/fb-contrib/samples/BED_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BogusExceptionDeclaration.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2009-03-01 07:39:25 UTC (rev 1128)
+++ trunk/fb-contrib/etc/findbugs.xml 2009-03-01 08:54:28 UTC (rev 1129)
@@ -332,6 +332,10 @@
speed="moderate"
reports="DSOC_DUBIOUS_SET_OF_COLLECTIONS" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.BogusExceptionDeclaration"
+ speed="moderate"
+ reports="BED_BOGUS_EXCEPTION_DECLARATION" />
+
<!-- BugPattern -->
<BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING" category="PERFORMANCE" />
@@ -442,8 +446,9 @@
<BugPattern abbrev="JAO" type="JAO_JUNIT_ASSERTION_ODITIES_IMPOSSIBLE_NULL" category="CORRECTNESS" />
<BugPattern abbrev="SCA" type="SCA_SUSPICIOUS_CLONE_ALGORITHM" category="CORRECTNESS" />
<BugPattern abbrev="WEM" type="WEM_WEAK_EXCEPTION_MESSAGING" category="STYLE" />
- <BugPattern abbrev="SCSS" type="SCSS_SUSPICIOUS_CLUSTERED_SESSION_SUPPORT" category="CORRECTNESS" experimental="true" />
- <BugPattern abbrev="LO" type="LO_SUSPECT_LOG_CLASS" category="CORRECTNESS" experimental="true" />
- <BugPattern abbrev="IICU" type="IICU_INCORRECT_INTERNAL_CLASS_USE" category="CORRECTNESS" experimental="true" />
- <BugPattern abbrev="DSOC" type="DSOC_DUBIOUS_SET_OF_COLLECTIONS" category="PERFORMANCE" experimental="true" />
+ <BugPattern abbrev="SCSS" type="SCSS_SUSPICIOUS_CLUSTERED_SESSION_SUPPORT" category="CORRECTNESS" />
+ <BugPattern abbrev="LO" type="LO_SUSPECT_LOG_CLASS" category="CORRECTNESS" />
+ <BugPattern abbrev="IICU" type="IICU_INCORRECT_INTERNAL_CLASS_USE" category="CORRECTNESS" />
+ <BugPattern abbrev="DSOC" type="DSOC_DUBIOUS_SET_OF_COLLECTIONS" category="PERFORMANCE" />
+ <BugPattern abbrev="BED" type="BED_BOGUS_EXCEPTION_DECLARATION" category="CORRECTNESS" experimental="true" />
</FindbugsPlugin>
\ No newline at end of file
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2009-03-01 07:39:25 UTC (rev 1128)
+++ trunk/fb-contrib/etc/messages.xml 2009-03-01 08:54:28 UTC (rev 1129)
@@ -924,6 +924,17 @@
</Details>
</Detector>
+ <Detector class="com.mebigfatguy.fbcontrib.detect.BogusExceptionDeclaration">
+ <Details>
+ <![CDATA[
+ <p>looks for constructors, static methods and private methods that declare that they throw
+ checked exceptions that the actual code never throws. Since these methods can't be overridden,
+ there is no reason to add these exceptions to the method declaration.</p>
+ <p>It is a moderately fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
+
<!-- BugPattern -->
<BugPattern type="ISB_INEFFICIENT_STRING_BUFFERING">
@@ -2407,6 +2418,19 @@
</Details>
</BugPattern>
+ <BugPattern type="BED_BOGUS_EXCEPTION_DECLARATION">
+ <ShortDescription>non derivable method declares throwing an exception that isn't thrown</ShortDescription>
+ <LongDescription>non derivable method {1} declares throwing an exception that isn't thrown</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method declares that it throws a checked exception that it does not throw. As this method is
+ either a constructor, static method or private method, there is no reason for this method to declare
+ the exception in it's throws clause, and just causes calling methods to unnecessarily handle an exception
+ that will never be thrown. The exception in question should be removed from the throws clause.</p>
+ ]]>
+ </Details>
+ </BugPattern>
+
<!-- BugCode -->
<BugCode abbrev="ISB">Inefficient String Buffering</BugCode>
@@ -2484,4 +2508,5 @@
<BugCode abbrev="LO">Logger Oddities</BugCode>
<BugCode abbrev="IICU">Incorrect Internal Class use</BugCode>
<BugCode abbrev="DSOC">Dubious Set of Collections</BugCode>
+ <BugCode abbrev="BED">Bogus Exception Declaration</BugCode>
</MessageCollection>
\ No newline at end of file
Added: trunk/fb-contrib/samples/BED_Sample.java
===================================================================
--- trunk/fb-contrib/samples/BED_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/BED_Sample.java 2009-03-01 08:54:28 UTC (rev 1129)
@@ -0,0 +1,22 @@
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.zip.DataFormatException;
+
+
+public class BED_Sample
+{
+ public BED_Sample() throws IOException
+ {
+
+ }
+
+ private void badThrow() throws SQLException
+ {
+
+ }
+
+ public static void badStatic() throws DataFormatException
+ {
+
+ }
+}
Property changes on: trunk/fb-contrib/samples/BED_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BogusExceptionDeclaration.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BogusExceptionDeclaration.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BogusExceptionDeclaration.java 2009-03-01 08:54:28 UTC (rev 1129)
@@ -0,0 +1,113 @@
+package com.mebigfatguy.fbcontrib.detect;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.bcel.Repository;
+import org.apache.bcel.classfile.Code;
+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.BytecodeScanningDetector;
+import edu.umd.cs.findbugs.ba.ClassContext;
+import edu.umd.cs.findbugs.ba.XMethod;
+
+/**
+ * looks for constructors, private methods or static methods that declare that they
+ * throw specific checked exceptions, but that do not. This just causes callers of
+ * these methods to do extra work to handle an exception that will never be thrown.
+ */
+public class BogusExceptionDeclaration extends BytecodeScanningDetector {
+ private static JavaClass runtimeExceptionClass;
+ static {
+ try {
+ runtimeExceptionClass = Repository.lookupClass("java/lang/RuntimeException");
+ } catch (ClassNotFoundException cnfe) {
+ runtimeExceptionClass = null;
+ }
+ }
+ private final BugReporter bugReporter;
+ private Set<String> declaredCheckedExceptions;
+
+ public BogusExceptionDeclaration(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+
+ @Override
+ public void visitClassContext(ClassContext classContext) {
+ try {
+ if (runtimeExceptionClass != null) {
+ declaredCheckedExceptions = new HashSet<String>();
+ super.visitClassContext(classContext);
+ }
+ } finally {
+ declaredCheckedExceptions = null;
+ }
+ }
+
+ /**
+ * implements the visitor to see if the method declares that it throws any
+ * checked exceptions.
+ *
+ * @param obj the context object of the currently parsed code block
+ */
+ @Override
+ public void visitCode(Code obj) {
+ Method method = getMethod();
+ if (method.isStatic() || method.isPrivate() || "<init>".equals(method.getName())) {
+ ExceptionTable et = method.getExceptionTable();
+ if (et != null) {
+ String[] exNames = et.getExceptionNames();
+ for (String exName : exNames) {
+ try {
+ JavaClass exCls = Repository.lookupClass(exName);
+ if (!exCls.instanceOf(runtimeExceptionClass)) {
+ declaredCheckedExceptions.add(exName);
+ }
+ } catch (ClassNotFoundException cnfe) {
+ bugReporter.reportMissingClass(cnfe);
+ }
+ }
+ if (!declaredCheckedExceptions.isEmpty()) {
+ super.visitCode(obj);
+ if (!declaredCheckedExceptions.isEmpty()) {
+ BugInstance bi = new BugInstance(this, "BED_BOGUS_EXCEPTION_DECLARATION", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this, 0);
+ for (String ex : declaredCheckedExceptions) {
+ bi.addString(ex.replaceAll("/", "."));
+ }
+ bugReporter.reportBug(bi);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * implements the visitor to look for method calls that could throw the exceptions
+ * that are listed in the declaration.
+ */
+ @Override
+ public void sawOpcode(int seen) {
+ if (declaredCheckedExceptions.isEmpty()) {
+ return;
+ }
+
+ if ((seen == INVOKEVIRTUAL)
+ || (seen == INVOKEINTERFACE)
+ || (seen == INVOKESPECIAL)
+ || (seen == INVOKESTATIC)) {
+ XMethod method = getXMethod();
+ String[] thrownExceptions = method.getThrownExceptions();
+ for (String thrownException : thrownExceptions) {
+ declaredCheckedExceptions.remove(thrownException.replaceAll("/", "."));
+ }
+ }
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BogusExceptionDeclaration.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|