|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.guava; |
| 17 | + |
| 18 | +import org.openrewrite.ExecutionContext; |
| 19 | +import org.openrewrite.Preconditions; |
| 20 | +import org.openrewrite.Recipe; |
| 21 | +import org.openrewrite.TreeVisitor; |
| 22 | +import org.openrewrite.java.JavaIsoVisitor; |
| 23 | +import org.openrewrite.java.JavaTemplate; |
| 24 | +import org.openrewrite.java.MethodMatcher; |
| 25 | +import org.openrewrite.java.ShortenFullyQualifiedTypeReferences; |
| 26 | +import org.openrewrite.java.search.UsesMethod; |
| 27 | +import org.openrewrite.java.tree.J; |
| 28 | +import org.openrewrite.java.tree.JavaType; |
| 29 | + |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import static java.util.Collections.singleton; |
| 33 | + |
| 34 | +public class NoGuavaPredicatesInstanceOf extends Recipe { |
| 35 | + private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher("com.google.common.base.Predicates instanceOf(..)"); |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getDisplayName() { |
| 39 | + return "Prefer `ASpecificClass.class::isInstance`"; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getDescription() { |
| 44 | + return "Prefer `ASpecificClass.class::isInstance` over `Predicates.instanceOf(ASpecificClass.class)`."; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Set<String> getTags() { |
| 49 | + return singleton("guava"); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 54 | + return Preconditions.check( |
| 55 | + new UsesMethod<>(PREDICATES_INSTANCE_OF), |
| 56 | + new JavaIsoVisitor<ExecutionContext>() { |
| 57 | + @Override |
| 58 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 59 | + if (PREDICATES_INSTANCE_OF.matches(method)) { |
| 60 | + maybeRemoveImport("com.google.common.base.Predicates"); |
| 61 | + |
| 62 | + return JavaTemplate.builder("#{any()}::isInstance") |
| 63 | + .build() |
| 64 | + .apply(getCursor(), |
| 65 | + method.getCoordinates().replace(), |
| 66 | + method.getArguments().get(0)); |
| 67 | + } |
| 68 | + return super.visitMethodInvocation(method, ctx); |
| 69 | + } |
| 70 | + } |
| 71 | + ); |
| 72 | + } |
| 73 | +} |
0 commit comments