resources.report.rules.pmd.StringBufferInstantiationWithChar.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
The newest version!
StringBufferInstantiationWithChar
StringBufferInstantiationWithChar
Individual character values provided as initialization arguments will be converted into integers. This can lead to internal buffer sizes that are larger than expected. Some examples:
new StringBuffer() // 16 new StringBuffer(6) // 6 new StringBuffer(“hello world”) // 11 + 16 = 27 new StringBuffer(‘A’) // chr(A) = 65 new StringBuffer(“A”) // 1 + 16 = 17
new StringBuilder() // 16 new StringBuilder(6) // 6 new StringBuilder(“hello world”) // 11 + 16 = 27 new StringBuilder(‘C’) // chr(C) = 67 new StringBuilder(“A”) // 1 + 16 = 17
//AllocationExpression/ClassOrInterfaceType
[@Image='StringBuffer' or @Image='StringBuilder']
/../Arguments/ArgumentList/Expression/PrimaryExpression
/PrimaryPrefix/
Literal
[starts-with(@Image, "'")]
[ends-with(@Image, "'")]
Example(s):
// 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");