Skip to content

Conversation

timtebeek
Copy link
Member

@timtebeek timtebeek commented Sep 27, 2025

Summary

  • Enhanced AnnotateNullableParameters recipe to recognize Objects.requireNonNullElse() and Objects.requireNonNullElseGet() calls as indicators of nullable parameters
  • These methods provide default values when arguments are null, implying they accept nullable parameters

Why this change?

  • Objects.requireNonNull() throws an exception if the argument is null, enforcing non-null parameters
  • Objects.requireNonNullElse() and Objects.requireNonNullElseGet() provide default values for null arguments, indicating the parameters can be null
  • Parameters that use these fallback methods should be annotated with @Nullable

Changes

  • Added Objects.requireNonNullElse(..) and Objects.requireNonNullElseGet(..) to the list of null-checking method matchers
  • Implemented visitMethodInvocation to handle standalone calls (not just those in if conditions)
  • Added comprehensive test coverage for both methods

Test plan

  • Added test for Objects.requireNonNullElse with default values
  • Added test for Objects.requireNonNullElseGet with supplier functions
  • Added test for multiple parameters with mixed usage of both methods
  • All existing tests continue to pass

🤖 Generated with Claude Code

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
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Sep 27, 2025
@timtebeek
Copy link
Member Author

Not sure I agree with Claude here; when using requireNonNull I'd expect the variable to not be nullable, as we don't expect folks to rely on the NPE that gets thrown.

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
@timtebeek timtebeek changed the title Add Objects.requireNonNull support to AnnotateNullableParameters Add Objects.requireNonNullElse/ElseGet support to AnnotateNullableParameters Sep 27, 2025
@timtebeek timtebeek changed the title Add Objects.requireNonNullElse/ElseGet support to AnnotateNullableParameters Add Objects.requireNonNullElse/ElseGet support to AnnotateNullableParameters Sep 27, 2025
@timtebeek timtebeek self-assigned this Sep 27, 2025
@timtebeek timtebeek added the enhancement New feature or request label Sep 27, 2025
@timtebeek timtebeek marked this pull request as ready for review September 27, 2025 10:55
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Sep 27, 2025
@timtebeek
Copy link
Member Author

@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>
@stefanodallapalma
Copy link
Contributor

Not sure I agree with Claude here; when using requireNonNull I'd expect the variable to not be nullable, as we don't expect folks to rely on the NPE that gets thrown.

@timtebeek I had a similar concern here.

Objects.requireNonNullElse() and requireNonNullElseGet make sense. Now I realized we could support Optional.ofNullable() too

@timtebeek
Copy link
Member Author

Thanks yes requireNonNull is quite the opposite, but requireNonNullElse* and Optional.ofNullable do indicate nullable parameters I think, or at the very least defensive coding. From your comment it's not yet clear whether you'd agree with the changes made here for those specific methods (not including requireNonNull from an earlier iteration). Is the current diff ok to merge you think?

@stefanodallapalma
Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Member Author

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.

@timtebeek timtebeek merged commit 0bd9340 into main Sep 27, 2025
2 checks passed
@timtebeek timtebeek deleted the add-requirenonnull-to-annotate-nullable-params branch September 27, 2025 13:43
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Sep 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants