forked from phoedos/pmd
Added a new if to check for the append statements after an append contructor.
Summary: Now the rule check for appends after a contructor call. Added a new test for this case. Issue: http://sourceforge.net/p/pmd/bugs/1370/ Test Plan: Run tests. Reviewers: jmsotuyo Reviewed By: jmsotuyo Differential Revision: http://ph.monits.com/D12292
This commit is contained in:
@ -11,6 +11,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
|
||||
@ -88,19 +89,41 @@ public class ConsecutiveAppendsShouldReuseRule extends AbstractJavaRule {
|
||||
ASTStatement statement = (ASTStatement) node.jjtGetChild(0);
|
||||
if (isFirstChild(statement, ASTStatementExpression.class)) {
|
||||
ASTStatementExpression stmtExp = (ASTStatementExpression) statement.jjtGetChild(0);
|
||||
ASTPrimaryPrefix primaryPrefix = stmtExp.getFirstDescendantOfType(ASTPrimaryPrefix.class);
|
||||
if (primaryPrefix != null) {
|
||||
ASTName name = primaryPrefix.getFirstChildOfType(ASTName.class);
|
||||
if (name != null) {
|
||||
String image = name.getImage();
|
||||
if (image.endsWith(".append")) {
|
||||
String variable = image.substring(0, image.indexOf('.'));
|
||||
if (isAStringBuilderBuffer(primaryPrefix, variable)) {
|
||||
return variable;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (stmtExp.jjtGetNumChildren() == 1) {
|
||||
ASTPrimaryPrefix primaryPrefix = stmtExp.getFirstDescendantOfType(ASTPrimaryPrefix.class);
|
||||
if (primaryPrefix != null) {
|
||||
ASTName name = primaryPrefix.getFirstChildOfType(ASTName.class);
|
||||
if (name != null) {
|
||||
String image = name.getImage();
|
||||
if (image.endsWith(".append")) {
|
||||
String variable = image.substring(0, image.indexOf('.'));
|
||||
if (isAStringBuilderBuffer(primaryPrefix, variable)) {
|
||||
return variable;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final ASTExpression exp = stmtExp.getFirstDescendantOfType(ASTExpression.class);
|
||||
if (isFirstChild(exp, ASTPrimaryExpression.class)) {
|
||||
final ASTPrimarySuffix primarySuffix = ((ASTPrimaryExpression) exp.jjtGetChild(0)).getFirstDescendantOfType(ASTPrimarySuffix.class);
|
||||
if (primarySuffix != null) {
|
||||
final String name = primarySuffix.getImage();
|
||||
if (name != null && name.equals("append")) {
|
||||
final ASTPrimaryExpression pExp = stmtExp.getFirstDescendantOfType(ASTPrimaryExpression.class);
|
||||
if (pExp != null) {
|
||||
final ASTName astName = stmtExp.getFirstDescendantOfType(ASTName.class);
|
||||
if (astName != null) {
|
||||
final String variable = astName.getImage();
|
||||
if (isAStringBuilderBuffer(primarySuffix, variable)) {
|
||||
return variable;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isFirstChild(node, ASTLocalVariableDeclaration.class)) {
|
||||
ASTLocalVariableDeclaration lvd = (ASTLocalVariableDeclaration) node.jjtGetChild(0);
|
||||
|
@ -214,6 +214,7 @@ public class Foo {
|
||||
public class Foo {
|
||||
public void foo() {
|
||||
final StringBuffer stringBuffer = new StringBuffer().append("agrego ").append("un ");
|
||||
stringBuffer = new StringBuffer().append("agrego ").append("un ");
|
||||
stringBuffer.append("string ");
|
||||
}
|
||||
}
|
||||
@ -234,6 +235,19 @@ public class StringBufferTest {
|
||||
stringBuffer2.append("string "); // but not on this one
|
||||
Log.i(TAG, stringBuffer2.toString());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#1370 ConsecutiveAppendsShouldReuse not detected properly on StringBuffer - part 3</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void foo() {
|
||||
final StringBuffer stringBuffer;
|
||||
stringBuffer = new StringBuffer().append("agrego ").append("un ");
|
||||
stringBuffer.append("string ");
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user