[Fb-contrib-commit] SF.net SVN: fb-contrib: [868] trunk/fb-contrib
Brought to you by:
dbrosius
From: <dbr...@us...> - 2007-02-26 07:53:27
|
Revision: 868 http://svn.sourceforge.net/fb-contrib/?rev=868&view=rev Author: dbrosius Date: 2007-02-25 23:53:26 -0800 (Sun, 25 Feb 2007) Log Message: ----------- add NAB check for new Integer(1).intValue() 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-25 12:47:44 UTC (rev 867) +++ trunk/fb-contrib/etc/findbugs.xml 2007-02-26 07:53:26 UTC (rev 868) @@ -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" /> + 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" /> <Detector class="com.mebigfatguy.fbcontrib.detect.UnnecessaryStoreBeforeReturn" speed="fast" @@ -311,6 +311,7 @@ <BugPattern abbrev="NAB" type="NAB_NEEDLESS_AUTOBOXING_VALUEOF" category="PERFORMANCE" /> <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="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-25 12:47:44 UTC (rev 867) +++ trunk/fb-contrib/etc/messages.xml 2007-02-26 07:53:26 UTC (rev 868) @@ -1095,6 +1095,22 @@ </Details> </BugPattern> + <BugPattern type="NAB_NEEDLESS_BOX_TO_UNBOX"> + <ShortDescription>method creates Boxed primitive from primitive only to get primitive value</ShortDescription> + <LongDescription>method {1} creates Boxed primitive from primitive only to get primitive value</LongDescription> + <Details> + <![CDATA[ + <p>This method constructs a Boxed Primitive from a primitive only to call the primitiveValue() method to + convert it back to a primitive. Just use the primitive value instead.</p> + <pre> + primitive i = new BoxedPrimitive(1).primitiveValue(); + should just use + primitive i = 1; + </pre> + ]]> + </Details> + </BugPattern> + <BugPattern type="USBR_UNNECESSARY_STORE_BEFORE_RETURN"> <ShortDescription>method stores return result in local before immediately returning it</ShortDescription> <LongDescription>method {1} stores return result in local before immediately returning it</LongDescription> Modified: trunk/fb-contrib/samples/NAB_Sample.java =================================================================== --- trunk/fb-contrib/samples/NAB_Sample.java 2007-02-25 12:47:44 UTC (rev 867) +++ trunk/fb-contrib/samples/NAB_Sample.java 2007-02-26 07:53:26 UTC (rev 868) @@ -55,8 +55,8 @@ public void testExtraneousParse() { - Boolean bo = Boolean.valueOf(Boolean.parseBoolean("test")); - bo = new Boolean(Boolean.parseBoolean("test")); + Boolean bo = Boolean.valueOf(Boolean.parseBoolean("true")); + bo = new Boolean(Boolean.parseBoolean("true")); Byte b = Byte.valueOf(Byte.parseByte("1")); b = new Byte(Byte.parseByte("1")); Short s = Short.valueOf(Short.parseShort("1")); @@ -70,4 +70,22 @@ Double d = Double.valueOf(Double.parseDouble("1")); d = new Double(Double.parseDouble("1")); } + + public void testBoxToUnbox() + { + boolean bo = new Boolean(true).booleanValue(); + bo = Boolean.valueOf(true).booleanValue(); + byte b = new Byte((byte)1).byteValue(); + b = Byte.valueOf((byte)1).byteValue(); + short s = new Short((short)2).shortValue(); + s = Short.valueOf((short) 2).shortValue(); + int i = new Integer(3).intValue(); + i = Integer.valueOf(3).intValue(); + long l = new Long(4).longValue(); + l = Long.valueOf(4).longValue(); + float f = new Float(5.0f).floatValue(); + f = Float.valueOf(5.0f).floatValue(); + double d = new Double(6.0).doubleValue(); + d = Double.valueOf(6.0).doubleValue(); + } } Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java 2007-02-25 12:47:44 UTC (rev 867) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java 2007-02-26 07:53:26 UTC (rev 868) @@ -38,6 +38,7 @@ private static final int SEEN_VALUE = 1; private static final int SEEN_VALUEOF = 2; private static final int SEEN_PARSE = 3; + private static final int SEEN_CTOR = 4; private static final Map<String, String[]> boxClasses = new HashMap<String, String[]>(); static { @@ -95,12 +96,12 @@ } } else if (seen == INVOKESTATIC) { boxClass = getClassConstantOperand(); - if (boxClasses.containsKey(boxClass)) { + String[] boxSigs = boxClasses.get(boxClass); + if (boxSigs != null) { if ("valueOf".equals(getNameConstantOperand()) && getSigConstantOperand().startsWith("(Ljava/lang/String;)")) { if (!"java/lang/Boolean".equals(boxClass) || (getClassContext().getJavaClass().getMajor() >= Constants.MAJOR_1_5)) state = SEEN_VALUEOF; - } - else { + } else { String parseSig = parseClasses.get(boxClass); if (parseSig != null) { String methodInfo = getNameConstantOperand() + getSigConstantOperand(); @@ -110,6 +111,14 @@ } } } + } else if (seen == INVOKESPECIAL) { + boxClass = getClassConstantOperand(); + String[] boxSigs = boxClasses.get(boxClass); + if (boxSigs != null) { + if ("<init>".equals(getNameConstantOperand()) && boxSigs[1].equals(getSigConstantOperand())) { + state = SEEN_CTOR; + } + } } break; @@ -146,6 +155,19 @@ state = SEEN_NOTHING; break; + case SEEN_CTOR: + if (seen == INVOKEVIRTUAL) { + String[] boxSigs = boxClasses.get(boxClass); + if (boxSigs[0].equals(getNameConstantOperand() + getSigConstantOperand())) { + bugReporter.reportBug(new BugInstance(this, "NAB_NEEDLESS_BOX_TO_UNBOX", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } + } + state = SEEN_NOTHING; + break; + case SEEN_VALUEOF: if (seen == INVOKEVIRTUAL) { String[] boxSigs = boxClasses.get(boxClass); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |