forked from phoedos/pmd
#1380 InsufficientStringBufferDeclaration false positive when literal string passed to a lookup service
This commit is contained in:
@ -192,8 +192,10 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
|
||||
ASTLiteral literal = (ASTLiteral) xn.jjtGetChild(0);
|
||||
String str = xn.jjtGetChild(0).getImage();
|
||||
if (str != null) {
|
||||
if(isStringOrCharLiteral(literal)){
|
||||
anticipatedLength += str.length() - 2;
|
||||
if (literal.isStringLiteral()) {
|
||||
anticipatedLength += str.length() - 2;
|
||||
} else if (literal.isCharLiteral()) {
|
||||
anticipatedLength += 1;
|
||||
} else if(literal.isIntLiteral() && str.startsWith("0x")){
|
||||
// but only if we are not inside a cast expression
|
||||
Node parentNode = literal.jjtGetParent().jjtGetParent().jjtGetParent();
|
||||
@ -316,7 +318,10 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
|
||||
}
|
||||
|
||||
private boolean isAdditive(Node n) {
|
||||
return n.hasDescendantOfType(ASTAdditiveExpression.class);
|
||||
ASTAdditiveExpression add = n.getFirstDescendantOfType(ASTAdditiveExpression.class);
|
||||
// if the first descendant additive expression is deeper than 4 levels,
|
||||
// it belongs to a nested method call and not anymore to the append argument.
|
||||
return add != null && add.getNthParent(4) == n;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -993,6 +993,36 @@ public class StringBufferTest {
|
||||
final StringBuffer stringBuffer = new StringBuffer().append("Added ").append(" a ");
|
||||
stringBuffer.append("string longer than 16 characters");
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#1380 InsufficientStringBufferDeclaration false positive when literal string passed to a lookup service</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.util.Locale;
|
||||
import org.springframework.context.MessageSource;
|
||||
|
||||
public class StringBuilderWithMessageRetrieval {
|
||||
private final MessageSource messageSource;
|
||||
|
||||
public StringBuilderWithMessageRetrieval(MessageSource messageSource) {
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
public void run(String[] strings) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
boolean notFirst = false;
|
||||
for (String string : strings) {
|
||||
if (notFirst) {
|
||||
builder.append('\n');
|
||||
}
|
||||
|
||||
builder.append(messageSource.getMessage("some.long.label." + string, null, Locale.ENGLISH));
|
||||
notFirst = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
@ -14,6 +14,7 @@
|
||||
|
||||
* [#1370](https://sourceforge.net/p/pmd/bugs/1370/): ConsecutiveAppendsShouldReuse not detected properly on StringBuffer
|
||||
* [#1371](https://sourceforge.net/p/pmd/bugs/1371/): InsufficientStringBufferDeclaration not detected properly on StringBuffer
|
||||
* [#1380](https://sourceforge.net/p/pmd/bugs/1380/): InsufficientStringBufferDeclaration false positive when literal string passed to a lookup service
|
||||
* [#1384](https://sourceforge.net/p/pmd/bugs/1384/): NullPointerException in ConsecutiveLiteralAppendsRule
|
||||
* [#1388](https://sourceforge.net/p/pmd/bugs/1388/): ConstructorCallsOverridableMethodRule doesn't work with params?
|
||||
* [#1392](https://sourceforge.net/p/pmd/bugs/1392/): SimplifyStartsWith false-negative
|
||||
|
Reference in New Issue
Block a user