[Fb-contrib-commit] SF.net SVN: fb-contrib: [885] trunk/fb-contrib
Brought to you by:
dbrosius
From: <dbr...@us...> - 2007-06-08 02:53:17
|
Revision: 885 http://svn.sourceforge.net/fb-contrib/?rev=885&view=rev Author: dbrosius Date: 2007-06-07 19:53:18 -0700 (Thu, 07 Jun 2007) Log Message: ----------- add check for s.toCharArray()[0] 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 2007-05-27 21:13:53 UTC (rev 884) +++ trunk/fb-contrib/etc/findbugs.xml 2007-06-08 02:53:18 UTC (rev 885) @@ -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,SPP_USE_MATH_CONSTANT,SPP_STUTTERED_ASSIGNMENT,SPP_USE_ISNAN,SPP_USE_BIGDECIMAL_STRING_CTOR,SPP_STRINGBUFFER_WITH_EMPTY_STRING,SPP_EQUALS_ON_ENUM,SPP_INVALID_BOOLEAN_NULL_CHECK" /> + reports="SPP_NEGATIVE_BITSET_ITEM,SPP_INTERN_ON_CONSTANT,SPP_NO_CHAR_SB_CTOR,SPP_USE_MATH_CONSTANT,SPP_STUTTERED_ASSIGNMENT,SPP_USE_ISNAN,SPP_USE_BIGDECIMAL_STRING_CTOR,SPP_STRINGBUFFER_WITH_EMPTY_STRING,SPP_EQUALS_ON_ENUM,SPP_INVALID_BOOLEAN_NULL_CHECK,SPP_USE_CHARAT" /> <Detector class="com.mebigfatguy.fbcontrib.detect.BloatedAssignmentScope" speed="fast" @@ -363,6 +363,7 @@ <BugPattern abbrev="SPP" type="SPP_STRINGBUFFER_WITH_EMPTY_STRING" category="PERFORMANCE" /> <BugPattern abbrev="SPP" type="SPP_EQUALS_ON_ENUM" category="CORRECTNESS" /> <BugPattern abbrev="SPP" type="SPP_INVALID_BOOLEAN_NULL_CHECK" category="CORRECTNESS" experimental="true" /> + <BugPattern abbrev="SPP" type="SPP_USE_CHARAT" category="PERFORMANCE" experimental="true" /> <BugPattern abbrev="BAS" type="BAS_BLOATED_ASSIGNMENT_SCOPE" category="PERFORMANCE" /> <BugPattern abbrev="SCII" type="SCII_SPOILED_CHILD_INTERFACE_IMPLEMENTATOR" category="STYLE" /> <BugPattern abbrev="DWI" type="DWI_DELETING_WHILE_ITERATING" category="CORRECTNESS" /> Modified: trunk/fb-contrib/etc/messages.xml =================================================================== --- trunk/fb-contrib/etc/messages.xml 2007-05-27 21:13:53 UTC (rev 884) +++ trunk/fb-contrib/etc/messages.xml 2007-06-08 02:53:18 UTC (rev 885) @@ -1796,6 +1796,18 @@ </Details> </BugPattern> + <BugPattern type="SPP_USE_CHARAT"> + <ShortDescription>Method fetches character array just to do the equivalent of the charAt method</ShortDescription> + <LongDescription>Method {1} fetches character array just to do the equivalent of the charAt method</LongDescription> + <Details> + <![CDATA[ + <p>This method calls the toCharArray method on a String the fetch an array of characters, only + to retrieve one of those characters by index. It is more performant to just use the charAt method. + </p> + ]]> + </Details> + </BugPattern> + <BugPattern type="BAS_BLOATED_ASSIGNMENT_SCOPE"> <ShortDescription>Method assigns a variable in a larger scope then is needed</ShortDescription> <LongDescription>Method {1} assigns a variable in a larger scope then is needed</LongDescription> Modified: trunk/fb-contrib/samples/SPP_Sample.java =================================================================== --- trunk/fb-contrib/samples/SPP_Sample.java 2007-05-27 21:13:53 UTC (rev 884) +++ trunk/fb-contrib/samples/SPP_Sample.java 2007-06-08 02:53:18 UTC (rev 885) @@ -71,4 +71,11 @@ if (e && e.booleanValue()) System.out.println("Booya"); } + + public char usechatAt(String s) + { + if (s.length() > 0) + return s.toCharArray()[0]; + return ' '; + } } Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java 2007-05-27 21:13:53 UTC (rev 884) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/SillynessPotPourri.java 2007-06-08 02:53:18 UTC (rev 885) @@ -86,6 +86,7 @@ @Override public void sawOpcode(int seen) { int reg = -1; + String userValue = null; try { stack.mergeJumps(this); @@ -142,6 +143,25 @@ .addSourceLine(this)); } } + } else if ((seen >= ICONST_0) && (seen <= ICONST_3)) { + if (stack.getStackDepth() > 0) { + OpcodeStack.Item item = stack.getStackItem(0); + String tca = (String)item.getUserValue(); + if ("toCharArray".equals(tca)) { + userValue = "iconst"; + } + } + } else if (seen == CALOAD) { + if (stack.getStackDepth() > 0) { + OpcodeStack.Item item = stack.getStackItem(0); + String ic = (String)item.getUserValue(); + if ("iconst".equals(ic)) { + bugReporter.reportBug(new BugInstance(this, "SPP_USE_CHARAT", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + } + } } else if (seen == INVOKEVIRTUAL) { String className = getClassConstantOperand(); String methodName = getNameConstantOperand(); @@ -178,6 +198,8 @@ } } } + } else if ("toCharArray".equals(methodName)) { + userValue = "toCharArray"; } } else if ("equals(Ljava/lang/Object;)Z".equals(methodName + getSigConstantOperand())) { try { @@ -274,6 +296,11 @@ } finally { stack.sawOpcode(this, seen); + if (userValue != null) + { + OpcodeStack.Item item = stack.getStackItem(0); + item.setUserValue(userValue); + } lastOpcode = seen; lastReg = reg; System.arraycopy(lastPCs, 1, lastPCs, 0, 3); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |