Affects PMD Version:
Rule:
InsufficientStringBufferDeclaration
Description:
When calculating the size of a StringBuilder at runtime, e.g. by adding the lengths of strings to be appended or by adding constant values e.g. for the output of Date/DecimalFormat will result in a InsufficientStringBufferDeclaration even if the calculated value will be larger than the length of the constant strings alone.
Code Sample demonstrating the issue:
public String foo(final String x, final String y, final double z, final Date d, final double v) {
final StringBuilder sb = new StringBuilder(20 + 9*2 + 16 + 2 * x.length() + y.length());
final DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);
df.applyPattern("#,##0.000");
final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
sb.append("foobar")
.append(df.format(z))
.append(' ')
.append(x)
.append(y)
.append("foobar")
.append(sdf.format(d))
.append("foobar")
.append(df.format(v))
.append(' ')
.append(x);
return sb.toString();
}
Running PMD through: Gradle
Affects PMD Version:
Rule:
InsufficientStringBufferDeclaration
Description:
When calculating the size of a
StringBuilderat runtime, e.g. by adding the lengths of strings to be appended or by adding constant values e.g. for the output of Date/DecimalFormat will result in aInsufficientStringBufferDeclarationeven if the calculated value will be larger than the length of the constant strings alone.Code Sample demonstrating the issue:
Running PMD through: Gradle