Skip to main content Link Search Menu Expand Document (external link)

StringRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

Disable all rules by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules.*)) as compiler argument.

Table of contents
  1. StringIsEmpty
  2. StringIsNullOrEmpty
  3. OptionalNonEmptyString
  4. FilterEmptyString
  5. JoinStrings
  6. StringValueOf
  7. StringValueOfMethodReference
  8. SubstringRemainder
  9. Utf8EncodedLength

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.

 ImmutableSet<Boolean> testStringIsEmpty() {
     return ImmutableSet.of(
-        "foo".length() == 0,
-        "bar".length() <= 0,
-        "baz".length() < 1,
-        "foo".length() != 0,
-        "bar".length() > 0,
-        "baz".length() >= 1);
+        "foo".isEmpty(),
+        "bar".isEmpty(),
+        "baz".isEmpty(),
+        !"foo".isEmpty(),
+        !"bar".isEmpty(),
+        !"baz".isEmpty());
   }

StringIsNullOrEmpty

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIsNullOrEmpty") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules\.StringIsNullOrEmpty)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Boolean> testStringIsNullOrEmpty() {
     return ImmutableSet.of(
-        getClass().getName() == null || getClass().getName().isEmpty(),
-        getClass().getName() != null && !getClass().getName().isEmpty());
+        Strings.isNullOrEmpty(getClass().getName()), !Strings.isNullOrEmpty(getClass().getName()));
   }

OptionalNonEmptyString

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("OptionalNonEmptyString") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules\.OptionalNonEmptyString)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Optional<String>> testOptionalNonEmptyString() {
     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(s -> !s.isEmpty()),
+        Optional.ofNullable(toString()).filter(s -> !s.isEmpty()),
+        Optional.ofNullable(toString()).filter(s -> !s.isEmpty()),
+        Optional.ofNullable(toString()).filter(s -> !s.isEmpty()));
   }

FilterEmptyString

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("FilterEmptyString") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules\.FilterEmptyString)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Optional<String> testFilterEmptyString() {
-    return Optional.of("foo").map(Strings::emptyToNull);
+    return Optional.of("foo").filter(s -> !s.isEmpty());
   }

JoinStrings

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("JoinStrings") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules\.JoinStrings)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testJoinStrings() {
     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")));
   }

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.

 String testStringValueOf() {
-    return Objects.toString("foo");
+    return String.valueOf("foo");
   }

StringValueOfMethodReference

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringValueOfMethodReference") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules\.StringValueOfMethodReference)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Function<Object, String> testStringValueOfMethodReference() {
-    return Objects::toString;
+    return String::valueOf;
   }

SubstringRemainder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("SubstringRemainder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(StringRules\.SubstringRemainder)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 String testSubstringRemainder() {
-    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");
   }

Copyright © 2017-2023 Picnic Technologies BV