[Fb-contrib-commit] SF.net SVN: fb-contrib: [864] trunk/fb-contrib
Brought to you by:
dbrosius
From: <dbr...@us...> - 2007-02-20 01:35:54
|
Revision: 864 http://svn.sourceforge.net/fb-contrib/?rev=864&view=rev Author: dbrosius Date: 2007-02-19 17:35:46 -0800 (Mon, 19 Feb 2007) Log Message: ----------- add SPP fix for pattern if (b && b.booleanValue()) Modified Paths: -------------- trunk/fb-contrib/etc/findbugs.xml trunk/fb-contrib/etc/messages.xml trunk/fb-contrib/samples/SPP_Sample.java trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java Modified: trunk/fb-contrib/etc/findbugs.xml =================================================================== --- trunk/fb-contrib/etc/findbugs.xml 2007-02-20 00:31:17 UTC (rev 863) +++ trunk/fb-contrib/etc/findbugs.xml 2007-02-20 01:35:46 UTC (rev 864) @@ -256,7 +256,7 @@ <Detector class="com.mebigfatguy.fbcontrib.detect.SillynessPotPourri" speed="fast" - reports="SPP_NEGATIVE_BITSET_ITEM,SPP_INTERN_ON_CONSTANT,SPP_NO_CHAR_SB_CTOR,SPP_USE_MATH_CONSTANT,SPP_STUTTERED_ASSIGNMENT,SPP_USE_ISNAN,SPP_USE_BIGDECIMAL_STRING_CTOR,SPP_STRINGBUFFER_WITH_EMPTY_STRING,SPP_EQUALS_ON_ENUM" /> + reports="SPP_NEGATIVE_BITSET_ITEM,SPP_INTERN_ON_CONSTANT,SPP_NO_CHAR_SB_CTOR,SPP_USE_MATH_CONSTANT,SPP_STUTTERED_ASSIGNMENT,SPP_USE_ISNAN,SPP_USE_BIGDECIMAL_STRING_CTOR,SPP_STRINGBUFFER_WITH_EMPTY_STRING,SPP_EQUALS_ON_ENUM,SPP_INVALID_BOOLEAN_NULL_CHECK" /> <Detector class="com.mebigfatguy.fbcontrib.detect.BloatedAssignmentScope" speed="fast" @@ -360,6 +360,7 @@ <BugPattern abbrev="SPP" type="SPP_USE_BIGDECIMAL_STRING_CTOR" category="CORRECTNESS" /> <BugPattern abbrev="SPP" type="SPP_STRINGBUFFER_WITH_EMPTY_STRING" category="PERFORMANCE" /> <BugPattern abbrev="SPP" type="SPP_EQUALS_ON_ENUM" category="CORRECTNESS" /> + <BugPattern abbrev="SPP" type="SPP_INVALID_BOOLEAN_NULL_CHECK" category="CORRECTNESS" /> <BugPattern abbrev="BAS" type="BAS_BLOATED_ASSIGNMENT_SCOPE" category="PERFORMANCE" /> <BugPattern abbrev="SCII" type="SCII_SPOILED_CHILD_INTERFACE_IMPLEMENTATOR" category="STYLE" /> <BugPattern abbrev="DWI" type="DWI_DELETING_WHILE_ITERATING" category="CORRECTNESS" /> Modified: trunk/fb-contrib/etc/messages.xml =================================================================== --- trunk/fb-contrib/etc/messages.xml 2007-02-20 00:31:17 UTC (rev 863) +++ trunk/fb-contrib/etc/messages.xml 2007-02-20 01:35:46 UTC (rev 864) @@ -1741,6 +1741,23 @@ </Details> </BugPattern> + <BugPattern type="SPP_INVALID_BOOLEAN_NULL_CHECK"> + <ShortDescription>Method uses invalid C++ style null check on Boolean</ShortDescription> + <LongDescription>Method {1} uses invalid C++ style null check on Boolean</LongDescription> + <Details> + <![CDATA[ + <p>This method attempts to check for null by just refering to the variable name + as would be done in C++. This ordinarily would be considered a compile error, except the + variable in question is a Boolean, which does an auto unbox to boolean.</p> + <pre> + if (b && b.booleanValue()) + should be + if ((b != null) && b.booleanValue()) + </pre> + ]]> + </Details> + </BugPattern> + <BugPattern type="BAS_BLOATED_ASSIGNMENT_SCOPE"> <ShortDescription>Method assigns a variable in a larger scope then is needed</ShortDescription> <LongDescription>Method {1} assigns a variable in a larger scope then is needed</LongDescription> Modified: trunk/fb-contrib/samples/SPP_Sample.java =================================================================== --- trunk/fb-contrib/samples/SPP_Sample.java 2007-02-20 00:31:17 UTC (rev 863) +++ trunk/fb-contrib/samples/SPP_Sample.java 2007-02-20 01:35:46 UTC (rev 864) @@ -62,4 +62,12 @@ if (f.equals(Flap.Jack)) System.out.println("Flap Jacks"); } + + public void testCPPBoolean(Boolean a, Boolean b, Boolean c, Boolean d, Boolean e) + { + if (b && b.booleanValue()) + System.out.println("Booya"); + if (e && e.booleanValue()) + System.out.println("Booya"); + } } Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java 2007-02-20 00:31:17 UTC (rev 863) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java 2007-02-20 01:35:46 UTC (rev 864) @@ -18,6 +18,8 @@ */ package com.mebigfatguy.fbcontrib.detect; +import java.util.Arrays; + import org.apache.bcel.Repository; import org.apache.bcel.classfile.Code; import org.apache.bcel.classfile.ConstantDouble; @@ -38,6 +40,7 @@ { private BugReporter bugReporter; private OpcodeStack stack; + private int lastPCs[]; private int lastOpcode; private int lastReg; @@ -53,9 +56,11 @@ public void visitClassContext(ClassContext classContext) { try { stack = new OpcodeStack(); + lastPCs = new int[4]; super.visitClassContext(classContext); } finally { stack = null; + lastPCs = null; } } @@ -69,6 +74,7 @@ stack.resetForMethodEntry(this); lastOpcode = -1; lastReg = -1; + Arrays.fill(lastPCs, -1); super.visitCode(obj); } @@ -181,6 +187,33 @@ } catch (ClassNotFoundException cnfe) { bugReporter.reportMissingClass(cnfe); } + } else if ("java/lang/Boolean".equals(className) + && "booleanValue".equals(methodName)) { + if (lastPCs[0] != -1) { + int range1Size = lastPCs[2] - lastPCs[0]; + if (range1Size == (getNextPC() - lastPCs[3])) { + byte[] bytes = getCode().getCode(); + int ifeq = 0x000000FF & bytes[lastPCs[2]]; + if (ifeq == IFEQ) { + int start1 = lastPCs[0]; + int start2 = lastPCs[3]; + boolean found = true; + for (int i = 0; i < range1Size; i++) { + if (bytes[start1+i] != bytes[start2+i]) { + found = false; + break; + } + } + + if (found) { + bugReporter.reportBug(new BugInstance(this, "SPP_INVALID_BOOLEAN_NULL_CHECK", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } + } + } + } } } else if (seen == INVOKESPECIAL) { String className = getClassConstantOperand(); @@ -239,6 +272,8 @@ stack.sawOpcode(this, seen); lastOpcode = seen; lastReg = reg; + System.arraycopy(lastPCs, 1, lastPCs, 0, 3); + lastPCs[3] = getPC(); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |