[java] InsufficientStringBufferDeclaration: Fix CCE for Character

Fixes #5314
This commit is contained in:
Andreas Dangel
2024-11-14 17:48:08 +01:00
parent bb2782241e
commit 3fdbf7d6cb
3 changed files with 28 additions and 1 deletions
@@ -240,6 +240,12 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRulecha
private int calculateExpression(ASTExpression expression) {
Object value = expression.getConstValue();
return value == null ? State.UNKNOWN_CAPACITY : (Integer) value;
if (value == null) {
return State.UNKNOWN_CAPACITY;
}
if (value instanceof Character) {
return (Character) value;
}
return (Integer) value;
}
}
@@ -1419,4 +1419,23 @@ public class LiteralExpression {
}
]]></code>
</test-code>
<test-code>
<description>#5314 [java] InsufficientStringBufferDeclarationRule: Lack of handling for char type parameters</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class StringBufferInstantiationWithCharTest {
public void example01() {
// misleading instantiation, these buffers
// are actually sized to 99 characters long
StringBuffer sb1 = new StringBuffer('c');
StringBuilder sb2 = new StringBuilder('c');
// in these forms, just single characters are allocated
StringBuffer sb3 = new StringBuffer("c");
StringBuilder sb4 = new StringBuilder("c");
}
}
]]></code>
</test-code>
</test-data>