-
Notifications
You must be signed in to change notification settings - Fork 92
Add Objects.requireNonNullElse/ElseGet support to AnnotateNullableParameters
#743
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
Merged
timtebeek
merged 6 commits into
main
from
add-requirenonnull-to-annotate-nullable-params
Sep 27, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
49f6cb1
Add support for Objects.requireNonNull in AnnotateNullableParameters
timtebeek d91b57e
Replace requireNonNull with requireNonNullElse/ElseGet support
timtebeek 6885955
Merge tests
timtebeek 0bf363c
Only check the first argument, not any fallback
timtebeek 967f4e8
Add Optional.ofNullable support to AnnotateNullableParameters
timtebeek b9fcc55
Adjust test as suggested
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,6 +378,122 @@ boolean bar(Object obj) { | |
| @Nested | ||
| class KnownNullCheckers { | ||
|
|
||
| @Test | ||
| void objectsRequireNonNullElse() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.util.Objects; | ||
|
|
||
| class PersonBuilder { | ||
| private String name; | ||
| private String email; | ||
|
|
||
| public PersonBuilder setName(String name) { | ||
| this.name = Objects.requireNonNullElse(name, "Unknown"); | ||
| return this; | ||
| } | ||
|
|
||
| public PersonBuilder setNameWithFallback(String name, String fallback) { | ||
| this.name = Objects.requireNonNullElse(name, fallback); | ||
| return this; | ||
| } | ||
|
|
||
| public PersonBuilder setEmail(String email) { | ||
| this.email = Objects.requireNonNullElseGet(email, () -> "[email protected]"); | ||
| return this; | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| class PersonBuilder { | ||
| private String name; | ||
| private String email; | ||
|
|
||
| public PersonBuilder setName(@Nullable String name) { | ||
| this.name = Objects.requireNonNullElse(name, "Unknown"); | ||
| return this; | ||
| } | ||
|
|
||
| public PersonBuilder setNameWithFallback(@Nullable String name, String fallback) { | ||
| this.name = Objects.requireNonNullElse(name, fallback); | ||
| return this; | ||
| } | ||
|
|
||
| public PersonBuilder setEmail(@Nullable String email) { | ||
| this.email = Objects.requireNonNullElseGet(email, () -> "[email protected]"); | ||
| return this; | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void optionalOfNullable() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.util.Optional; | ||
|
|
||
| public class PersonService { | ||
|
|
||
| public Optional<String> processValue(String value) { | ||
| return Optional.ofNullable(value) | ||
| .filter(v -> !v.isEmpty()) | ||
| .map(String::toUpperCase); | ||
| } | ||
|
|
||
| public Optional<String> wrapValue(String input) { | ||
| // Direct usage of parameter in Optional.ofNullable | ||
| return Optional.ofNullable(input); | ||
| } | ||
|
|
||
| public Optional<String> conditionalWrap(String data) { | ||
| if (data != null && data.length() > 5) { | ||
| return Optional.of(data); | ||
| } | ||
| return Optional.ofNullable(data); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class PersonService { | ||
|
|
||
| public Optional<String> processValue(@Nullable String value) { | ||
| return Optional.ofNullable(value) | ||
| .filter(v -> !v.isEmpty()) | ||
| .map(String::toUpperCase); | ||
| } | ||
|
|
||
| public Optional<String> wrapValue(@Nullable String input) { | ||
| // Direct usage of parameter in Optional.ofNullable | ||
| return Optional.ofNullable(input); | ||
| } | ||
|
|
||
| public Optional<String> conditionalWrap(@Nullable String data) { | ||
| if (data != null && data.length() > 5) { | ||
| return Optional.of(data); | ||
| } | ||
| return Optional.ofNullable(data); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @CsvSource({ | ||
| "java.util.Objects, Objects.nonNull", | ||
| "org.apache.commons.lang3.StringUtils, StringUtils.isNotBlank", | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I find this test case a little redundant due to
data != null. I'd rather test something like the following:Optional.ofNullable(data).ifPresent(it -> ...);What do you think?
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.
Yes makes sense, thanks! Will merge with that change.