Skip to content

Commit be32de3

Browse files
committed
Use Expression.unwrap() in UsePredicateNot
1 parent a2ed24d commit be32de3

1 file changed

Lines changed: 6 additions & 21 deletions

File tree

src/main/java/org/openrewrite/java/migrate/util/UsePredicateNot.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.openrewrite.java.migrate.util;
1717

1818
import lombok.Getter;
19-
import org.jspecify.annotations.Nullable;
2019
import org.openrewrite.ExecutionContext;
2120
import org.openrewrite.Preconditions;
2221
import 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

Comments
 (0)