[Fb-contrib-commit] SF.net SVN: fb-contrib: [871] trunk/fb-contrib
Brought to you by:
dbrosius
From: <dbr...@us...> - 2007-02-26 09:02:31
|
Revision: 871 http://svn.sourceforge.net/fb-contrib/?rev=871&view=rev Author: dbrosius Date: 2007-02-26 01:02:31 -0800 (Mon, 26 Feb 2007) Log Message: ----------- add check for new Float(f).intValue() and others. Also filter out valueOf(String, radix) calls Modified Paths: -------------- trunk/fb-contrib/etc/findbugs.xml trunk/fb-contrib/etc/messages.xml trunk/fb-contrib/samples/NAB_Sample.java trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java Modified: trunk/fb-contrib/etc/findbugs.xml =================================================================== --- trunk/fb-contrib/etc/findbugs.xml 2007-02-26 08:11:18 UTC (rev 870) +++ trunk/fb-contrib/etc/findbugs.xml 2007-02-26 09:02:31 UTC (rev 871) @@ -115,7 +115,7 @@ <Detector class="com.mebigfatguy.fbcontrib.detect.NeedlessAutoboxing" speed="fast" - reports="NAB_NEEDLESS_AUTOBOXING_CTOR,NAB_NEEDLESS_BOXING_STRING_CTOR,NAB_NEEDLESS_AUTOBOXING_VALUEOF,NAB_NEEDLESS_BOXING_PARSE,NAB_NEEDLESS_BOXING_VALUEOF,NAB_NEEDLESS_BOX_TO_UNBOX" /> + reports="NAB_NEEDLESS_AUTOBOXING_CTOR,NAB_NEEDLESS_BOXING_STRING_CTOR,NAB_NEEDLESS_AUTOBOXING_VALUEOF,NAB_NEEDLESS_BOXING_PARSE,NAB_NEEDLESS_BOXING_VALUEOF,NAB_NEEDLESS_BOX_TO_UNBOX,NAB_NEEDLESS_BOX_TO_CAST" /> <Detector class="com.mebigfatguy.fbcontrib.detect.UnnecessaryStoreBeforeReturn" speed="fast" @@ -312,6 +312,7 @@ <BugPattern abbrev="NAB" type="NAB_NEEDLESS_BOXING_PARSE" category="PERFORMANCE" /> <BugPattern abbrev="NAB" type="NAB_NEEDLESS_BOXING_VALUEOF" category="PERFORMANCE" /> <BugPattern abbrev="NAB" type="NAB_NEEDLESS_BOX_TO_UNBOX" category="PERFORMANCE" experimental="true" /> + <BugPattern abbrev="NAB" type="NAB_NEEDLESS_BOX_TO_CAST" category="PERFORMANCE" experimental="true" /> <BugPattern abbrev="USBR" type="USBR_UNNECESSARY_STORE_BEFORE_RETURN" category="STYLE" /> <BugPattern abbrev="COM" type="COM_COPIED_OVERRIDDEN_METHOD" category="STYLE" /> <BugPattern abbrev="ABC" type="ABC_ARRAY_BASED_COLLECTIONS" category="CORRECTNESS" /> Modified: trunk/fb-contrib/etc/messages.xml =================================================================== --- trunk/fb-contrib/etc/messages.xml 2007-02-26 08:11:18 UTC (rev 870) +++ trunk/fb-contrib/etc/messages.xml 2007-02-26 09:02:31 UTC (rev 871) @@ -1113,6 +1113,25 @@ ]]> </Details> </BugPattern> + + <BugPattern type="NAB_NEEDLESS_BOX_TO_CAST"> + <ShortDescription>method creates Boxed primitive from primitive only to cast to another primitive type</ShortDescription> + <LongDescription>method {1} creates Boxed primitive from primitive only to cast to another primitive type</LongDescription> + <Details> + <![CDATA[ + <p>This method constructs a Boxed Primitive from a primitive only to call the primitiveValue() method to + cast the value to another primitive typee. It is simpler to just use casting</p> + <pre> + primitive i = new BoxedPrimitive(1.0).primitiveValue(); + or + primitive i = BoxedPrimitive.valueOf(1.0).primitiveValue(); + + should just use + primitive i = (primitive)1.0; + </pre> + ]]> + </Details> + </BugPattern> <BugPattern type="USBR_UNNECESSARY_STORE_BEFORE_RETURN"> <ShortDescription>method stores return result in local before immediately returning it</ShortDescription> Modified: trunk/fb-contrib/samples/NAB_Sample.java =================================================================== --- trunk/fb-contrib/samples/NAB_Sample.java 2007-02-26 08:11:18 UTC (rev 870) +++ trunk/fb-contrib/samples/NAB_Sample.java 2007-02-26 09:02:31 UTC (rev 871) @@ -88,4 +88,38 @@ double d = new Double(6.0).doubleValue(); d = Double.valueOf(6.0).doubleValue(); } + + public void testBoxedCast() + { + short s = new Short((short)2).byteValue(); + s = Short.valueOf((short) 2).byteValue(); + int i = new Integer(3).byteValue(); + i = Integer.valueOf(3).byteValue(); + i = new Integer(3).shortValue(); + i = Integer.valueOf(3).shortValue(); + long l = new Long(4).byteValue(); + l = Long.valueOf(4).byteValue(); + l = new Long(4).shortValue(); + l = Long.valueOf(4).shortValue(); + l = new Long(4).intValue(); + l = Long.valueOf(4).intValue(); + float f = new Float(5.0f).byteValue(); + f = Float.valueOf(5.0f).byteValue(); + f = new Float(5.0f).shortValue(); + f = Float.valueOf(5.0f).shortValue(); + f = new Float(5.0f).intValue(); + f = Float.valueOf(5.0f).intValue(); + f = new Float(5.0f).longValue(); + f = Float.valueOf(5.0f).longValue(); + double d = new Double(6.0).byteValue(); + d = Double.valueOf(6.0).byteValue(); + d = new Double(6.0).shortValue(); + d = Double.valueOf(6.0).shortValue(); + d = new Double(6.0).intValue(); + d = Double.valueOf(6.0).intValue(); + d = new Double(6.0).longValue(); + d = Double.valueOf(6.0).longValue(); + d = new Double(6.0).floatValue(); + d = Double.valueOf(6.0).floatValue(); + } } Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java 2007-02-26 08:11:18 UTC (rev 870) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java 2007-02-26 09:02:31 UTC (rev 871) @@ -104,7 +104,7 @@ if (sig.startsWith("(Ljava/lang/String;)")) { if (!"java/lang/Boolean".equals(boxClass) || (getClassContext().getJavaClass().getMajor() >= Constants.MAJOR_1_5)) state = SEEN_VALUEOFSTRING; - } else { + } else if (!sig.startsWith("(Ljava/lang/String;")) { state = SEEN_VALUEOFPRIMITIVE; } } else { @@ -170,6 +170,11 @@ .addClass(this) .addMethod(this) .addSourceLine(this)); + } else if (getSigConstantOperand().startsWith("()") && getNameConstantOperand().endsWith("Value")) { + bugReporter.reportBug(new BugInstance(this, "NAB_NEEDLESS_BOX_TO_CAST", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); } } state = SEEN_NOTHING; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |