[Fb-contrib-commit] SF.net SVN: fb-contrib:[1153] trunk/fb-contrib
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2009-04-11 02:50:20
|
Revision: 1153
http://fb-contrib.svn.sourceforge.net/fb-contrib/?rev=1153&view=rev
Author: dbrosius
Date: 2009-04-11 02:50:18 +0000 (Sat, 11 Apr 2009)
Log Message:
-----------
new UNNC detector
Modified Paths:
--------------
trunk/fb-contrib/etc/findbugs.xml
trunk/fb-contrib/etc/messages.xml
Added Paths:
-----------
trunk/fb-contrib/samples/UNNC_Sample.java
trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UnnecessaryNewNullCheck.java
Modified: trunk/fb-contrib/etc/findbugs.xml
===================================================================
--- trunk/fb-contrib/etc/findbugs.xml 2009-04-10 15:44:00 UTC (rev 1152)
+++ trunk/fb-contrib/etc/findbugs.xml 2009-04-11 02:50:18 UTC (rev 1153)
@@ -336,6 +336,10 @@
speed="moderate"
reports="BED_BOGUS_EXCEPTION_DECLARATION" />
+ <Detector class="com.mebigfatguy.fbcontrib.detect.UnnecessaryNewNullCheck"
+ speed="fast"
+ reports="UNNC_UNNECESSARY_NEW_NULL_CHECK" />
+
<!-- BugPattern -->
<BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING" category="PERFORMANCE" />
@@ -452,4 +456,5 @@
<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" />
+ <BugPattern abbrev="UNNC" type="UNNC_UNNECESSARY_NEW_NULL_CHECK" 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-04-10 15:44:00 UTC (rev 1152)
+++ trunk/fb-contrib/etc/messages.xml 2009-04-11 02:50:18 UTC (rev 1153)
@@ -935,6 +935,18 @@
</Details>
</Detector>
+ <Detector class="com.mebigfatguy.fbcontrib.detect.UnnecessaryNewNullCheck">
+ <Details>
+ <![CDATA[
+ <p>looks for allocations of objects, and then immediately checking to see if the
+ object is null, or non null. As the new operator is guaranteed to eiher succeed, or throw
+ an exception, this null check is useless, and denotes a misunderstanding as to how
+ the jvm works. You can remove this guard.</p>
+ <p>It is a fast detector</p>
+ ]]>
+ </Details>
+ </Detector>
+
<!-- BugPattern -->
<BugPattern type="ISB_INEFFICIENT_STRING_BUFFERING">
@@ -2444,6 +2456,18 @@
</Details>
</BugPattern>
+ <BugPattern type="UNNC_UNNECESSARY_NEW_NULL_CHECK">
+ <ShortDescription>method checks the result of a new allocation</ShortDescription>
+ <LongDescription>method {1} checks the result of a new allocation</LongDescription>
+ <Details>
+ <![CDATA[
+ <p>This method allocations an object with new, and then checks that the object is null
+ or non null. As the new operator is guaranteed to either succeed or throw an exception,
+ this null check is unnecessary and can be removed.
+ ]]>
+ </Details>
+ </BugPattern>
+
<!-- BugCode -->
<BugCode abbrev="ISB">Inefficient String Buffering</BugCode>
@@ -2522,4 +2546,5 @@
<BugCode abbrev="IICU">Incorrect Internal Class use</BugCode>
<BugCode abbrev="DSOC">Dubious Set of Collections</BugCode>
<BugCode abbrev="BED">Bogus Exception Declaration</BugCode>
+ <BugCode abbrev="UNNC">Unnecessary New Null Check</BugCode>
</MessageCollection>
\ No newline at end of file
Added: trunk/fb-contrib/samples/UNNC_Sample.java
===================================================================
--- trunk/fb-contrib/samples/UNNC_Sample.java (rev 0)
+++ trunk/fb-contrib/samples/UNNC_Sample.java 2009-04-11 02:50:18 UTC (rev 1153)
@@ -0,0 +1,55 @@
+
+
+public class UNNC_Sample {
+ public void testPosNEW()
+ {
+ UNNC_Sample sample = new UNNC_Sample();
+
+ if (sample != null)
+ System.out.println("OK");
+ }
+
+ public void testNegNEW()
+ {
+ UNNC_Sample sample = new UNNC_Sample();
+
+ if (sample == null)
+ return;
+
+ System.out.println("OK");
+ }
+
+ public void testPosANEWARAY()
+ {
+ String[] s = new String[10];
+ if (s != null)
+ System.out.println("OK");
+ }
+
+ public void testNegANEWARRAY()
+ {
+ String[] s = new String[10];
+ if (s == null)
+ return;
+
+ System.out.println("OK");
+ }
+
+ public void testPosMULTIANEWARRAY()
+ {
+ String[][] s = new String[10][5];
+ if (s != null)
+ System.out.println("OK");
+ }
+
+ public void testNegMULTIANEWARRAY()
+ {
+ String[][] s = new String[10][5];
+ if (s == null)
+ return;
+
+ System.out.println("OK");
+ }
+
+
+}
Property changes on: trunk/fb-contrib/samples/UNNC_Sample.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UnnecessaryNewNullCheck.java
===================================================================
--- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UnnecessaryNewNullCheck.java (rev 0)
+++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UnnecessaryNewNullCheck.java 2009-04-11 02:50:18 UTC (rev 1153)
@@ -0,0 +1,140 @@
+package com.mebigfatguy.fbcontrib.detect;
+
+import java.util.BitSet;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.bcel.Constants;
+import org.apache.bcel.classfile.Code;
+import org.apache.bcel.classfile.CodeException;
+
+import com.mebigfatguy.fbcontrib.utils.Integer14;
+import com.mebigfatguy.fbcontrib.utils.RegisterUtils;
+
+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;
+
+/** looks for construction of new objects, and then the immediate testing
+ * whether the object is null or not. As the new operator will always succeed,
+ * or through an exception, this test is unnecessary and represents a misunderstanding
+ * as to how the jvm works.
+ */
+public class UnnecessaryNewNullCheck extends BytecodeScanningDetector
+{
+ private final BugReporter bugReporter;
+ private OpcodeStack stack;
+ private Set<Integer> allocationRegs;
+ private Set<Integer> handlerStarts;
+
+ public UnnecessaryNewNullCheck(BugReporter bugReporter) {
+ this.bugReporter = bugReporter;
+ }
+
+ @Override
+ public void visitClassContext(ClassContext classContext) {
+ try {
+ stack = new OpcodeStack();
+ allocationRegs = new HashSet<Integer>();
+ handlerStarts = new HashSet<Integer>();
+ super.visitClassContext(classContext);
+ } finally {
+ stack = null;
+ allocationRegs = null;
+ handlerStarts = null;
+ }
+ }
+
+ @Override
+ public void visitCode(Code obj) {
+ if (prescreen()) {
+ stack.resetForMethodEntry(this);
+ allocationRegs.clear();
+ handlerStarts.clear();
+
+ CodeException[] ce = obj.getExceptionTable();
+ if (ce != null) {
+ for (CodeException element : ce) {
+ handlerStarts.add(Integer14.valueOf(element.getHandlerPC()));
+ }
+ }
+ super.visitCode(obj);
+ }
+ }
+
+ @Override
+ public void sawOpcode(int seen) {
+ boolean sawAlloc = false;
+ try {
+ switch (seen) {
+ case NEW:
+ case ANEWARRAY:
+ case MULTIANEWARRAY:
+ sawAlloc = true;
+ break;
+
+ case ASTORE:
+ case ASTORE_0:
+ case ASTORE_1:
+ case ASTORE_2:
+ case ASTORE_3:
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ int reg = RegisterUtils.getAStoreReg(this, seen);
+ if (item.getUserValue() != null) {
+ allocationRegs.add(Integer14.valueOf(reg));
+ } else {
+ allocationRegs.remove(Integer14.valueOf(reg));
+ }
+ }
+ break;
+
+ case ALOAD:
+ case ALOAD_0:
+ case ALOAD_1:
+ case ALOAD_2:
+ case ALOAD_3:
+ int reg = RegisterUtils.getALoadReg(this, seen);
+ if (allocationRegs.contains(Integer14.valueOf(reg))) {
+ sawAlloc = true;
+ }
+ break;
+
+ case IFNONNULL:
+ case IFNULL:
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ if (item.getUserValue() != null) {
+ bugReporter.reportBug(new BugInstance(this, "UNNC_UNNECESSARY_NEW_NULL_CHECK", NORMAL_PRIORITY)
+ .addClass(this)
+ .addMethod(this)
+ .addSourceLine(this));
+ }
+ }
+ break;
+ }
+
+ if (handlerStarts.contains(Integer14.valueOf(getPC())))
+ allocationRegs.clear();
+
+ } finally {
+ stack.sawOpcode(this, seen);
+ if (sawAlloc) {
+ if (stack.getStackDepth() > 0) {
+ OpcodeStack.Item item = stack.getStackItem(0);
+ item.setUserValue(Boolean.TRUE);
+ }
+ }
+ }
+ }
+
+ private boolean prescreen() {
+ BitSet bytecodeSet = getClassContext().getBytecodeSet(getMethod());
+ return (bytecodeSet != null) &&
+ (bytecodeSet.get(Constants.NEW)
+ || bytecodeSet.get(Constants.ANEWARRAY)
+ || bytecodeSet.get(Constants.MULTIANEWARRAY));
+ }
+}
Property changes on: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UnnecessaryNewNullCheck.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.
|