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. EmptyString
  2. StringIdentity
  3. StringIsEmpty
  4. StringIsEmptyPredicate
  5. StringIsNotEmptyPredicate
  6. StringIsNullOrEmpty
  7. StringIsBlank
  8. OptionalNonEmptyString
  9. FilterEmptyString
  10. JoinStrings
  11. StringValueOf
  12. NewStringFromCharArraySubSequence
  13. NewStringFromCharArray
  14. StringValueOfMethodReference
  15. SubstringRemainder
  16. Utf8EncodedLength
  17. StringIndexOfChar
  18. StringIndexOfString
  19. StringLastIndexOfChar
  20. StringLastIndexOfString
  21. StringLastIndexOfCharWithIndex
  22. StringLastIndexOfStringWithIndex
  23. StringStartsWith

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";
 }

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,
-      "qux".length() != 0,
-      "quux".length() > 0,
-      "corge".length() >= 1);
+      "foo".isEmpty(),
+      "bar".isEmpty(),
+      "baz".isEmpty(),
+      !"qux".isEmpty(),
+      !"quux".isEmpty(),
+      !"corge".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()));
 }

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.

 boolean testStringIsBlank() {
-  return "foo".trim().isEmpty();
+  return "foo".isBlank();
 }

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");
 }

StringIndexOfChar

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 int testStringIndexOfChar() {
-  return "foo".substring(1).indexOf('a');
+  return Math.max(-1, "foo".indexOf('a', 1) - 1);
 }

StringIndexOfString

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 int testStringIndexOfString() {
-  return "foo".substring(1).indexOf("bar");
+  return Math.max(-1, "foo".indexOf("bar", 1) - 1);
 }

StringLastIndexOfChar

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 int testStringLastIndexOfChar() {
-  return "foo".substring(1).lastIndexOf('a');
+  return Math.max(-1, "foo".lastIndexOf('a') - 1);
 }

StringLastIndexOfString

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 int testStringLastIndexOfString() {
-  return "foo".substring(1).lastIndexOf("bar");
+  return Math.max(-1, "foo".lastIndexOf("bar") - 1);
 }

StringLastIndexOfCharWithIndex

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 int testStringLastIndexOfCharWithIndex() {
-  return "foo".substring(0, 2).lastIndexOf('a');
+  return "foo".lastIndexOf('a', 2 - 1);
 }

StringLastIndexOfStringWithIndex

SUGGESTION

Simplification

Suppression

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

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

Samples

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

 int testStringLastIndexOfStringWithIndex() {
-  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);
 }

Copyright © 2017-2024 Picnic Technologies BV