Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Dec 25, 2024
1 parent 97c4c57 commit b7754d9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ private CharSequenceRules() {}
* Prefer {@link CharSequence#isEmpty()} over alternatives that consult the char sequence's
* length.
*/
// XXX: Drop this rule once we (and OpenRewrite) no longer support projects targeting Java 14 or
// below.
static final class CharSequenceIsEmpty {
@BeforeTemplate
boolean before(CharSequence ch) {
return Refaster.anyOf(ch.length() == 0, ch.length() <= 0, ch.length() < 1);
boolean before(CharSequence charSequence) {
return Refaster.anyOf(
charSequence.length() == 0, charSequence.length() <= 0, charSequence.length() < 1);
}

@AfterTemplate
@AlsoNegation
boolean after(CharSequence ch) {
return ch.isEmpty();
boolean after(CharSequence charSequence) {
return charSequence.isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ final class StringRules {
private StringRules() {}

/** Prefer {@link String#isEmpty()} over alternatives that consult the string's length. */
// XXX: Now that we build with JDK 15+, this rule can be generalized to cover all `CharSequence`
// subtypes. This does require a mechanism (perhaps an annotation, or a separate Maven module) to
// make sure that non-String expressions are rewritten only if client code also targets JDK 15+.
@SuppressWarnings("CharSequenceIsEmpty" /* This is a more specific template. */)
// XXX: Drop this rule once we (and OpenRewrite) no longer support projects targeting Java 14 or
// below. The `CharSequenceIsEmpty` rule then suffices. (This rule exists so that e.g. projects
// that target JDK 11 can disable `CharSequenceIsEmpty` without losing a valuable rule.)
// XXX: Look into a more general approach to supporting different Java language levels, such as
// rule selection based on some annotation, or a separate Maven module.
static final class StringIsEmpty {
@BeforeTemplate
@SuppressWarnings("CharSequenceIsEmpty" /* This is a more specific template. */)
boolean before(String str) {
return Refaster.anyOf(str.length() == 0, str.length() <= 0, str.length() < 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
final class CharSequenceRulesTest implements RefasterRuleCollectionTestCase {
ImmutableSet<Boolean> testCharSequenceIsEmpty() {
return ImmutableSet.of(
((CharSequence) "foo").length() == 0,
((CharSequence) "bar").length() <= 0,
((CharSequence) "baz").length() < 1,
((CharSequence) "foo").length() != 0,
((CharSequence) "bar").length() > 0,
((CharSequence) "baz").length() >= 1);
new StringBuilder("foo").length() == 0,
new StringBuilder("bar").length() <= 0,
new StringBuilder("baz").length() < 1,
new StringBuilder("qux").length() != 0,
new StringBuilder("quux").length() > 0,
new StringBuilder("corge").length() >= 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
final class CharSequenceRulesTest implements RefasterRuleCollectionTestCase {
ImmutableSet<Boolean> testCharSequenceIsEmpty() {
return ImmutableSet.of(
((CharSequence) "foo").isEmpty(),
((CharSequence) "bar").isEmpty(),
((CharSequence) "baz").isEmpty(),
!((CharSequence) "foo").isEmpty(),
!((CharSequence) "bar").isEmpty(),
!((CharSequence) "baz").isEmpty());
new StringBuilder("foo").isEmpty(),
new StringBuilder("bar").isEmpty(),
new StringBuilder("baz").isEmpty(),
!new StringBuilder("qux").isEmpty(),
!new StringBuilder("quux").isEmpty(),
!new StringBuilder("corge").isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ ImmutableSet<Boolean> testStringIsEmpty() {
"foo".length() == 0,
"bar".length() <= 0,
"baz".length() < 1,
"foo".length() != 0,
"bar".length() > 0,
"baz".length() >= 1);
"qux".length() != 0,
"quux".length() > 0,
"corge".length() >= 1);
}

boolean testStringIsEmptyPredicate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ ImmutableSet<Boolean> testStringIsEmpty() {
"foo".isEmpty(),
"bar".isEmpty(),
"baz".isEmpty(),
!"foo".isEmpty(),
!"bar".isEmpty(),
!"baz".isEmpty());
!"qux".isEmpty(),
!"quux".isEmpty(),
!"corge".isEmpty());
}

boolean testStringIsEmptyPredicate() {
Expand Down

0 comments on commit b7754d9

Please sign in to comment.