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..819b79f35e --- /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
+ * 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 replaceCollections2Filter() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import java.util.Collection;
+
+ import com.google.common.base.Predicate;
+ import com.google.common.collect.Collections2;
+
+ class Test {
+ Collection