Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce CharSequenceRules Refaster rule collection #1480

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rickie
Copy link
Member

@rickie rickie commented Dec 25, 2024

This aims to solve the following issue: #1394.

@timtebeek mentioned that OpenRewrite has some checks that ensure that only the correct recipes are loaded for the Java version it's running on. For us I also don't see any problems for running on our code. The only advantage is that we will be matching more now :).

@rickie rickie added this to the 0.20.0 milestone Dec 25, 2024
final class CharSequenceRulesTest implements RefasterRuleCollectionTestCase {
ImmutableSet<Boolean> testCharSequenceIsEmpty() {
return ImmutableSet.of(
((CharSequence) "foo").length() == 0,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a better way, but now we know for sure its a CharSequence :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new StringBuilder("foo"), str.subSequence(0, 1), ... :)

Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 force-pushed the rossendrijver/char_sequence branch from bb69636 to b7754d9 Compare December 25, 2024 13:21
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased and added a commit. Suggested commit message:

Introduce `CharSequenceRules` Refaster rule collection (#1480)

Resolves #1394.

*/
static final class CharSequenceIsEmpty {
@BeforeTemplate
boolean before(CharSequence ch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
boolean before(CharSequence ch) {
boolean before(CharSequence charSequence) {

As ch generally denotes "char"`. (Unifying the variable naming approach is also on my list, somewhere ;).)

@@ -32,6 +32,7 @@ private StringRules() {}
// 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. */)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scope of this suppression should be restricted to the @BeforeTemplate.

Comment on lines 32 to 34
// 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+.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be updated.

final class CharSequenceRulesTest implements RefasterRuleCollectionTestCase {
ImmutableSet<Boolean> testCharSequenceIsEmpty() {
return ImmutableSet.of(
((CharSequence) "foo").length() == 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new StringBuilder("foo"), str.subSequence(0, 1), ... :)

* Prefer {@link CharSequence#isEmpty()} over alternatives that consult the char sequence's
* length.
*/
static final class CharSequenceIsEmpty {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document this rule's relation to StringIsEmpty.

Comment on lines 12 to 14
((CharSequence) "foo").length() != 0,
((CharSequence) "bar").length() > 0,
((CharSequence) "baz").length() >= 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"qux", "quux", "corge" (we inconsistently sometimes also include "quuz"; something to unify another day ;).)

Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants