1515 */
1616package org .openrewrite .java .migrate .guava ;
1717
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 ;
18+ import org .openrewrite .*;
19+ import org .openrewrite .java .JavaVisitor ;
2420import org .openrewrite .java .MethodMatcher ;
2521import org .openrewrite .java .search .UsesMethod ;
2622import org .openrewrite .java .tree .J ;
23+ import org .openrewrite .java .tree .JLeftPadded ;
24+ import org .openrewrite .java .tree .JRightPadded ;
25+ import org .openrewrite .java .tree .Space ;
26+ import org .openrewrite .marker .Markers ;
27+
2728import java .util .Set ;
2829
30+ import static java .util .Collections .emptyList ;
2931import static java .util .Collections .singleton ;
3032
3133public class NoGuavaPredicatesInstanceOf extends Recipe {
3234 private static final MethodMatcher PREDICATES_INSTANCE_OF = new MethodMatcher ("com.google.common.base.Predicates instanceOf(..)" );
3335
3436 @ Override
3537 public String getDisplayName () {
36- return "Prefer `ASpecificClass .class::isInstance`" ;
38+ return "Prefer `A .class::isInstance`" ;
3739 }
3840
3941 @ Override
4042 public String getDescription () {
41- return "Prefer `ASpecificClass .class::isInstance` over `Predicates.instanceOf(ASpecificClass .class)`." ;
43+ return "Prefer `A .class::isInstance` over `Predicates.instanceOf(A .class)`." ;
4244 }
4345
4446 @ Override
@@ -50,17 +52,30 @@ public Set<String> getTags() {
5052 public TreeVisitor <?, ExecutionContext > getVisitor () {
5153 return Preconditions .check (
5254 new UsesMethod <>(PREDICATES_INSTANCE_OF ),
53- new JavaIsoVisitor <ExecutionContext >() {
55+ new JavaVisitor <ExecutionContext >() {
5456 @ Override
55- public J . MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
57+ public J visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
5658 if (PREDICATES_INSTANCE_OF .matches (method )) {
5759 maybeRemoveImport ("com.google.common.base.Predicates" );
58-
59- return JavaTemplate .builder ("#{any()}::isInstance" )
60- .build ()
61- .apply (getCursor (),
62- method .getCoordinates ().replace (),
63- method .getArguments ().get (0 ));
60+ // XXX `JavaTemplate.builder("#{any()}::isInstance")` failed here
61+ // TODO Add type information for reference and identifier
62+ return new J .MemberReference (
63+ Tree .randomId (),
64+ method .getPrefix (),
65+ Markers .EMPTY ,
66+ JRightPadded .build (method .getArguments ().get (0 )),
67+ null ,
68+ JLeftPadded .build (new J .Identifier (
69+ Tree .randomId (),
70+ Space .EMPTY ,
71+ Markers .EMPTY ,
72+ emptyList (),
73+ "isInstance" ,
74+ null ,
75+ null )),
76+ null ,
77+ null ,
78+ null );
6479 }
6580 return super .visitMethodInvocation (method , ctx );
6681 }
0 commit comments