Skip to content

Commit ed9ea6b

Browse files
committed
Don't use try/catch to handle null type
1 parent 938454b commit ed9ea6b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesEqualTo.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences;
2626
import org.openrewrite.java.search.UsesMethod;
2727
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.JavaType;
2829

2930
import java.util.Objects;
3031
import java.util.Set;
@@ -60,17 +61,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6061
maybeRemoveImport("com.google.common.base.Predicates");
6162
maybeAddImport("java.util.function.Predicate");
6263

63-
try {
64-
String typeString = Objects.requireNonNull(method.getArguments().get(0).getType()).toString();
65-
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
66-
.imports("java.util.function.Predicate")
67-
.build()
68-
.apply(getCursor(),
69-
method.getCoordinates().replace(),
70-
method.getArguments().get(0));
71-
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
72-
return genericMethod;
73-
} catch (NullPointerException e) {
64+
JavaType argumentType = method.getArguments().get(0).getType();
65+
if (argumentType == null) {
7466
// Fallback if no type is found.
7567
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
7668
.imports("java.util.function.Predicate")
@@ -79,6 +71,16 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
7971
method.getCoordinates().replace(),
8072
method.getArguments().get(0));
8173
}
74+
75+
String typeString = Objects.requireNonNull(argumentType).toString();
76+
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
77+
.imports("java.util.function.Predicate")
78+
.build()
79+
.apply(getCursor(),
80+
method.getCoordinates().replace(),
81+
method.getArguments().get(0));
82+
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
83+
return genericMethod;
8284
}
8385
return super.visitMethodInvocation(method, ctx);
8486
}

0 commit comments

Comments
 (0)