-
Notifications
You must be signed in to change notification settings - Fork 61
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
Add recipe to rewrite java 11 `Stream.collect(Collectors.toUnmodifiab… #167
Conversation
…leList())` and optionally `Stream.collect(Collectors.toList())` to java 16 `Stream.toList()`
Hi @Laurens-W ! thanks for opening this pull request! :) Fun to see you get involved here. |
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(Preconditions.and(new UsesJavaVersion<>(16), | ||
new UsesMethod<>(STREAM_COLLECT_TO_LIST)), new JavaVisitor<ExecutionContext>() { |
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.
Perhaps we can add additional preconditions for using either COLLECTORS_TO_LIST
or COLLECTORS_TO_UNMODIFIABLE_LIST
.
@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; |
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.
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.
src/test/java/org/openrewrite/staticanalysis/ReplaceCollectWithStreamToListTest.java
Outdated
Show resolved
Hide resolved
…hStreamToListTest.java Co-authored-by: Tim te Beek <timtebeek@gmail.com>
src/main/java/org/openrewrite/staticanalysis/ReplaceCollectWithStreamToList.java
Show resolved
Hide resolved
…hStreamToList.java Co-authored-by: Tim te Beek <timtebeek@gmail.com>
Great work! Continued here: openrewrite/rewrite-migrate-java#293 |
What's changed?
Adding a recipe that rewrites Java 11
Stream.collect(Collectors.toUnmodifiableList())
to Java 16Stream.toList()
with an optional toggle to includeStream.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
./gradlew licenseFormat