[Fb-contrib-commit] SF.net SVN: fb-contrib: [562] trunk/fb-contrib/etc
Brought to you by:
dbrosius
From: <dbr...@us...> - 2006-06-09 02:04:41
|
Revision: 562 Author: dbrosius Date: 2006-06-08 19:04:31 -0700 (Thu, 08 Jun 2006) ViewCVS: http://svn.sourceforge.net/fb-contrib/?rev=562&view=rev Log Message: ----------- add ISB_EMPTY_STRING_APPENDING bug pattern to ISB Modified Paths: -------------- trunk/fb-contrib/etc/findbugs.xml trunk/fb-contrib/etc/messages.xml trunk/fb-contrib/samples/ISB_Sample.java trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/InefficientStringBuffering.java Modified: trunk/fb-contrib/etc/findbugs.xml =================================================================== --- trunk/fb-contrib/etc/findbugs.xml 2006-06-08 17:52:39 UTC (rev 561) +++ trunk/fb-contrib/etc/findbugs.xml 2006-06-09 02:04:31 UTC (rev 562) @@ -15,7 +15,7 @@ <Detector class="com.mebigfatguy.fbcontrib.detect.InefficientStringBuffering" speed="fast" - reports="ISB_INEFFICIENT_STRING_BUFFERING" /> + reports="ISB_INEFFICIENT_STRING_BUFFERING,ISB_EMPTY_STRING_APPENDING" /> <Detector class="com.mebigfatguy.fbcontrib.detect.SyncCollectionIterators" speed="slow" @@ -214,6 +214,7 @@ <!-- BugPattern --> <BugPattern abbrev="ISB" type="ISB_INEFFICIENT_STRING_BUFFERING" category="PERFORMANCE" /> + <BugPattern abbrev="ISB" type="ISB_EMPTY_STRING_APPENDING" category="PERFORMANCE" /> <BugPattern abbrev="SCI" type="SCI_SYNCHRONIZED_COLLECTION_ITERATORS" category="CORRECTNESS" /> <BugPattern abbrev="CC" type="CC_CYCLOMATIC_COMPLEXITY" category="STYLE" /> <BugPattern abbrev="OCP" type="OCP_OVERLY_CONCRETE_PARAMETER" category="STYLE" /> Modified: trunk/fb-contrib/etc/messages.xml =================================================================== --- trunk/fb-contrib/etc/messages.xml 2006-06-08 17:52:39 UTC (rev 561) +++ trunk/fb-contrib/etc/messages.xml 2006-06-09 02:04:31 UTC (rev 562) @@ -615,6 +615,19 @@ </Details> </BugPattern> + <BugPattern type="ISB_EMPTY_STRING_APPENDING"> + <ShortDescription>method concatenates an empty string to effect type conversion</ShortDescription> + <LongDescription>method {1} concatenates an empty string to effect type conversion</LongDescription> + <Details> + <![CDATA[ + <p> This method concatenates an empty string with a literal value, in order to convert + the literal value into a string. It is more efficient to use String.valueOf() to do the same + thing as you do not incur the cost of creating a StringBuffer/Builder and calling methods on it + to accomplish this.</p> + ]]> + </Details> + </BugPattern> + <BugPattern type="SCI_SYNCHRONIZED_COLLECTION_ITERATORS"> <ShortDescription>method creates iterators on synchronized collections</ShortDescription> <LongDescription>method {1} creates iterators on synchronized collections</LongDescription> Modified: trunk/fb-contrib/samples/ISB_Sample.java =================================================================== --- trunk/fb-contrib/samples/ISB_Sample.java 2006-06-08 17:52:39 UTC (rev 561) +++ trunk/fb-contrib/samples/ISB_Sample.java 2006-06-09 02:04:31 UTC (rev 562) @@ -38,6 +38,11 @@ return msg; } + public String testISB8(int a) + { + return "" + a; + } + public String toString() { String a = System.getProperty("foo"); Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/InefficientStringBuffering.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/InefficientStringBuffering.java 2006-06-08 17:52:39 UTC (rev 561) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/InefficientStringBuffering.java 2006-06-09 02:04:31 UTC (rev 562) @@ -19,6 +19,8 @@ package com.mebigfatguy.fbcontrib.detect; import org.apache.bcel.classfile.Code; +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantString; import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; @@ -75,6 +77,26 @@ .addSourceLine(this)); } } + } else if (seen == INVOKEVIRTUAL) { + String calledClass = getClassConstantOperand(); + if (("java/lang/StringBuffer".equals(calledClass) + || "java/lang/StringBuilder".equals(calledClass)) + && "append".equals(getNameConstantOperand()) + && getSigConstantOperand().startsWith("(Ljava/lang/String;)")) { + if (opStack.getStackDepth() > 0) { + OpcodeStack.Item itm = opStack.getStackItem(0); + Object cons = itm.getConstant(); + if ((cons instanceof String) && (itm.getRegisterNumber() < 0)) { + if (((String)cons).length() == 0) { + bugReporter.reportBug( + new BugInstance(this, "ISB_EMPTY_STRING_APPENDING", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } + } + } + } } else if (seen == GOTO) { int depth = opStack.getStackDepth(); for (int i = 0; i < depth; i++) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |