[java] ConsecutiveLiteralAppends - fix reported line numbers

This commit is contained in:
Andreas Dangel committed 2021-06-11 17:07:04 +02:00
1 parent f02e7c6ab0
commit 616bc57882
2 files changed
+34 -5

No files matched your search

@@ -187,7 +187,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule {
counter.reset();
} else if (invocation.getArguments().getFirstChild() instanceof ASTStringLiteral
|| invocation instanceof ASTMethodCall) {
counter.count(invocation);
counter.count(invocation.getArguments().getFirstChild());
}
}
@@ -198,9 +198,10 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule {
checkForViolation(data);
if (firstArg instanceof ASTInfixExpression) {
if (((ASTInfixExpression) firstArg).getRightOperand() instanceof ASTStringLiteral) {
ASTExpression rightOperand = ((ASTInfixExpression) firstArg).getRightOperand();
if (rightOperand instanceof ASTStringLiteral) {
// argument ends with ... + "some string"
counter.count(invocation);
counter.count(rightOperand);
}
} else {
// continue with a fresh round
@@ -209,7 +210,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRulechainRule {
} else {
// no variables appended, compiler will take care of merging all the
// string concats, we really only have 1 then
counter.count(invocation);
counter.count(invocation.getArguments().getFirstChild());
}
}
@@ -953,7 +953,7 @@ public class Foo {
<test-code>
<description>32, Including the constructor's string</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>3,8,15,26</expected-linenumbers>
<expected-linenumbers>3,8,15,27</expected-linenumbers>
<code><![CDATA[
public class Foo {
public void bar() {
@@ -1671,6 +1671,34 @@ public class ConsecutiveLiteralAppendsForEach {
sb.append('\t').append(s).append('\n');
}
}
}
]]></code>
</test-code>
<test-code>
<description>Wrong count of appends - should be 3</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>4,8</expected-linenumbers>
<expected-messages>
<message>StringBuffer (or StringBuilder).append is called 3 consecutive times with literals. Use a single append with a single combined String.</message>
<message>StringBuffer (or StringBuilder).append is called 2 consecutive times with literals. Use a single append with a single combined String.</message>
</expected-messages>
<code><![CDATA[
public class ConsecutiveLiteralAppends3 {
public String createMessage(String description) {
StringBuilder sb = new StringBuilder(description)
.append("\n") // <--- here
.append("Endpoint handler details:\n")
.append("Method [")
.append(this.getMethod())
.append("]\n") // <--- here
.append("Bean [")
.append(this.getBean())
.append("]\n");
return sb.toString();
}
public String getMethod() { return "method"; }
public String getBean() { return "bean"; }
}
]]></code>
</test-code>