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 recipe to rewrite java 11 `Stream.collect(Collectors.toUnmodifiab… #167

Conversation

Laurens-W
Copy link
Contributor

@Laurens-W Laurens-W commented Sep 15, 2023

What's changed?

Adding a recipe that rewrites Java 11 Stream.collect(Collectors.toUnmodifiableList()) to Java 16 Stream.toList() with an optional toggle to include Stream.collect(Collectors.toList()) so no harm is done initially.

What's your motivation?

I asked on slack if there was an opposing recipe to replacestreamtolistwithcollect, we came to the conclusion that it does not exist but does seem justifiable to have.

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

Anyone you would like to review specifically?

@timtebeek @joanvr

Have you considered any alternatives or workarounds?

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

…leList())` and optionally `Stream.collect(Collectors.toList())` to java 16 `Stream.toList()`
@timtebeek
Copy link
Contributor

Hi @Laurens-W ! thanks for opening this pull request! :) Fun to see you get involved here.
I do wonder if we should perhaps target this recipe for inclusion here instead:
https://github.com/openrewrite/rewrite-migrate-java/blob/e8c30122672782861094ef366d495d7b8d35c3ec/src/main/java/org/openrewrite/java/migrate/util
There's a few more collection related recipes there, and it ties in with only upgrading when on a particular Java version or above. What do you think?

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.and(new UsesJavaVersion<>(16),
new UsesMethod<>(STREAM_COLLECT_TO_LIST)), new JavaVisitor<ExecutionContext>() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we can add additional preconditions for using either COLLECTORS_TO_LIST or COLLECTORS_TO_UNMODIFIABLE_LIST.

Comment on lines 44 to 48
@Option(displayName = "Should the recipe also apply to mutable toList?",
description = "Also replace Java 11 `Stream.collect(Collectors.toList())` with Java 16 `Stream.toList()`.",
required = false)
@Nullable
Boolean includeMutable;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if we sufficiently explain the risk here; maybe we should state that there's a risk when converting from mutable to immutable collections, hence why it's an option only, and turned off by default.

…hStreamToListTest.java

Co-authored-by: Tim te Beek <timtebeek@gmail.com>
@timtebeek
Copy link
Contributor

Great work! Continued here: openrewrite/rewrite-migrate-java#293

@timtebeek timtebeek closed this Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants