-
Notifications
You must be signed in to change notification settings - Fork 39
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/extend RequireNonNullElse{,Get}
Refaster rules
#425
Introduce/extend RequireNonNullElse{,Get}
Refaster rules
#425
Conversation
requireNonNullElse(A, B)
and `requ…requireNonNullElse(A, B)
over Optional alternative.
Looks good. No mutations were possible for these changes. |
Thanks for the PR @benhalasi! I'll have a closer look at the changes later.
Yes, this a known issue. For this we have a little trick; see how |
1739c77
to
399fee6
Compare
There was a problem hiding this 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. Thanks for your contribution @benhalasi!
Suggested commit message:
Introduce/extend `RequireNonNullElse{,Get}` Refaster rules (#425)
Fixes #364.
The next release may be a few weeks out, but if you'd like to use these changes sooner, let us know and we'll see what we can do :)
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
T after(T first, T second) { | ||
return requireNonNullElse(first, second); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Objects#requireNonNullElseGet(Object, Supplier)} over {@link Optional}{@code | ||
* .ofNullable(Object).orElseGet(Supplier)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than reconstructing the @BeforeTemplate
code, in Javadoc we often use more generic phrasing. In other classes we often talk about "more contrived alternatives".
@BeforeTemplate | ||
T beforeOptional(T first, T second) { | ||
return Optional.ofNullable(first).orElse(second); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Refaster.anyOf
we can collapse the two @BeforeTemplate
s. We generally prefer that approach where feasible.
*/ | ||
// XXX: This rule is not valid in case `supplier` may return `null`: Optional will return `null`, | ||
// while the `requireNonNullElseGet` replacement will throw an NPE. | ||
static final class RequireNonNullElseGet<T, S extends Supplier<T>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of subtype matching is done automatically by Refaster. That said, there's indeed work to be done here: we should introduce S extends T
, so that we can match Supplier<S>
.
Looks good. No mutations were possible for these changes. |
399fee6
to
ea7f5c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased and have one small remark for @Stephan202.
Aside from that; changes LGTM! Suggested commit message also LGTM!
@benhalasi Thanks for your contribution!
* Prefer {@link Objects#requireNonNullElse(Object, Object)} over non-JDK or more contrived | ||
* alternatives. | ||
*/ | ||
// XXX: This rule is not valid in case `second` is `@Nullable`: in that case the Guava and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Stephan202 we looked into a NullableMatcher
related to this, maybe we should prioritize that one again sometime. Such master could be useful here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/master/matcher/
;)
Indeed I started work on that here, but I recall hitting some issues. (The AbstractMatcherTestChecker
changes are part of that, but IIRC there was more. Would have to check out the branch and run the tests to refresh my memory.)
Looks good. No mutations were possible for these changes. |
Shall we add |
SGTM; updated the suggested commit message. (There are also @EnricSala's observations around |
Rebased. Will soon make the earlier mentioned GH issues. Sorry, haven't come around do doing it yet. Will merge right after that. |
ea7f5c6
to
67c1519
Compare
Looks good. No mutations were possible for these changes. |
…ireNonNullElseGet(A, () -> B)` over Optional alternative. Added Rules: (NullRules) - prefer `requireNonNullElse(A, B)` over `Optional.ofNullable(A).orElse(B)` - prefer `requireNonNullElseGet(A, () -> B)` over `Optional.ofNullable(A).orElseGet(() -> B)` Related issue: PicnicSupermarket#364
67c1519
to
8e36bd0
Compare
Looks good. No mutations were possible for these changes. |
requireNonNullElse(A, B)
over Optional alternative.RequireNonNullElse{,Get}
Refaster rules
Related issue: #364
Added Rules:
requireNonNullElse(A, B)
overOptional.ofNullable(A).orElse(B)
requireNonNullElseGet(A, () -> B)
overOptional.ofNullable(A).orElseGet(() -> B)
Wrote test inputs and outputs.
Potential problems:
In the tests, referenced
Optional
by it's fully qualified name to avoid breakingRefasterRulesTest
or leaving unused imports in the code. - this seems hacky but didn't came up with better solution.Neither the new or already existing rules are not dealing with potential NPE when the second argument can be or produce null. - this seems to be a larger or at least a separated issue.