|
| 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.*; |
| 19 | +import org.openrewrite.java.JavaVisitor; |
| 20 | +import org.openrewrite.java.MethodMatcher; |
| 21 | +import org.openrewrite.java.search.UsesMethod; |
| 22 | +import org.openrewrite.java.tree.*; |
| 23 | +import org.openrewrite.marker.Markers; |
| 24 | + |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import static java.util.Collections.emptyList; |
| 28 | +import static java.util.Collections.singleton; |
| 29 | + |
| 30 | +public class NoGuavaPredicatesInstanceOf extends Recipe { |
| 31 | + private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher("com.google.common.base.Predicates instanceOf(..)"); |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getDisplayName() { |
| 35 | + return "Prefer `A.class::isInstance`"; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getDescription() { |
| 40 | + return "Prefer `A.class::isInstance` over `Predicates.instanceOf(A.class)`."; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Set<String> getTags() { |
| 45 | + return singleton("guava"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 50 | + return Preconditions.check( |
| 51 | + new UsesMethod<>(PREDICATES_INSTANCE_OF), |
| 52 | + new JavaVisitor<ExecutionContext>() { |
| 53 | + @Override |
| 54 | + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 55 | + if (PREDICATES_INSTANCE_OF.matches(method)) { |
| 56 | + maybeRemoveImport("com.google.common.base.Predicates"); |
| 57 | + Expression clazz = method.getArguments().get(0); |
| 58 | + // XXX `JavaTemplate.builder("#{any()}::isInstance")` failed here |
| 59 | + // TODO Add type information for reference and identifier |
| 60 | + return new J.MemberReference( |
| 61 | + Tree.randomId(), |
| 62 | + method.getPrefix(), |
| 63 | + Markers.EMPTY, |
| 64 | + JRightPadded.build(clazz), |
| 65 | + null, |
| 66 | + JLeftPadded.build(new J.Identifier( |
| 67 | + Tree.randomId(), |
| 68 | + Space.EMPTY, |
| 69 | + Markers.EMPTY, |
| 70 | + emptyList(), |
| 71 | + "isInstance", |
| 72 | + null, |
| 73 | + null)), |
| 74 | + null, |
| 75 | + null, |
| 76 | + null); |
| 77 | + } |
| 78 | + return super.visitMethodInvocation(method, ctx); |
| 79 | + } |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments