StringJoin
SUGGESTION
Simplification
Summary
Prefer
String#join
overString#format
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringJoin")
to the enclosing element.Disable this pattern completely by adding
-Xep:StringJoin:OFF
as compiler argument. Learn more.
Samples
Replacement
Shows the difference in example code before and after the bug pattern is applied.
class A {
void m() {
- String.format("%s", getClass().getName());
- String.format("%s%s", getClass().getName(), getClass().getName());
- String.format("%s%s", getClass().getName(), hashCode());
- String.format("%s%s", hashCode(), getClass().getName());
- String.format("%s-%s", getClass().getName(), getClass().getName());
- String.format("%saa%s", getClass().getName(), getClass().getName());
- String.format("%s\"%s", getClass().getName(), getClass().getName());
- String.format("%s_%s_%s", getClass().getName(), getClass().getName(), getClass().getName());
+ String.valueOf(getClass().getName());
+ String.join("", getClass().getName(), getClass().getName());
+ String.join("", getClass().getName(), String.valueOf(hashCode()));
+ String.join("", String.valueOf(hashCode()), getClass().getName());
+ String.join("-", getClass().getName(), getClass().getName());
+ String.join("aa", getClass().getName(), getClass().getName());
+ String.join("\"", getClass().getName(), getClass().getName());
+ String.join("_", getClass().getName(), getClass().getName(), getClass().getName());
}
}
Identification
Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains:
comment is placed above any violating line.
import java.util.Formattable;
import java.util.Locale;
class A {
void m() {
String.join("-", getClass().getName());
String.format(getClass().getName(), getClass().getName());
String.format(Locale.ROOT, "%s", getClass().getName());
String.format("%20s", getClass().getName());
// BUG: Diagnostic matches: valueOf
String.format("%s", getClass().getName());
// BUG: Diagnostic matches: valueOf
String.format("%s", hashCode());
String.format("%s", (Formattable) null);
String.format("-%s", getClass().getName());
String.format("%s-", getClass().getName());
String.format("-%s-", getClass().getName());
// BUG: Diagnostic matches: join
String.format("%s%s", getClass().getName(), getClass().getName());
// BUG: Diagnostic matches: join
String.format("%s%s", getClass().getName(), hashCode());
// BUG: Diagnostic matches: join
String.format("%s%s", hashCode(), getClass().getName());
String.format("%s%s", getClass().getName(), (Formattable) null);
String.format("%s%s", (Formattable) null, getClass().getName());
String.format("%s%s", getClass().getName());
// BUG: Diagnostic matches: join
String.format("%s-%s", getClass().getName(), getClass().getName());
// BUG: Diagnostic matches: join
String.format("%saa%s", getClass().getName(), getClass().getName());
String.format("%s%%%s", getClass().getName(), getClass().getName());
// BUG: Diagnostic matches: join
String.format("%s_%s_%s", getClass().getName(), getClass().getName(), getClass().getName());
String.format("%s_%s_%s", getClass().getName(), getClass().getName());
String.format("%s_%s-%s", getClass().getName(), getClass().getName(), getClass().getName());
}
}