[Fb-contrib-commit] SF.net SVN: fb-contrib:[1688] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2011-06-13 06:45:57
|
Revision: 1688
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1688&view=rev
Author: dbrosius
Date: 2011-06-13 06:45:45 +0000 (Mon, 13 Jun 2011)
Log Message:
-----------
initial stub in of STB - non functional
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
Added Paths:
-----------
trunk/fb-contrib/samples/STB_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2011-06-13 05:07:07 UTC (rev 1687)
+++ trunk/fb-contrib/etc/findbugs.xml 2011-06-13 06:45:45 UTC (rev 1688)
@@ -222,6 +222,7 @@
<Detector class="com.mebigfatguy.fbcontrib.detect.LingeringGraphicsObjects" speed="fast" reports="LGO_LINGERING_GRAPHICS_OBJECT" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.StackedTryBlocks" speed="fast" reports="STB_STACKED_TRY_BLOCKS" />
<!-- BugPattern -->
@@ -388,4 +389,5 @@
<BugPattern abbrev="SEC" type="SEC_SIDE_EFFECT_CONSTRUCTOR" category="STYLE" experimental="true" />
<BugPattern abbrev="SGSU" type="SGSU_SUSPICIOUS_GETTER_SETTER_USE" category="CORRECTNESS" experimental="true" />
<BugPattern abbrev="LGO" type="LGO_LINGERING_GRAPHICS_OBJECT" category="PERFORMANCE" experimental="true" />
+ <BugPattern abbrev="STB" type="STB_STACKED_TRY_BLOCKS" category="STYLE" experimental="true" />
</FindbugsPlugin>
Modified: trunk/fb-contrib/etc/messages.xml
===================================================================
--- trunk/fb-contrib/etc/messages.xml 2011-06-13 05:07:07 UTC (rev 1687)
+++ trunk/fb-contrib/etc/messages.xml 2011-06-13 06:45:45 UTC (rev 1688)
@@ -1204,6 +1204,18 @@
]]>
</Details>
</Detector>
+
+ <Detector class="com.mebigfatguy.fbcontrib.detect.StackedTryBlocks">
+ <Details>
+ <![CDATA[
+ <p>This detector looks for two or more try catch blocks that are consecutive
+ and catch the same kind of exception, and each catch block mandatorily throws
+ the same exception. These two catch blocks can and should be made into one
+ catch block to simply the code.</p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
<!-- BugPattern -->
@@ -3302,6 +3314,19 @@
]]>
</Details>
</BugPattern>
+
+ <BugPattern type="STB_STACKED_TRY_BLOCKS">
+ <ShortDescription>Method stacks similar try/catch blocks</ShortDescription>
+ <LongDescription>Method {1} stacks similar try/catch blocks</LongDescription>
+ <Details>
+ <![CDATA[
+ <P>This method declares two try catch blocks one after another, where each
+ catch block catches the same type of exception. They also throw uniformly the
+ same type of exception. These two catch blocks can be combined into one to
+ simplify the method.</p>
+ ]]>
+ </Details>
+ </BugPattern>
<!-- BugCode -->
@@ -3404,4 +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>
</MessageCollection>
Added: trunk/fb-contrib/samples/STB_Sample.java
===================================================================
--- trunk/fb-contrib/samples/STB_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/STB_Sample.java 2011-06-13 06:45:45 UTC (rev 1688)
@@ -0,0 +1,23 @@
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class STB_Sample {
+ public void testSTB(File f1, File f2) throws STBException {
+ 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 {
+ }
+}
Property changes on: trunk/fb-contrib/samples/STB_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.java 2011-06-13 06:45:45 UTC (rev 1688)
@@ -0,0 +1,165 @@
+package com.mebigfatguy.fbcontrib.detect;
+
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.bcel.classfile.Code;
+import org.apache.bcel.classfile.CodeException;
+
+import edu.umd.cs.findbugs.BugReporter;
+import edu.umd.cs.findbugs.BytecodeScanningDetector;
+
+/**
+ * looks for two or more try catch blocks that are consecutive and catch the
+ * same kind of exception, and throw the same exception always. These blocks can
+ * be coalesced into one.
+ */
+
+public class StackedTryBlocks extends BytecodeScanningDetector {
+
+ private final BugReporter bugReporter;
+ private Map<TryBlock, TryBlock> blocks;
+ private List<TryBlock> inBlocks;
+
+ public StackedTryBlocks(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+ @Override
+ public void visitCode(Code obj) {
+
+ try {
+ blocks = new HashMap<TryBlock, TryBlock>();
+ inBlocks = new ArrayList<TryBlock>();
+
+ CodeException[] ces = obj.getExceptionTable();
+ for (CodeException ce : ces) {
+ TryBlock tb = new TryBlock(ce);
+ TryBlock block = blocks.get(tb);
+ if (block != null) {
+ block.addCatchType(ce);
+ } else {
+ blocks.put(tb, tb);
+ }
+ }
+
+ Iterator<TryBlock> it = blocks.keySet().iterator();
+ int numSingleCatchBlocks = 0;
+ while (it.hasNext()) {
+ if (!it.next().hasMultipleHandlers()) {
+ numSingleCatchBlocks++;
+ }
+ }
+
+ if (numSingleCatchBlocks > 1) {
+ super.visitCode(obj);
+ }
+ } finally {
+ blocks = null;
+ inBlocks = null;
+ }
+ }
+
+ @Override
+ public void sawOpcode(int seen) {
+
+ TryBlock block = findBlockWithStart();
+ if (block != null) {
+ inBlocks.add(block);
+ }
+
+ if (inBlocks.size() > 0) {
+ int nextPC = getNextPC();
+ TryBlock innerBlock = inBlocks.get(inBlocks.size() - 1);
+ if (nextPC == innerBlock.getHandlerPC()) {
+ if ((seen == GOTO) || (seen == GOTO_W)) {
+ innerBlock.setEndHandlerPC(getBranchTarget());
+ } else {
+ innerBlock.setEndHandlerPC(getCode().getLength());
+ }
+ }
+ }
+
+ }
+
+ private TryBlock findBlockWithStart() {
+
+ int pc = getPC();
+
+ for (TryBlock block : blocks.keySet()) {
+ if (block.getStartPC() == pc) {
+ return block;
+ }
+ }
+
+ return null;
+ }
+
+ static class TryBlock {
+ int startPC;
+ int endPC;
+ int handlerPC;
+ int endHandlerPC;
+ BitSet catchTypes;
+ int throwType;
+
+ public TryBlock(CodeException ce) {
+ startPC = ce.getStartPC();
+ endPC = ce.getEndPC();
+ handlerPC = ce.getHandlerPC();
+ catchTypes = new BitSet();
+ catchTypes.set(ce.getCatchType());
+ }
+
+ public void addCatchType(CodeException ce) {
+ catchTypes.set(ce.getCatchType());
+ }
+
+ public boolean hasMultipleHandlers() {
+ int bit = catchTypes.nextSetBit(0);
+ return catchTypes.nextSetBit(bit + 1) >= 0;
+ }
+
+ public void setEndHandlerPC(int end) {
+ endHandlerPC = end;
+ }
+
+ public void setThrowType(int type) {
+ throwType = type;
+ }
+
+ public int getStartPC() {
+ return startPC;
+ }
+
+ public int getHandlerPC() {
+ return handlerPC;
+ }
+
+ @Override
+ public int hashCode() {
+ return startPC ^ endPC;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof TryBlock) {
+ TryBlock that = (TryBlock) o;
+ return (startPC == that.startPC) && (endPC == that.endPC);
+ }
+
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return "{" + startPC + " -> " + endPC + "} (catch "
+ + catchTypes.get(0) + ") {" + handlerPC + " -> "
+ + endHandlerPC + "}";
+ }
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/StackedTryBlocks.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.
|