1616package org .openrewrite .java .migrate .util ;
1717
1818import lombok .Getter ;
19- import org .jspecify .annotations .Nullable ;
2019import org .openrewrite .ExecutionContext ;
2120import org .openrewrite .Preconditions ;
2221import org .openrewrite .Recipe ;
@@ -57,8 +56,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
5756 if (!PREDICATE_NEGATE .matches (m ) || m .getSelect () == null ) {
5857 return m ;
5958 }
60- J .TypeCast cast = asPredicateCast (m .getSelect ());
61- if (cast == null ) {
59+ Expression unwrapped = m .getSelect ().unwrap ();
60+ if (!(unwrapped instanceof J .TypeCast )) {
61+ return m ;
62+ }
63+ J .TypeCast cast = (J .TypeCast ) unwrapped ;
64+ if (!TypeUtils .isAssignableTo (PREDICATE_FQN , cast .getType ())) {
6265 return m ;
6366 }
6467 maybeAddImport (PREDICATE_FQN );
@@ -67,24 +70,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6770 .build ()
6871 .apply (getCursor (), m .getCoordinates ().replace (), cast .getExpression ());
6972 }
70-
71- private J .@ Nullable TypeCast asPredicateCast (Expression select ) {
72- Expression e = select ;
73- if (e instanceof J .Parentheses ) {
74- Object tree = ((J .Parentheses <?>) e ).getTree ();
75- if (tree instanceof Expression ) {
76- e = (Expression ) tree ;
77- }
78- }
79- if (!(e instanceof J .TypeCast )) {
80- return null ;
81- }
82- J .TypeCast cast = (J .TypeCast ) e ;
83- if (!TypeUtils .isAssignableTo (PREDICATE_FQN , cast .getType ())) {
84- return null ;
85- }
86- return cast ;
87- }
8873 }
8974 );
9075 }
0 commit comments