diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOf.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOf.java new file mode 100644 index 0000000000..54b48755b7 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOf.java @@ -0,0 +1,83 @@ +/* + * 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. + * 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.*; +import org.openrewrite.java.JavaVisitor; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.*; +import org.openrewrite.marker.Markers; + +import java.util.Set; + +import static java.util.Collections.emptyList; +import static java.util.Collections.singleton; + +public class NoGuavaPredicatesInstanceOf extends Recipe { + private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher("com.google.common.base.Predicates instanceOf(..)"); + + @Override + public String getDisplayName() { + return "Prefer `A.class::isInstance`"; + } + + @Override + public String getDescription() { + return "Prefer `A.class::isInstance` over `Predicates.instanceOf(A.class)`."; + } + + @Override + public Set getTags() { + return singleton("guava"); + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check( + new UsesMethod<>(PREDICATES_INSTANCE_OF), + new JavaVisitor() { + @Override + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + if (PREDICATES_INSTANCE_OF.matches(method)) { + maybeRemoveImport("com.google.common.base.Predicates"); + Expression clazz = method.getArguments().get(0); + // XXX `JavaTemplate.builder("#{any()}::isInstance")` failed here + // TODO Add type information for reference and identifier + return new J.MemberReference( + Tree.randomId(), + method.getPrefix(), + Markers.EMPTY, + JRightPadded.build(clazz), + null, + JLeftPadded.build(new J.Identifier( + Tree.randomId(), + Space.EMPTY, + Markers.EMPTY, + emptyList(), + "isInstance", + null, + null)), + null, + null, + null); + } + 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 3c5239af2a..45852f3633 100644 --- a/src/main/resources/META-INF/rewrite/no-guava.yml +++ b/src/main/resources/META-INF/rewrite/no-guava.yml @@ -41,6 +41,7 @@ recipeList: - org.openrewrite.java.migrate.guava.NoGuavaMapsNewTreeMap - org.openrewrite.java.migrate.guava.NoGuavaPredicatesAndOr - org.openrewrite.java.migrate.guava.NoGuavaPredicatesEqualTo + - org.openrewrite.java.migrate.guava.NoGuavaPredicatesInstanceOf - org.openrewrite.java.migrate.guava.NoGuavaPrimitiveAsList - org.openrewrite.java.migrate.guava.NoGuavaRefasterRecipes - org.openrewrite.java.migrate.guava.NoGuavaMapsNewHashMap diff --git a/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOfTest.java b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOfTest.java new file mode 100644 index 0000000000..c8b94bb8b2 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesInstanceOfTest.java @@ -0,0 +1,70 @@ +/* + * 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. + * 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 org.openrewrite.test.TypeValidation; + +import static org.openrewrite.java.Assertions.java; + +class NoGuavaPredicatesInstanceOfTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec + .recipe(new NoGuavaPredicatesInstanceOf()) + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "guava")); + } + + @DocumentExample + @Test + void predicatesEqualToToPredicateIsEqual() { + rewriteRun( + // XXX Pending JavaTemplate support for MemberReference + spec -> spec.afterTypeValidationOptions(TypeValidation.none()), + //language=java + java( + """ + import java.util.Collection; + + import com.google.common.base.Predicates; + import com.google.common.collect.Iterables; + + class Test { + boolean test(Collection collection) { + return Iterables.all(collection, Predicates.instanceOf(String.class)); + } + } + """, + """ + import java.util.Collection; + + import com.google.common.collect.Iterables; + + class Test { + boolean test(Collection collection) { + return Iterables.all(collection, String.class::isInstance); + } + } + """ + ) + ); + } +}