From 942e67c6d2b5840af38c319fca341862168d152c Mon Sep 17 00:00:00 2001 From: Laurent Redor Date: Fri, 24 Oct 2025 17:54:53 +0200 Subject: [PATCH 1/2] Convert Guava `Collections2.filter` --- .../guava/NoGuavaCollections2Filter.java | 74 ++++++++++++++++++ .../resources/META-INF/rewrite/no-guava.yml | 1 + .../guava/NoGuavaCollections2FilterTest.java | 76 +++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java create mode 100644 src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java new file mode 100644 index 0000000000..8cf0fd6533 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java @@ -0,0 +1,74 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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.guava; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.J; + +import java.util.Set; + +import static java.util.Collections.singleton; + +public class NoGuavaCollections2Filter extends Recipe { + private static final MethodMatcher COLLECTIONS2_FILTER = new MethodMatcher("com.google.common.collect.Collections2 filter(java.util.Collection, com.google.common.base.Predicate)"); + + @Override + public String getDisplayName() { + return "Prefer `Collection.stream().filter(Predicate)`"; + } + + @Override + public String getDescription() { + return "Prefer `Collection.stream().filter(Predicate)` over `Collections2.filter(Collection, Predicate)`."; + } + + @Override + public Set getTags() { + return singleton("guava"); + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check( + new UsesMethod<>("com.google.common.collect.Collections2 filter(java.util.Collection, com.google.common.base.Predicate)"), + new JavaIsoVisitor() { + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (COLLECTIONS2_FILTER.matches(method)) { + maybeRemoveImport("com.google.common.base.Predicate"); + maybeRemoveImport("com.google.common.collect.Collections2"); + maybeAddImport("java.util.function.Predicate"); + + return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).toList()") + .build() + .apply(getCursor(), + method.getCoordinates().replace(), + method.getArguments().get(0), + method.getArguments().get(1)); + } + return super.visitMethodInvocation(method, ctx); + } + } + ); + } +} diff --git a/src/main/resources/META-INF/rewrite/no-guava.yml b/src/main/resources/META-INF/rewrite/no-guava.yml index 3b01ea22e2..d6f5ef911c 100644 --- a/src/main/resources/META-INF/rewrite/no-guava.yml +++ b/src/main/resources/META-INF/rewrite/no-guava.yml @@ -27,6 +27,7 @@ tags: recipeList: - org.openrewrite.java.migrate.guava.NoGuavaJava11 - org.openrewrite.java.migrate.guava.NoGuavaJava21 + - org.openrewrite.java.migrate.guava.NoGuavaCollections2Filter - org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir - org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor - org.openrewrite.java.migrate.guava.NoGuavaInlineMeMethods diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java new file mode 100644 index 0000000000..81bf1c09e5 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Moderne Source Available License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://docs.moderne.io/licensing/moderne-source-available-license + *

+ * 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.guava; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class NoGuavaCollections2FilterTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .recipe(new NoGuavaCollections2Filter()) + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava")); + } + + @DocumentExample + @Test + void replaceSetsFilter() { + //language=java + rewriteRun( + java( + """ + import java.util.ArrayList; + import java.util.Collection; + import java.util.Objects; + + import com.google.common.base.Predicate; + import com.google.common.collect.Collections2; + + class Test { + public static Collection test() { + Collection collection = new ArrayList<>(); + Predicate isNotNull = Objects::nonNull; + return Collections2.filter(collection, isNotNull); + } + } + """, + """ + import java.util.ArrayList; + import java.util.Collection; + import java.util.Objects; + + import com.google.common.base.Predicate; + + class Test { + public static Collection test() { + Collection collection = new ArrayList<>(); + Predicate isNotNull = Objects::nonNull; + return collection.stream().filter(isNotNull).toList(); + } + } + """ + ) + ); + } +} From 07afa11d4330820cb25dbf56a7bf0c10013dab8d Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 24 Oct 2025 18:40:47 +0200 Subject: [PATCH 2/2] Minor polish --- .../migrate/guava/NoGuavaCollections2Filter.java | 2 +- .../guava/NoGuavaCollections2FilterTest.java | 16 ++++------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java index 8cf0fd6533..819b79f35e 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2Filter.java @@ -50,7 +50,7 @@ public Set getTags() { @Override public TreeVisitor getVisitor() { return Preconditions.check( - new UsesMethod<>("com.google.common.collect.Collections2 filter(java.util.Collection, com.google.common.base.Predicate)"), + new UsesMethod<>(COLLECTIONS2_FILTER), new JavaIsoVisitor() { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java index 81bf1c09e5..92ca007faa 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaCollections2FilterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2025 the original author or authors. *

* Licensed under the Moderne Source Available License (the "License"); * you may not use this file except in compliance with the License. @@ -35,37 +35,29 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test - void replaceSetsFilter() { + void replaceCollections2Filter() { //language=java rewriteRun( java( """ - import java.util.ArrayList; import java.util.Collection; - import java.util.Objects; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; class Test { - public static Collection test() { - Collection collection = new ArrayList<>(); - Predicate isNotNull = Objects::nonNull; + Collection test(Collection collection, Predicate isNotNull) { return Collections2.filter(collection, isNotNull); } } """, """ - import java.util.ArrayList; import java.util.Collection; - import java.util.Objects; import com.google.common.base.Predicate; class Test { - public static Collection test() { - Collection collection = new ArrayList<>(); - Predicate isNotNull = Objects::nonNull; + Collection test(Collection collection, Predicate isNotNull) { return collection.stream().filter(isNotNull).toList(); } }