[Fb-contrib-commit] SF.net SVN: fb-contrib:[1694] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2011-07-02 03:47:01
|
Revision: 1694
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1694&view=rev
Author: dbrosius
Date: 2011-07-02 03:46:55 +0000 (Sat, 02 Jul 2011)
Log Message:
-----------
ignore STBs when the method declares throwing the same exception type as is caught
Modified Paths:
--------------
trunk/fb-contrib/etc/messages.xml
trunk/fb-contrib/samples/STB_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2011-07-02 03:46:18 UTC (rev 1693)
+++ trunk/fb-contrib/etc/messages.xml 2011-07-02 03:46:55 UTC (rev 1694)
@@ -3429,5 +3429,5 @@
<BugCode abbrev="SEC">Side Effect Constructor</BugCode>
<BugCode abbrev="SGSU">Suspicious Getter Setter Use</BugCode>
<BugCode abbrev="LGO">Lingering Graphics Object</BugCode>
- <BugCode abbrev="STB">Stacked Catch Blocks</BugCode>
+ <BugCode abbrev="STB">Stacked Try Blocks</BugCode>
</MessageCollection>
Modified: trunk/fb-contrib/samples/STB_Sample.java
===================================================================
--- trunk/fb-contrib/samples/STB_Sample.java 2011-07-02 03:46:18 UTC (rev 1693)
+++ trunk/fb-contrib/samples/STB_Sample.java 2011-07-02 03:46:55 UTC (rev 1694)
@@ -18,6 +18,20 @@
}
}
+ public void fpTestMethodDeclaresThrownType(File f1, File f2) throws STBException, IOException {
+ try {
+ InputStream is = new FileInputStream(f1);
+ } catch (IOException ioe) {
+ throw new STBException();
+ }
+
+ try {
+ InputStream is = new FileInputStream(f2);
+ } catch (IOException ioe) {
+ throw new STBException();
+ }
+ }
+
static class STBException extends Exception {
}
}
Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java 2011-07-02 03:46:18 UTC (rev 1693)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java 2011-07-02 03:46:55 UTC (rev 1694)
@@ -1,19 +1,25 @@
package com.mebigfatguy.fbcontrib.detect;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.BitSet;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.CodeException;
+import org.apache.bcel.classfile.ConstantClass;
+import org.apache.bcel.classfile.ConstantPool;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.BytecodeScanningDetector;
import edu.umd.cs.findbugs.OpcodeStack;
import edu.umd.cs.findbugs.ba.ClassContext;
+import edu.umd.cs.findbugs.ba.XMethod;
/**
* looks for two or more try catch blocks that are consecutive and catch the
@@ -46,6 +52,11 @@
public void visitCode(Code obj) {
try {
+ XMethod xMethod = getXMethod();
+ String[] tes = xMethod.getThrownExceptions();
+ Set<String> thrownExceptions = new HashSet<String>(Arrays.<String> asList((tes == null) ? new String[0]
+ : tes));
+
blocks = new ArrayList<TryBlock>();
inBlocks = new ArrayList<TryBlock>();
@@ -64,7 +75,8 @@
Iterator<TryBlock> it = blocks.iterator();
while (it.hasNext()) {
TryBlock block = it.next();
- if (block.hasMultipleHandlers() || block.isFinally()) {
+ if (block.hasMultipleHandlers() || block.isFinally()
+ || block.catchIsThrown(getConstantPool(), thrownExceptions)) {
it.remove();
}
}
@@ -78,20 +90,12 @@
for (int i = 1; i < blocks.size(); i++) {
TryBlock secondBlock = blocks.get(i);
- if ((firstBlock.getCatchType() == secondBlock
- .getCatchType())
- && (firstBlock.getThrowSignature()
- .equals(secondBlock.getThrowSignature()))) {
- bugReporter.reportBug(new BugInstance(this,
- "STB_STACKED_TRY_BLOCKS", NORMAL_PRIORITY)
- .addClass(this)
- .addMethod(this)
- .addSourceLineRange(this,
- firstBlock.getStartPC(),
- firstBlock.getEndHandlerPC())
- .addSourceLineRange(this,
- secondBlock.getStartPC(),
- secondBlock.getEndHandlerPC()));
+ if ((firstBlock.getCatchType() == secondBlock.getCatchType())
+ && (firstBlock.getThrowSignature().equals(secondBlock.getThrowSignature()))) {
+ bugReporter.reportBug(new BugInstance(this, "STB_STACKED_TRY_BLOCKS", NORMAL_PRIORITY)
+ .addClass(this).addMethod(this)
+ .addSourceLineRange(this, firstBlock.getStartPC(), firstBlock.getEndHandlerPC())
+ .addSourceLineRange(this, secondBlock.getStartPC(), secondBlock.getEndHandlerPC()));
}
@@ -136,8 +140,7 @@
if (innerBlock.inCatch()) {
if (((seen >= Constants.IFEQ) && ((seen <= Constants.RET)))
- || ((seen >= Constants.IRETURN) && (seen <= Constants.RETURN))
- || (seen == GOTO_W)) {
+ || ((seen >= Constants.IRETURN) && (seen <= Constants.RETURN)) || (seen == GOTO_W)) {
blocks.remove(innerBlock);
inBlocks.remove(inBlocks.size() - 1);
} else if (seen == ATHROW) {
@@ -212,6 +215,15 @@
return catchTypes.get(0);
}
+ public boolean catchIsThrown(ConstantPool pool, Set<String> thrownExceptions) {
+ if (thrownExceptions.size() > 0) {
+ int exIndex = catchTypes.nextSetBit(0);
+ String exName = ((ConstantClass) pool.getConstant(exIndex)).getBytes(pool);
+ return thrownExceptions.contains(exName);
+ }
+ return false;
+ }
+
public void setEndHandlerPC(int end) {
endHandlerPC = end;
}
@@ -221,8 +233,7 @@
}
public String getThrowSignature() {
- return (throwSig == null) ? String.valueOf(System
- .identityHashCode(this)) : throwSig;
+ return (throwSig == null) ? String.valueOf(System.identityHashCode(this)) : throwSig;
}
public int getStartPC() {
@@ -266,8 +277,7 @@
@Override
public String toString() {
- return "{" + startPC + " -> " + endPC + "} (catch "
- + catchTypes.nextSetBit(0) + ") {" + handlerPC + " -> "
+ return "{" + startPC + " -> " + endPC + "} (catch " + catchTypes.nextSetBit(0) + ") {" + handlerPC + " -> "
+ endHandlerPC + "}";
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|