Skip to content

Commit 45f0281

Browse files
committed
Always "cast" the Predicate.isEqual method
In some cases, the type can be inferred, but not always. To follow the “Do no harm” principle, it’s safer to always "cast" the `Predicate.isEqual` method explicitly. Before this commit, certain cases resulted in compilation errors (see the new test inlinedPredicatesEqualToWithStandardType, which reveals this problem).
1 parent 926d84d commit 45f0281

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
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;
2928

29+
import java.util.Objects;
3030
import java.util.Set;
3131

3232
import static java.util.Collections.singleton;
@@ -60,8 +60,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6060
maybeRemoveImport("com.google.common.base.Predicates");
6161
maybeAddImport("java.util.function.Predicate");
6262

63-
if (method.getMethodType().getParameterTypes().get(0) instanceof JavaType.Parameterized) {
64-
String typeString = method.getArguments().get(0).getType().toString();
63+
try {
64+
String typeString = Objects.requireNonNull(method.getArguments().get(0).getType()).toString();
6565
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
6666
.imports("java.util.function.Predicate")
6767
.build()
@@ -70,14 +70,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
7070
method.getArguments().get(0));
7171
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
7272
return genericMethod;
73+
} catch (NullPointerException e) {
74+
// Fallback if no type is found.
75+
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
76+
.imports("java.util.function.Predicate")
77+
.build()
78+
.apply(getCursor(),
79+
method.getCoordinates().replace(),
80+
method.getArguments().get(0));
7381
}
74-
// Fallback is not type is found.
75-
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
76-
.imports("java.util.function.Predicate")
77-
.build()
78-
.apply(getCursor(),
79-
method.getCoordinates().replace(),
80-
method.getArguments().get(0));
8182
}
8283
return super.visitMethodInvocation(method, ctx);
8384
}

src/test/java/org/openrewrite/java/migrate/guava/NoGuavaPredicatesEqualToTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static Predicate<String> isHelloPredicate() {
5454
5555
class A {
5656
public static Predicate<String> isHelloPredicate() {
57-
return Predicate.isEqual("hello");
57+
return Predicate.<String>isEqual("hello");
5858
}
5959
}
6060
"""
@@ -64,7 +64,7 @@ public static Predicate<String> isHelloPredicate() {
6464

6565
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/903")
6666
@Test
67-
void inlinedPredicatesEqualToToPredicateIsEqual() {
67+
void inlinedPredicatesEqualToWithParameterizedType() {
6868
rewriteRun(
6969
//language=java
7070
java(
@@ -93,4 +93,40 @@ public static void test(Collection<String> aCollection, Predicate<Collection<Str
9393
)
9494
);
9595
}
96+
97+
@Test
98+
void inlinedPredicatesEqualToWithStandardType() {
99+
rewriteRun(
100+
//language=java
101+
java(
102+
"""
103+
import com.google.common.base.Predicates;
104+
import com.google.common.base.Predicate;
105+
106+
class Test {
107+
static Predicate<String> getMaxLengthPredicate() {
108+
return s -> s.length() < 10;
109+
}
110+
111+
public static void test() {
112+
Predicate<String> combined = Predicates.and(Predicates.equalTo("MyTest"), getMaxLengthPredicate());
113+
}
114+
}
115+
""",
116+
"""
117+
import java.util.function.Predicate;
118+
119+
class Test {
120+
static Predicate<String> getMaxLengthPredicate() {
121+
return s -> s.length() < 10;
122+
}
123+
124+
public static void test() {
125+
Predicate<String> combined = Predicate.<String>isEqual("MyTest").and(getMaxLengthPredicate());
126+
}
127+
}
128+
"""
129+
)
130+
);
131+
}
96132
}

0 commit comments

Comments
 (0)