Fixed bug 1306180 - AvoidConcatenatingNonLiteralsInStringBuffer no longer reports false positives on certain StringBuffer usages.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3877 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2005-09-29 13:14:28 +00:00
parent 8229be508a
commit cfa917c96f
7 changed files with 31 additions and 5 deletions

View File

@ -8,6 +8,7 @@ Fixed bug 1293277 - Messages that used 'pluginname' had duplicated messages.
Fixed bug 1291353 - ASTMethodDeclaration isPublic/isAbstract methods are always return true. The syntactical modifier - i.e., whether or not 'public' was used in the source code in the method declaration - is available via 'isSyntacticallyPublic' and 'isSyntacticallyAbstract'
Fixed bug 1296544 - TooManyFields no longer checks the wrong property value.
Fixed bug 1304739 - StringInstantiation no longer crashes on certain String constructor usages.
Fixed bug 1306180 - AvoidConcatenatingNonLiteralsInStringBuffer no longer reports false positives on certain StringBuffer usages.
September 15, 2005 - 3.3:
New rules: PositionLiteralsFirstInComparisons (in the design ruleset), UnnecessaryLocalBeforeReturn (design ruleset), ProperLogger (logging-jakarta-commons ruleset), UselessOverridingMethod (basic ruleset), PackageCase (naming ruleset), NoPackage (naming ruleset), UnnecessaryCaseChange (strings ruleset)

View File

@ -28,6 +28,7 @@ public class AvoidConcatenatingNonLiteralsInStringBufferTest extends SimpleAggre
new TestDescriptor(TEST8, "usage of the StringBuffer constructor that takes an int", 0, rule),
new TestDescriptor(TEST9, "nested", 0, rule),
new TestDescriptor(TEST10, "looking up too high", 0, rule),
new TestDescriptor(TEST11, "looking too deep", 0, rule),
});
}
@ -110,4 +111,12 @@ public class AvoidConcatenatingNonLiteralsInStringBufferTest extends SimpleAggre
" }" + PMD.EOL +
"}";
private static final String TEST11 =
"public class Foo {" + PMD.EOL +
" public void bar(int i) {" + PMD.EOL +
" StringBuffer buf = new StringBuffer();" + PMD.EOL +
" buf.append(getFoo(getBar(i + \"hi\")));" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -8,6 +8,7 @@ These are new rules that are still in progress
<rule name="UselessAssignment"
message="This assignment to ''{0}'' is useless"
class="net.sourceforge.pmd.rules.UselessAssignment"

View File

@ -79,10 +79,9 @@ Avoid concatenating non literals in a StringBuffer constructor or append().
public class Foo {
void bar() {
// Avoid this
StringBuffer sb=new
StringBuffer("AAAAAAAAAA"+System.getProperty("java.io.tmpdir"));
StringBuffer sb=new StringBuffer("tmp = "+System.getProperty("java.io.tmpdir"));
// use instead something like this
StringBuffer sb = new StringBuffer("AAAAAAAAAA");
StringBuffer sb = new StringBuffer("tmp = ");
sb.append(System.getProperty("java.io.tmpdir"));
}
}

View File

