StringRules
SUGGESTION
Simplification
Suppression
Disable all rules by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$).*as compiler argument.
Table of contents
- EmptyString
- StringIdentity
- StringIsEmptyWithString
- StringIsEmpty
- NotStringIsEmpty
- StringsIsNullOrEmpty
- StringIsBlank
- OptionalOfNullableFilterNotStringIsEmpty
- OptionalFilterNotStringIsEmpty
- StringJoin
- StringJoinVarargs
- StringValueOfWithObject
- NewString3
- NewString1
- StringValueOf
- StringSubstring
- Utf8EncodedLength
- MathMaxNegativeOneStringIndexOfMinusInt
- MathMaxNegativeOneStringIndexOfMinusIntWithInt
- MathMaxNegativeOneStringIndexOfMinusString
- MathMaxNegativeOneStringIndexOfMinusStringWithInt
- MathMaxNegativeOneStringLastIndexOfMinusInt
- MathMaxNegativeOneStringLastIndexOfMinusString
- StringLastIndexOfMinusOneInt
- StringLastIndexOfMinusOneString
- StringStartsWith
- Formatted
EmptyString
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("EmptyString")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$EmptyString).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<String> testEmptyString() {
- return ImmutableSet.of(
- new String(),
- new String(new byte[0], UTF_8),
- new String(new byte[] {}, UTF_8),
- new String(new char[0]),
- new String(new char[] {}));
+ return ImmutableSet.of("", "", "", "", "");
}StringIdentity
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringIdentity")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIdentity).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
String testStringIdentity() {
- return new String("foo");
+ return "foo";
}StringIsEmptyWithString
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringIsEmptyWithString")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsEmptyWithString).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Boolean> testStringIsEmptyWithString() {
return ImmutableSet.of(
- "foo".length() == 0,
- "bar".length() <= 0,
- "baz".length() < 1,
- "qux".length() != 0,
- "quux".length() > 0,
- "corge".length() >= 1);
+ "foo".isEmpty(),
+ "bar".isEmpty(),
+ "baz".isEmpty(),
+ !"qux".isEmpty(),
+ !"quux".isEmpty(),
+ !"corge".isEmpty());
}StringIsEmpty
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringIsEmpty")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsEmpty).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
boolean testStringIsEmpty() {
- return Stream.of("foo").anyMatch(s -> s.isEmpty());
+ return Stream.of("foo").anyMatch(String::isEmpty);
}NotStringIsEmpty
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("NotStringIsEmpty")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$NotStringIsEmpty).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
boolean testNotStringIsEmpty() {
- return Stream.of("foo").anyMatch(s -> !s.isEmpty());
+ return Stream.of("foo").anyMatch(not(String::isEmpty));
}StringsIsNullOrEmpty
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringsIsNullOrEmpty")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringsIsNullOrEmpty).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Boolean> testStringsIsNullOrEmpty() {
return ImmutableSet.of(
- getClass().getName() == null || getClass().getName().isEmpty(),
- getClass().getName() != null && !getClass().getName().isEmpty());
+ Strings.isNullOrEmpty(getClass().getName()), !Strings.isNullOrEmpty(getClass().getName()));
}StringIsBlank
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringIsBlank")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsBlank).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Boolean> testStringIsBlank() {
- return ImmutableSet.of("foo".trim().isEmpty(), !"foo".trim().isEmpty());
+ return ImmutableSet.of("foo".isBlank(), !"foo".isBlank());
}OptionalOfNullableFilterNotStringIsEmpty
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalOfNullableFilterNotStringIsEmpty")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$OptionalOfNullableFilterNotStringIsEmpty).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<Optional<String>> testOptionalOfNullableFilterNotStringIsEmpty() {
return ImmutableSet.of(
- Strings.isNullOrEmpty(toString()) ? Optional.empty() : Optional.of(toString()),
- Strings.isNullOrEmpty(toString()) ? Optional.empty() : Optional.ofNullable(toString()),
- !Strings.isNullOrEmpty(toString()) ? Optional.of(toString()) : Optional.empty(),
- !Strings.isNullOrEmpty(toString()) ? Optional.ofNullable(toString()) : Optional.empty());
+ Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
+ Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
+ Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
+ Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)));
}OptionalFilterNotStringIsEmpty
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("OptionalFilterNotStringIsEmpty")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$OptionalFilterNotStringIsEmpty).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Optional<String> testOptionalFilterNotStringIsEmpty() {
- return Optional.of("foo").map(Strings::emptyToNull);
+ return Optional.of("foo").filter(not(String::isEmpty));
}StringJoin
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringJoin")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringJoin).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<String> testStringJoin() {
return ImmutableSet.of(
- Joiner.on("a").join(new String[] {"foo", "bar"}),
- Joiner.on("b").join(new CharSequence[] {"foo", "bar"}),
- Arrays.stream(new String[] {"foo", "bar"}).collect(joining("c")),
- Joiner.on("d").join(ImmutableList.of("foo", "bar")),
- Streams.stream(Iterables.cycle(ImmutableList.of("foo", "bar"))).collect(joining("e")),
- ImmutableList.of("foo", "bar").stream().collect(joining("f")));
+ String.join("a", new String[] {"foo", "bar"}),
+ String.join("b", new CharSequence[] {"foo", "bar"}),
+ String.join("c", new String[] {"foo", "bar"}),
+ String.join("d", ImmutableList.of("foo", "bar")),
+ String.join("e", Iterables.cycle(ImmutableList.of("foo", "bar"))),
+ String.join("f", ImmutableList.of("foo", "bar")));
}StringJoinVarargs
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringJoinVarargs")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringJoinVarargs).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
String testStringJoinVarargs() {
- return Stream.of("foo", "bar").collect(joining(","));
+ return String.join(",", "foo", "bar");
}StringValueOfWithObject
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringValueOfWithObject")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringValueOfWithObject).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
String testStringValueOfWithObject() {
- return Objects.toString("foo");
+ return String.valueOf("foo");
}NewString3
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("NewString3")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$NewString3).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<String> testNewString3() {
return ImmutableSet.of(
- String.valueOf(new char[] {'f', 'o', 'o'}, 0, 1),
- String.copyValueOf(new char[] {'b', 'a', 'r'}, 2, 3));
+ new String(new char[] {'f', 'o', 'o'}, 0, 1), new String(new char[] {'b', 'a', 'r'}, 2, 3));
}NewString1
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("NewString1")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$NewString1).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<String> testNewString1() {
return ImmutableSet.of(
- String.valueOf(new char[] {'f', 'o', 'o'}),
- new String(new char[] {'b', 'a', 'r'}, 0, new char[] {'b', 'a', 'r'}.length));
+ new String(new char[] {'f', 'o', 'o'}), new String(new char[] {'b', 'a', 'r'}));
}StringValueOf
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringValueOf")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringValueOf).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
Function<Object, String> testStringValueOf() {
- return Objects::toString;
+ return String::valueOf;
}StringSubstring
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringSubstring")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringSubstring).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
String testStringSubstring() {
- return "foo".substring(1, "foo".length());
+ return "foo".substring(1);
}Utf8EncodedLength
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("Utf8EncodedLength")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$Utf8EncodedLength).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testUtf8EncodedLength() {
- return "foo".getBytes(UTF_8).length;
+ return Utf8.encodedLength("foo");
}MathMaxNegativeOneStringIndexOfMinusInt
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("MathMaxNegativeOneStringIndexOfMinusInt")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$MathMaxNegativeOneStringIndexOfMinusInt).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testMathMaxNegativeOneStringIndexOfMinusInt() {
- return "foo".substring(1).indexOf('a');
+ return Math.max(-1, "foo".indexOf('a', 1) - 1);
}MathMaxNegativeOneStringIndexOfMinusIntWithInt
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("MathMaxNegativeOneStringIndexOfMinusIntWithInt")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$MathMaxNegativeOneStringIndexOfMinusIntWithInt).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testMathMaxNegativeOneStringIndexOfMinusIntWithInt() {
- return "foo".substring(1, 2).indexOf('a');
+ return Math.max(-1, "foo".indexOf('a', 1, 2) - 1);
}MathMaxNegativeOneStringIndexOfMinusString
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("MathMaxNegativeOneStringIndexOfMinusString")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$MathMaxNegativeOneStringIndexOfMinusString).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testMathMaxNegativeOneStringIndexOfMinusString() {
- return "foo".substring(1).indexOf("bar");
+ return Math.max(-1, "foo".indexOf("bar", 1) - 1);
}MathMaxNegativeOneStringIndexOfMinusStringWithInt
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("MathMaxNegativeOneStringIndexOfMinusStringWithInt")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$MathMaxNegativeOneStringIndexOfMinusStringWithInt).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testMathMaxNegativeOneStringIndexOfMinusStringWithInt() {
- return "foo".substring(1, 2).indexOf("bar");
+ return Math.max(-1, "foo".indexOf("bar", 1, 2) - 1);
}MathMaxNegativeOneStringLastIndexOfMinusInt
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("MathMaxNegativeOneStringLastIndexOfMinusInt")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$MathMaxNegativeOneStringLastIndexOfMinusInt).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testMathMaxNegativeOneStringLastIndexOfMinusInt() {
- return "foo".substring(1).lastIndexOf('a');
+ return Math.max(-1, "foo".lastIndexOf('a') - 1);
}MathMaxNegativeOneStringLastIndexOfMinusString
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("MathMaxNegativeOneStringLastIndexOfMinusString")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$MathMaxNegativeOneStringLastIndexOfMinusString).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testMathMaxNegativeOneStringLastIndexOfMinusString() {
- return "foo".substring(1).lastIndexOf("bar");
+ return Math.max(-1, "foo".lastIndexOf("bar") - 1);
}StringLastIndexOfMinusOneInt
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringLastIndexOfMinusOneInt")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringLastIndexOfMinusOneInt).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testStringLastIndexOfMinusOneInt() {
- return "foo".substring(0, 2).lastIndexOf('a');
+ return "foo".lastIndexOf('a', 2 - 1);
}StringLastIndexOfMinusOneString
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringLastIndexOfMinusOneString")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringLastIndexOfMinusOneString).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
int testStringLastIndexOfMinusOneString() {
- return "foo".substring(0, 2).lastIndexOf("bar");
+ return "foo".lastIndexOf("bar", 2 - 1);
}StringStartsWith
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("StringStartsWith")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$StringStartsWith).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
boolean testStringStartsWith() {
- return "foo".substring(1).startsWith("bar");
+ return "foo".startsWith("bar", 1);
}Formatted
SUGGESTION
Simplification
Suppression
Suppress false positives by adding the suppression annotation
@SuppressWarnings("Formatted")to the enclosing element.Disable this rule by adding
-XepOpt:Refaster:NamePattern=^(?!StringRules\$Formatted).*as compiler argument.
Samples
Shows the difference in example code before and after the Refaster rule is applied.
ImmutableSet<String> testFormatted() {
return ImmutableSet.of(
- String.format("Constant"),
- String.format("Number: %d", 42),
- String.format("%s" + "%s", "foo", "bar"));
+ ("Constant").formatted(),
+ ("Number: %d").formatted(42),
+ ("%s" + "%s").formatted("foo", "bar"));
}