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
+ * 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