@ -10,6 +10,7 @@ import net.sourceforge.pmd.ast.ASTBlockStatement;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.ast.ASTLiteral;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.Node;
/*
* How this rule works:
@ -59,6 +60,9 @@ public final class AvoidConcatenatingNonLiteralsInStringBuffer extends AbstractR
}
private boolean isInStringBufferAppend(ASTAdditiveExpression node) {
if (!eighthParentIsBlockStatement(node)) {
return false;
}
ASTBlockStatement s = (ASTBlockStatement) node.getFirstParentOfType(ASTBlockStatement.class);
if (s == null) {
return false;
@ -66,7 +70,18 @@ public final class AvoidConcatenatingNonLiteralsInStringBuffer extends AbstractR
ASTName n = (ASTName) s.getFirstChildOfType(ASTName.class);
return n.getImage()!=null && n.getImage().endsWith("append");
}
private boolean eighthParentIsBlockStatement(ASTAdditiveExpression node) {
Node curr = node;
for (int i=0; i<8; i++) {
if (node.jjtGetParent() == null) {
return false;
}
curr = curr.jjtGetParent();
}
return curr instanceof ASTBlockStatement;
}
private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) {
ASTAllocationExpression ao = (ASTAllocationExpression) node.getFirstParentOfType(ASTAllocationExpression.class);
if (ao == null) {

View File

@ -17,6 +17,7 @@ import java.util.Map;
public class StringToStringRule extends AbstractRule {
public Object visit(ASTVariableDeclaratorId node, Object data) {
// FIXME - use symbol table!!!
SimpleNode nameNode = node.getTypeNameNode();
if (nameNode instanceof ASTPrimitiveType) {
return data;

View File

@ -45,11 +45,11 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Andriy Rozeluk - bug report 1306180 for AvoidConcatenatingNonLiteralsInStringBuffer, reported bug 1293157 for UnusedPrivateMethod, suggested UnnecessaryCaseChange, bug report for SimplifyConditional, suggested UnnecessaryLocalBeforeReturn, suggestions for improving BooleanInstantiation, UnnecessaryReturn, AvoidDuplicateLiterals RFEs and bug reports, various other RFEs and thoughtful discussions as well</li>
<li>Thomas Dudziak - bug report 1304739 for StringInstantiation</li>
<li>Wouter Zelle - wrote NonThreadSafeSingleton rule, wrote DefaultPackage rule, worked thru ASTMethodDeclaration.isSyntacticallyX design, reported docs bug 1292689 for UnnecessaryLocalBeforeReturn, reported leftover ExceptionTypeChecking source file, rewrote UselessOverridingMethod in Java, UselessOverridingMethod rule, ProperLogger rule, nifty code to allow variables in XPath rules, externalInfoUrl data for all rules in basic and unusedcode rulesets, some very nifty XSLT, improvements to UseCorrectExceptionLogging, designed and implemented the "externalInfoUrl" feature in the rule definitions, fixed a devious bug in RuleSetFactory, AvoidPrintStackTrace, initial implementation of SimplifyConditional</li>
<li>Jorn Stampehl - Reported TooManyFields property bug, noticed redundancy of ExplicitCallToFinalize, reported bug in AvoidCallingFinalize, reported bug in JUnitAssertionsShouldIncludeMessage, reported bug in bug report on JUnitTestsShouldContainAsserts</li>
<li>Elliotte Rusty Harold - bug report 1293277 for duplicated rule messages, bug report for ConstructorCallsOverridableMethod, suggestion for improving command line interface, mispeling report, suggestion for improving Designer startup script, "how to make a ruleset" documentation suggestions, noticed outdated Xerces jars, script renaming suggestions, UseLocaleWithCaseConversions rule suggestion</li>
<li>Andriy Rozeluk - Reported bug 1293157 for UnusedPrivateMethod, suggested UnnecessaryCaseChange, bug report for SimplifyConditional, suggested UnnecessaryLocalBeforeReturn, suggestions for improving BooleanInstantiation, UnnecessaryReturn, AvoidDuplicateLiterals RFEs and bug reports, various other RFEs and thoughtful discussions as well</li>
<li>Pieter Bloemendaal - reported JDK 1.3 parsing bug 1292609, command line docs bug report, bug report for UnusedPrivateMethod, found typo in ArrayIsStoredDirectly, bug report for AvoidReassigningParametersRule</li>
<li>shawn2005 - documentation bug report</li>
<li>Benoit Xhenseval - bug report for UnusedPrivateMethod, suggestion to add elapsed time to XML report, bug report for ImmutableField, many bug reports (with good failure cases!), Ant task patch and bug report, XSLT patch, suggestion for improving XML report</li>