[java] Add test cases for InefficientStringBuffering
Originally from #1932 Author: jborgers <jborgers@jpinpoint.com>
This commit is contained in:
@ -340,6 +340,84 @@ public class Foo {
|
|||||||
String str2 = "b";
|
String str2 = "b";
|
||||||
StringBuffer sb = new StringBuffer(str1.length() + str2.length());
|
StringBuffer sb = new StringBuffer(str1.length() + str2.length());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Violation: Avoid contact in append method invocations</description>
|
||||||
|
<expected-problems>3</expected-problems>
|
||||||
|
<expected-linenumbers>9,18,24</expected-linenumbers>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
|
||||||
|
private static final String STATIC_FINAL_F = "static_final_field";
|
||||||
|
private static String static_f = "static_field";
|
||||||
|
private final String FINAL_F = "final_field";
|
||||||
|
|
||||||
|
private void concatIsBad(String arg) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("arg='" + arg + "'"); // bad
|
||||||
|
}
|
||||||
|
|
||||||
|
private void concatNumericIsBad() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(1 + 2); // bad
|
||||||
|
}
|
||||||
|
|
||||||
|
private String testStaticBad() {
|
||||||
|
StringBuilder sb = new StringBuilder().append("fld:" + static_f); // bad
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String testFinalLocalBad() {
|
||||||
|
String local = "local"; // non-final, jdk9+ optimized with indified Strings, assumed still worse
|
||||||
|
return new StringBuilder().append("fld:" + local).toString(); // assumed bad
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>No violation: Avoid contact in append method invocations</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
|
||||||
|
private static final String STATIC_FINAL_F = "static_final_field";
|
||||||
|
private final String FINAL_F = "final_field";
|
||||||
|
|
||||||
|
private void good(String arg) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("arg='").append(arg).append("'");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddInSecondOrThirdArgAppendGood() {
|
||||||
|
StringBuilder wrappedLine = new StringBuilder();
|
||||||
|
String str = "bar";
|
||||||
|
int offset = 1;
|
||||||
|
int wrapLength = 2;
|
||||||
|
wrappedLine.append(str, offset + 1, wrapLength + offset + 1); // + but no string concat
|
||||||
|
}
|
||||||
|
|
||||||
|
private String testLiteralsGood(String arg) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("lit:" + "etc");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String testFinalFieldGood() { // final assumed a constant expression
|
||||||
|
return new StringBuilder().append("fld:" + FINAL_F).toString(); //good
|
||||||
|
}
|
||||||
|
|
||||||
|
private String testStaticFinalFieldGood() {
|
||||||
|
return new StringBuilder().append("fld:" + STATIC_FINAL_F).toString(); // good
|
||||||
|
}
|
||||||
|
|
||||||
|
private String testFinalLocalGood() {
|
||||||
|
final String local = "local"; // final assumed a constant expression
|
||||||
|
return new StringBuilder().append("fld:" + local).toString(); // good
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user