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. StringIsEmptyPredicate
  3. StringIsNotEmptyPredicate
  4. StringIsNullOrEmpty
  5. OptionalNonEmptyString
  6. FilterEmptyString
  7. JoinStrings
  8. StringValueOf
  9. NewStringFromCharArraySubSequence
  10. NewStringFromCharArray
  11. StringValueOfMethodReference
  12. SubstringRemainder
  13. 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());
   }

StringIsEmptyPredicate

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 boolean testStringIsEmptyPredicate() {
-    return Stream.of("foo").anyMatch(s -> s.isEmpty());
+    return Stream.of("foo").anyMatch(String::isEmpty);
   }

StringIsNotEmptyPredicate

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 boolean testStringIsNotEmptyPredicate() {
-    return Stream.of("foo").anyMatch(s -> !s.isEmpty());
+    return Stream.of("foo").anyMatch(not(String::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(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)));
   }

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(not(String::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");
   }

NewStringFromCharArraySubSequence

SUGGESTION

Simplification

Suppression

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

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

Samples

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

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

NewStringFromCharArray

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 ImmutableSet<String> testNewStringFromCharArray() {
     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'}));
   }

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-2024 Picnic Technologies BV