From 467c10fb0360f42393e59c840e7b07032576efb3 Mon Sep 17 00:00:00 2001 From: Laurens-W Date: Fri, 15 Sep 2023 13:48:41 +0200 Subject: [PATCH] Rewrite `Stream.collect(Collectors.toUnmodifiableList())` to `Stream.toList()` (#293) * Add recipe to rewrite java 11 `Stream.collect(Collectors.toUnmodifiableList())` and optionally `Stream.collect(Collectors.toList())` to java 16 `Stream.toList()` * Polish * Apply suggestions from code review --------- Co-authored-by: Westerlaken, H.L. (Laurens) Co-authored-by: Tim te Beek Co-authored-by: Tim te Beek --- .../util/ReplaceStreamCollectWithToList.java | 106 ++++++++++ .../META-INF/rewrite/java-util-apis.yml | 1 + .../ReplaceStreamCollectWithToListTest.java | 183 ++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToList.java create mode 100644 src/test/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToListTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToList.java b/src/main/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToList.java new file mode 100644 index 0000000000..510c5db3d8 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToList.java @@ -0,0 +1,106 @@ +/* + * Copyright 2023 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate.util; + +import lombok.RequiredArgsConstructor; +import lombok.Value; +import org.openrewrite.*; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesJavaVersion; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; + +import java.time.Duration; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +@Value +public class ReplaceStreamCollectWithToList extends Recipe { + + private static final MethodMatcher STREAM_COLLECT = new MethodMatcher("java.util.stream.Stream collect(java.util.stream.Collector)"); + private static final MethodMatcher COLLECT_TO_UNMODIFIABLE_LIST = new MethodMatcher("java.util.stream.Collectors toUnmodifiableList()"); + private static final MethodMatcher COLLECT_TO_LIST = new MethodMatcher("java.util.stream.Collectors toList()"); + + @Option(displayName = "Convert mutable `Collectors.toList()` to immutable", + description = "Also replace `Stream.collect(Collectors.toList())` with `Stream.toList()`. " + + "*BEWARE*: Attempts to modify the returned list, result in an `UnsupportedOperationException`!", + required = false) + @Nullable + Boolean convertToList; + + @Override + public String getDisplayName() { + return "Replace `Stream.collect(Collectors.toUnmodifiableList())` with `Stream.toList()`"; + } + + @Override + public String getDescription() { + return "Replace `Stream.collect(Collectors.toUnmodifiableList())` with Java 16+ `Stream.toList()`. " + + "Also replaces `Stream.collect(Collectors.toList())` if `convertToList` is set to `true`."; + } + + @Override + public Duration getEstimatedEffortPerOccurrence() { + return Duration.ofMinutes(1); + } + + @Override + public Set getTags() { + return new HashSet<>(Collections.singletonList("RSPEC-6204")); + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check( + Preconditions.and( + new UsesJavaVersion<>(16), + new UsesMethod<>(STREAM_COLLECT), + Preconditions.or( + new UsesMethod<>(COLLECT_TO_UNMODIFIABLE_LIST), + new UsesMethod<>(COLLECT_TO_LIST)) + ), + new ReplaceCollectorToListVisitor(Boolean.TRUE.equals(convertToList))); + } + + @RequiredArgsConstructor + private static final class ReplaceCollectorToListVisitor extends JavaIsoVisitor { + private static final JavaTemplate template = JavaTemplate + .builder("#{any(java.util.stream.Stream)}.toList()") + .build(); + private final boolean convertToList; + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation result = super.visitMethodInvocation(method, ctx); + if (!STREAM_COLLECT.matches(method)) { + return result; + } + Expression command = method.getArguments().get(0); + if (COLLECT_TO_UNMODIFIABLE_LIST.matches(command) + || convertToList && COLLECT_TO_LIST.matches(command)) { + maybeRemoveImport("java.util.stream.Collectors"); + J.MethodInvocation toList = template.apply(updateCursor(result), result.getCoordinates().replace(), result.getSelect()); + return toList.getPadding().withSelect(result.getPadding().getSelect()); + } + return result; + } + } +} diff --git a/src/main/resources/META-INF/rewrite/java-util-apis.yml b/src/main/resources/META-INF/rewrite/java-util-apis.yml index d251bc8125..2fce66f24d 100644 --- a/src/main/resources/META-INF/rewrite/java-util-apis.yml +++ b/src/main/resources/META-INF/rewrite/java-util-apis.yml @@ -24,3 +24,4 @@ recipeList: - org.openrewrite.java.migrate.util.MigrateCollectionsSingletonSet - org.openrewrite.java.migrate.util.MigrateCollectionsUnmodifiableList - org.openrewrite.java.migrate.util.MigrateCollectionsUnmodifiableSet + - org.openrewrite.java.migrate.util.ReplaceStreamCollectWithToList diff --git a/src/test/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToListTest.java b/src/test/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToListTest.java new file mode 100644 index 0000000000..92e2a5aac2 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/util/ReplaceStreamCollectWithToListTest.java @@ -0,0 +1,183 @@ +/* + * Copyright 2023 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.migrate.util; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.java.Assertions.javaVersion; + +class ReplaceStreamCollectWithToListTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .recipe(new ReplaceStreamCollectWithToList(false)) + .allSources(s -> s.markers(javaVersion(17))); + } + + @Test + @DocumentExample + void replacesToUnmodifiableList() { + rewriteRun( + //language=java + java( + """ + import java.util.stream.Collectors; + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream.collect(Collectors.toUnmodifiableList()); + } + } + """, + """ + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream.toList(); + } + } + """ + ) + ); + } + + @Test + void doesNotReplaceToListByDefault() { + rewriteRun( + //language=java + java( + """ + import java.util.stream.Collectors; + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream.collect(Collectors.toList()); + } + } + """ + ) + ); + } + + @Test + void doesReplaceToListWhenFlagSetToTrue() { + rewriteRun( + recipeSpec -> recipeSpec.recipe(new ReplaceStreamCollectWithToList(true)), + //language=java + java( + """ + import java.util.stream.Collectors; + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream.collect(Collectors.toList()); + } + } + """, + """ + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream.toList(); + } + } + """ + ) + ); + } + + @Test + void retainWhitespace() { + rewriteRun( + //language=java + java( + """ + import java.util.stream.Collectors; + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream + .collect(Collectors.toUnmodifiableList()); + } + } + """, + """ + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream + .toList(); + } + } + """ + ) + ); + } + + @Test + void retainComment() { + rewriteRun( + //language=java + java( + """ + import java.util.stream.Collectors; + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream + // Convert to list + .collect(Collectors.toUnmodifiableList()); + } + } + """, + """ + import java.util.stream.Stream; + import java.util.List; + + class Example { + List test(Stream stream) { + return stream + // Convert to list + .toList(); + } + } + """ + ) + ); + } + +}