diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAll.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAll.java new file mode 100644 index 0000000000..8c2f5fa1ba --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAll.java @@ -0,0 +1,75 @@ +/* + * 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.JavaTemplate; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TypeUtils; + +import java.util.Set; + +import static java.util.Collections.singleton; + +public class NoGuavaIterablesAll extends Recipe { + private static final MethodMatcher ITERABLES_ALL = new MethodMatcher("com.google.common.collect.Iterables all(java.lang.Iterable, com.google.common.base.Predicate)"); + + @Override + public String getDisplayName() { + return "Prefer `Collection.stream().allMatch(Predicate)`"; + } + + @Override + public String getDescription() { + return "Prefer `Collection.stream().allMatch(Predicate)` over `Iterables.all(Collection, Predicate)`."; + } + + @Override + public Set getTags() { + return singleton("guava"); + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check(new UsesMethod<>(ITERABLES_ALL), new JavaVisitor() { + @Override + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (ITERABLES_ALL.matches(method)) { + maybeRemoveImport("com.google.common.base.Predicate"); + maybeRemoveImport("com.google.common.collect.Iterables"); + maybeAddImport("java.util.function.Predicate"); + + if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { + return JavaTemplate.builder("#{any(java.util.Collection)}.stream().allMatch(#{any(java.util.function.Predicate)})") + .build() + .apply(getCursor(), + method.getCoordinates().replace(), + method.getArguments().get(0), + method.getArguments().get(1)); + } + return method; + } + 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 45852f3633..c8f05c8df5 100644 --- a/src/main/resources/META-INF/rewrite/no-guava.yml +++ b/src/main/resources/META-INF/rewrite/no-guava.yml @@ -31,6 +31,7 @@ recipeList: - org.openrewrite.java.migrate.guava.NoGuavaCreateTempDir - org.openrewrite.java.migrate.guava.NoGuavaDirectExecutor - org.openrewrite.java.migrate.guava.NoGuavaFunctionsCompose + - org.openrewrite.java.migrate.guava.NoGuavaIterablesAll - org.openrewrite.java.migrate.guava.NoGuavaIterablesAnyFilter - org.openrewrite.java.migrate.guava.NoGuavaIterablesTransform - org.openrewrite.java.migrate.guava.NoGuavaCollections2Transform diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAllTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAllTest.java new file mode 100644 index 0000000000..109601d040 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAllTest.java @@ -0,0 +1,89 @@ +/* + * 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 NoGuavaIterablesAllTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .recipe(new NoGuavaIterablesAll()) + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava")); + } + + @DocumentExample + @Test + void replaceIterablesAll() { + //language=java + rewriteRun( + java( + """ + import java.util.Collection; + + import com.google.common.base.Predicate; + import com.google.common.collect.Iterables; + + class Test { + boolean test(Collection collection, Predicate aPredicate) { + return Iterables.all(collection, aPredicate); + } + } + """, + """ + import java.util.Collection; + + import com.google.common.base.Predicate; + + class Test { + boolean test(Collection collection, Predicate aPredicate) { + return collection.stream().allMatch(aPredicate); + } + } + """ + ) + ); + } + + @Test + void iterablesAllWithIterable() { + //language=java + rewriteRun( + java( + """ + import java.lang.Iterable; + + import com.google.common.base.Predicate; + import com.google.common.collect.Iterables; + + class Test { + boolean test(Iterable iterable, Predicate aPredicate) { + return Iterables.all(iterable, aPredicate); + } + } + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaTest.java index bd8f22d0f3..0382ab3792 100644 --- a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaTest.java +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaTest.java @@ -128,4 +128,36 @@ static boolean isEqual(Object obj0, Object obj1) { ) ); } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/920") + @Test + void iterablesAllMigration() { + //language=java + rewriteRun( + java( + """ + import java.util.Collection; + + import com.google.common.base.Predicate; + import com.google.common.collect.Iterables; + + class Test { + boolean test(Collection collection, Predicate aPredicate) { + return Iterables.all(collection, aPredicate); + } + } + """, + """ + import java.util.Collection; + import java.util.function.Predicate; + + class Test { + boolean test(Collection collection, Predicate aPredicate) { + return collection.stream().allMatch(aPredicate); + } + } + """ + ) + ); + } }