-
Notifications
You must be signed in to change notification settings - Fork 88
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
Add Objects.requireNonNullElse/ElseGet
support to AnnotateNullableParameters
#743
Conversation
The recipe now recognizes Objects.requireNonNull calls as null-checking operations and will annotate parameters passed to requireNonNull with @nullable. This handles both standalone calls and assignment statements using requireNonNull. - Added Objects.requireNonNull to the list of null-checking method matchers - Added visitMethodInvocation to handle standalone requireNonNull calls - Added tests for single and multiple parameter cases with requireNonNull
Not sure I agree with Claude here; when using |
Objects.requireNonNull throws an exception if the argument is null, enforcing non-null parameters. In contrast, requireNonNullElse and requireNonNullElseGet provide default values for null arguments, indicating they accept nullable parameters. - Replaced Objects.requireNonNull with requireNonNullElse and requireNonNullElseGet - These methods imply the parameter can be null since they provide fallback values - Updated all tests to use the new methods that actually indicate nullable parameters
Objects.requireNonNullElse/ElseGet
support to AnnotateNullableParameters
@stefanodallapalma this seems right up your alley ; any suggestions or concerns with this addition? |
Added Optional.ofNullable to the list of null-checking methods in the AnnotateNullableParameters recipe. When a method parameter is passed to Optional.ofNullable(), it will now be annotated with @nullable to indicate that the parameter can accept null values. This improves the recipe's ability to identify nullable parameters, as Optional.ofNullable is specifically designed to handle potentially null values. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
@timtebeek I had a similar concern here.
|
Thanks yes |
Oh yeah, thanks. The current diff looks good to me. I only left a minor comment |
} | ||
public Optional<String> conditionalWrap(@Nullable String data) { | ||
if (data != null && data.length() > 5) { |
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.
Summary
AnnotateNullableParameters
recipe to recognizeObjects.requireNonNullElse()
andObjects.requireNonNullElseGet()
calls as indicators of nullable parametersWhy this change?
Objects.requireNonNull()
throws an exception if the argument is null, enforcing non-null parametersObjects.requireNonNullElse()
andObjects.requireNonNullElseGet()
provide default values for null arguments, indicating the parameters can be null@Nullable
Changes
Objects.requireNonNullElse(..)
andObjects.requireNonNullElseGet(..)
to the list of null-checking method matchersvisitMethodInvocation
to handle standalone calls (not just those in if conditions)Test plan
Objects.requireNonNullElse
with default valuesObjects.requireNonNullElseGet
with supplier functions🤖 Generated with Claude Code