[Fb-contrib-commit] SF.net SVN: fb-contrib: [691] trunk/fb-contrib
Brought to you by:
dbrosius
From: <dbr...@us...> - 2006-11-19 05:18:32
|
Revision: 691 http://svn.sourceforge.net/fb-contrib/?rev=691&view=rev Author: dbrosius Date: 2006-11-18 21:18:32 -0800 (Sat, 18 Nov 2006) Log Message: ----------- add non Math.constants to SPP detector 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 2006-11-19 04:36:12 UTC (rev 690) +++ trunk/fb-contrib/etc/findbugs.xml 2006-11-19 05:18:32 UTC (rev 691) @@ -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"/> + reports="SPP_NEGATIVE_BITSET_ITEM,SPP_INTERN_ON_CONSTANT,SPP_NO_CHAR_SB_CTOR,SPP_USE_MATH_CONSTANT"/> <!-- BugPattern --> @@ -326,5 +326,6 @@ <BugPattern abbrev="SPP" type="SPP_NEGATIVE_BITSET_ITEM" category="CORRECTNESS" experimental="true" /> <BugPattern abbrev="SPP" type="SPP_INTERN_ON_CONSTANT" category="CORRECTNESS" experimental="true" /> <BugPattern abbrev="SPP" type="SPP_NO_CHAR_SB_CTOR" category="CORRECTNESS" experimental="true" /> + <BugPattern abbrev="SPP" type="SPP_USE_MATH_CONSTANT" category="CORRECTNESS" experimental="true" /> </FindbugsPlugin> \ No newline at end of file Modified: trunk/fb-contrib/etc/messages.xml =================================================================== --- trunk/fb-contrib/etc/messages.xml 2006-11-19 04:36:12 UTC (rev 690) +++ trunk/fb-contrib/etc/messages.xml 2006-11-19 05:18:32 UTC (rev 691) @@ -1551,6 +1551,17 @@ </Details> </BugPattern> + <BugPattern type="SPP_USE_MATH_CONSTANT"> + <ShortDescription>Method uses non standard math constant</ShortDescription> + <LongDescription>Method {1} uses non standard math constant</LongDescription> + <Details> + <![CDATA[ + <p>This method defines its own version of <b>PI</b> or <b>e</b> and the value is not as precise as the + one defined in the constants Math.PI or Math.E. Use these constants instead.</p> + ]]> + </Details> + </BugPattern> + <!-- BugCode --> <BugCode abbrev="ISB">Inefficient String Buffering</BugCode> Modified: trunk/fb-contrib/samples/SPP_Sample.java =================================================================== --- trunk/fb-contrib/samples/SPP_Sample.java 2006-11-19 04:36:12 UTC (rev 690) +++ trunk/fb-contrib/samples/SPP_Sample.java 2006-11-19 05:18:32 UTC (rev 691) @@ -2,7 +2,9 @@ public class SPP_Sample { - + private static final double pi = 3.14; + private static final double e = 2.72; + public void testSPPBitSet(BitSet b) { b.set(-1); @@ -19,4 +21,9 @@ sb.append("ictory"); return sb.toString(); } + + public double area(double radius) + { + return pi * radius * radius; + } } Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java 2006-11-19 04:36:12 UTC (rev 690) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java 2006-11-19 05:18:32 UTC (rev 691) @@ -19,6 +19,7 @@ package com.mebigfatguy.fbcontrib.detect; import org.apache.bcel.classfile.Code; +import org.apache.bcel.classfile.ConstantDouble; import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; @@ -72,7 +73,22 @@ try { stack.mergeJumps(this); - if (seen == INVOKEVIRTUAL) { + if (seen == LDC2_W) { + Object con = getConstantRefOperand(); + if (con instanceof ConstantDouble) { + double d = ((ConstantDouble) con).getBytes(); + double piDelta = Math.abs(d - Math.PI); + double eDelta = Math.abs(d - Math.E); + + if (((piDelta > 0.0) && (piDelta < 0.002)) + || ((eDelta > 0.0) && (eDelta < 0.002))) { + bugReporter.reportBug(new BugInstance(this, "SPP_USE_MATH_CONSTANT", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } + } + } else if (seen == INVOKEVIRTUAL) { String className = getClassConstantOperand(); String methodName = getNameConstantOperand(); if ("java/util/BitSet".equals(className)) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |