Skip to content

Conversation

e5LA
Copy link
Contributor

@e5LA e5LA commented Sep 13, 2025

What's changed?

Added recipe to migrate System.out.print() and System.out.println() to IO.print() and IO.println()

What's your motivation?

Closes #846.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Sep 13, 2025
@e5LA
Copy link
Contributor Author

e5LA commented Sep 13, 2025

While preparing the PR, I realized we might also want to handle import static java.lang.System.out; and out.println(...).
I’m not sure how common this pattern is - what do you think?

I can look into it and we could add it in a follow-up PR

@timtebeek
Copy link
Member

Great to see thanks! I think we can support that without extra effort if we switch to using MethodMatcher :)

@e5LA
Copy link
Contributor Author

e5LA commented Sep 13, 2025

Good point, thanks @timtebeek , I'll update the code.

@e5LA
Copy link
Contributor Author

e5LA commented Sep 14, 2025

@timtebeek - I just realized though, that print() and println() aren't methods on java.lang.System but on out. And out is a field of type java.io.PrintStream.
If we use the matcher against PrintStream we’ll end up matching all println calls on any PrintStream instance, not just on System.out..

Did I miss anything, or do you see a cleaner way to restrict it just to System.out?

@timtebeek timtebeek changed the title Add recipe to migrate System.out.print/println to IO.print/println Add recipe to migrate System.out.print/println to IO.print/println Sep 16, 2025
@timtebeek timtebeek added the recipe Recipe requested label Sep 16, 2025
@timtebeek timtebeek self-requested a review September 16, 2025 15:20
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Sep 16, 2025
Copy link
Member

@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.

Thanks a lot for the help here @e5LA ! I've added a few small changes, mostly to reduce duplication, support edge cases, improve performance and remove unused imports past migration. It's also now included in the Java 25 migration going forward. 🎉

@timtebeek timtebeek merged commit b566417 into openrewrite:main Sep 16, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Sep 16, 2025
@e5LA
Copy link
Contributor Author

e5LA commented Sep 16, 2025

Thanks for the improvements @timtebeek ! Always great to see yours solutions.
Glad it made it into the Java 25 migration.

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

Successfully merging this pull request may close these issues.

Replace System.out.print(ln) with IO.print(ln)
2 participants