[Fb-contrib-commit] SF.net SVN: fb-contrib: [833] trunk/fb-contrib
Brought to you by:
dbrosius
From: <dbr...@us...> - 2007-02-04 03:19:08
|
Revision: 833 http://svn.sourceforge.net/fb-contrib/?rev=833&view=rev Author: dbrosius Date: 2007-02-03 19:19:07 -0800 (Sat, 03 Feb 2007) Log Message: ----------- add check for pattern Boxed.valueOf(Boxed.parseBoxed("1")) Modified Paths: -------------- trunk/fb-contrib/etc/findbugs.xml trunk/fb-contrib/etc/messages.xml 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-04 02:45:07 UTC (rev 832) +++ trunk/fb-contrib/etc/findbugs.xml 2007-02-04 03:19:07 UTC (rev 833) @@ -115,7 +115,7 @@ <Detector class="com.mebigfatguy.fbcontrib.detect.NeedlessAutoboxing" speed="fast" - reports="NAB_NEEDLESS_AUTOBOXING_CTOR,NAB_NEEDLESS_AUTOBOXING_VALUEOF,NAB_NEEDLESS_AUTOBOXING_PARSE" /> + reports="NAB_NEEDLESS_AUTOBOXING_CTOR,NAB_NEEDLESS_AUTOBOXING_VALUEOF,NAB_NEEDLESS_BOXING_PARSE,NAB_NEEDLESS_BOXING_VALUEOF" /> <Detector class="com.mebigfatguy.fbcontrib.detect.UnnecessaryStoreBeforeReturn" speed="fast" @@ -306,7 +306,8 @@ <BugPattern abbrev="STS" type="STS_SPURIOUS_THREAD_STATES" category="MT_CORRECTNESS" /> <BugPattern abbrev="NAB" type="NAB_NEEDLESS_AUTOBOXING_CTOR" category="PERFORMANCE" /> <BugPattern abbrev="NAB" type="NAB_NEEDLESS_AUTOBOXING_VALUEOF" category="PERFORMANCE" /> - <BugPattern abbrev="NAB" type="NAB_NEEDLESS_AUTOBOXING_PARSE" category="PERFORMANCE" /> + <BugPattern abbrev="NAB" type="NAB_NEEDLESS_BOXING_PARSE" category="PERFORMANCE" /> + <BugPattern abbrev="NAB" type="NAB_NEEDLESS_BOXING_VALUEOF" category="PERFORMANCE" /> <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-04 02:45:07 UTC (rev 832) +++ trunk/fb-contrib/etc/messages.xml 2007-02-04 03:19:07 UTC (rev 833) @@ -1057,9 +1057,9 @@ </Details> </BugPattern> - <BugPattern type="NAB_NEEDLESS_AUTOBOXING_PARSE"> - <ShortDescription>method converts String to primitive using excessive autoboxing</ShortDescription> - <LongDescription>method {1} converts String to primitive using excessive autoboxing</LongDescription> + <BugPattern type="NAB_NEEDLESS_BOXING_PARSE"> + <ShortDescription>method converts String to primitive using excessive boxing</ShortDescription> + <LongDescription>method {1} converts String to primitive using excessive boxing</LongDescription> <Details> <![CDATA[ <p>This method passes a String to a wrapped primitive object's valueOf method, which in turn calls @@ -1070,6 +1070,19 @@ </Details> </BugPattern> + <BugPattern type="NAB_NEEDLESS_BOXING_VALUEOF"> + <ShortDescription>method converts String to boxed primitive using excessive boxing</ShortDescription> + <LongDescription>method {1} converts String to boxed primitive using excessive boxing</LongDescription> + <Details> + <![CDATA[ + <p>This method passes a String to a wrapped primitive object's parse method, which in turn calls + the valueOf() method to convert to a boxed primitive. When it is desired to convert from a String + to a boxed primitive object, it is simpler to use the BoxedPrimitive.valueOf(myString) + method. </p> + ]]> + </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/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java 2007-02-04 02:45:07 UTC (rev 832) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/NeedlessAutoboxing.java 2007-02-04 03:19:07 UTC (rev 833) @@ -36,19 +36,31 @@ private static final int SEEN_NOTHING = 0; private static final int SEEN_VALUE = 1; private static final int SEEN_VALUEOF = 2; + private static final int SEEN_PARSE = 3; private static final Map<String, String[]> boxClasses = new HashMap<String, String[]>(); static { boxClasses.put("java/lang/Boolean", new String[] { "booleanValue()Z", "(Z)V", "(Z)Ljava/lang/Boolean;" }); boxClasses.put("java/lang/Character", new String[] { "charValue()C", "(C)V", "(C)Ljava/lang/Character;" }); boxClasses.put("java/lang/Byte", new String[] { "byteValue()B", "(B)V", "(B)Ljava/lang/Byte;" }); - boxClasses.put("java.lang/Short", new String[] { "shortValue()S", "(S)V", "(S)Ljava/lang/Short;" }); + boxClasses.put("java/lang/Short", new String[] { "shortValue()S", "(S)V", "(S)Ljava/lang/Short;" }); boxClasses.put("java/lang/Integer", new String[] { "intValue()I", "(I)V", "(I)Ljava/lang/Integer;" }); boxClasses.put("java/lang/Long", new String[] { "longValue()J", "(J)V", "(J)Ljava/lang/Long;" }); boxClasses.put("java/lang/Float", new String[] { "floatValue()F", "(F)V", "(F)Ljava/lang/Float;" }); boxClasses.put("java/lang/Double", new String[] { "doubleValue()D", "(D)V", "(D)Ljava/lang/Double;" }); } + private static final Map<String, String> parseClasses = new HashMap<String, String>(); + static { + parseClasses.put("java/lang/Boolean", "parseBoolean(Ljava/lang/String;)Z"); + parseClasses.put("java/lang/Byte", "parseByte(Ljava/lang/String;)B"); + parseClasses.put("java/lang/Short", "parseShort(Ljava/lang/String;)S"); + parseClasses.put("java/lang/Integer", "parseInt(Ljava/lang/String;)I"); + parseClasses.put("java/lang/Long", "parseLong(Ljava/lang/String;)J"); + parseClasses.put("java/lang/Float", "parseFloat(Ljava/lang/String;)F"); + parseClasses.put("java/lang/Double", "parseDouble(Ljava/lang/String;)D"); + } + private BugReporter bugReporter; private int state; private String boxClass; @@ -75,12 +87,10 @@ boxClass = getClassConstantOperand(); String[] boxSigs = boxClasses.get(boxClass); if (boxSigs != null) { - String methodName = getNameConstantOperand(); - String methodSig = getSigConstantOperand(); - if (boxSigs[0].equals(methodName + methodSig)) { + String methodInfo = getNameConstantOperand() + getSigConstantOperand(); + if (boxSigs[0].equals(methodInfo)) { state = SEEN_VALUE; } - } } else if (seen == INVOKESTATIC) { boxClass = getClassConstantOperand(); @@ -88,6 +98,15 @@ if ("valueOf".equals(getNameConstantOperand()) && getSigConstantOperand().startsWith("(Ljava/lang/String;)")) { state = SEEN_VALUEOF; } + else { + String parseSig = parseClasses.get(boxClass); + if (parseSig != null) { + String methodInfo = getNameConstantOperand() + getSigConstantOperand(); + if (parseSig.equals(methodInfo)) { + state = SEEN_PARSE; + } + } + } } } break; @@ -129,7 +148,7 @@ if (seen == INVOKEVIRTUAL) { String[] boxSigs = boxClasses.get(boxClass); if (boxSigs[0].equals(getNameConstantOperand() + getSigConstantOperand())) { - bugReporter.reportBug(new BugInstance(this, "NAB_NEEDLESS_AUTOBOXING_PARSE", NORMAL_PRIORITY) + bugReporter.reportBug(new BugInstance(this, "NAB_NEEDLESS_BOXING_PARSE", NORMAL_PRIORITY) .addClass(this) .addMethod(this) .addSourceLine(this)); @@ -137,6 +156,21 @@ } state = SEEN_NOTHING; break; + + case SEEN_PARSE: + if (seen == INVOKESTATIC) { + if (boxClass.equals(getClassConstantOperand())) { + if ("valueOf".equals(getNameConstantOperand())) { + bugReporter.reportBug(new BugInstance(this, "NAB_NEEDLESS_BOXING_VALUEOF", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } + } + } + state = SEEN_NOTHING; + break; + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |