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

Add PreferJavaUtilObjectsRequireNonNullElse recipe for Java 11 #301

Conversation

josephw
Copy link
Contributor

@josephw josephw commented Sep 21, 2023

What's changed?

This adds PreferJavaUtilObjectsRequireNonNullElse, a ChangeMethodName/ChangeMethodTargetToStatic combination to convert Guava's MoreObjects.firstNonNull to the Java 9 equivalent of Objects.requireNonNullElse.

What's your motivation?

I'm aiming to extend the set of Guava calls that are replaced by direct equivalents, and get that included in a bulk recipe.

Anything in particular you'd like reviewers to focus on?

This method only shows up in Java 9. I've taken the approach of adding a new NoGuavaJava11 recipe, in the style of the UpgradeToJavaX recipes, so this doesn't break Java 8 projects, or require users to nominate the individual recipe directly.

The method is generic; I found that it didn't match with "firstNonNull(Object, Object)", so I'm using "..".

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

As I need this recipe, my alternative is to keep it in a local ruleset.

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ IDEA auto-formatter on affected files

Add a conversion from MoreObjects.firstNonNull to Java 9's
Objects.requireNonNullElse, as a direct replacement.
Some Guava migrations are only available in Java versions later than
8. Add a recipe to identify them.
Add a test; keep the test recipe activation distinct for this test, so anything else
Java 11-specific isn't accidentally enabled in tests.
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks for adding an immediate PR.

@timtebeek timtebeek merged commit d447621 into openrewrite:main Sep 21, 2023
@timtebeek timtebeek added recipe Recipe requested test provided labels Sep 21, 2023
@timtebeek
Copy link
Contributor

You should be able to use our snapshot versions to try this locally; I expect we'll follow up with a release in the next week or two. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested test provided
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants