Skip to content

Commit f9c0ad4

Browse files
lredortimtebeek
andauthored
Always add explicit type to the Predicate.isEqual method (#927)
* 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). * Add the recipe NoGuavaMapsNewLinkedHashMap to `no-guava.yml` It seems that the recipe NoGuavaMapsNewLinkedHashMap has never been added in `no-guava.yml`. * Don't use try/catch to handle null type --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 8f8a2f5 commit f9c0ad4

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openrewrite.java.tree.J;
2828
import org.openrewrite.java.tree.JavaType;
2929

30+
import java.util.Objects;
3031
import java.util.Set;
3132

3233
import static java.util.Collections.singleton;
@@ -60,24 +61,26 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6061
maybeRemoveImport("com.google.common.base.Predicates");
6162
maybeAddImport("java.util.function.Predicate");
6263

63-
if (method.getMethodType().getParameterTypes().get(0) instanceof JavaType.Parameterized) {
64-
String typeString = method.getArguments().get(0).getType().toString();
65-
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
64+
JavaType argumentType = method.getArguments().get(0).getType();
65+
if (argumentType == null) {
66+
// Fallback if no type is found.
67+
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
6668
.imports("java.util.function.Predicate")
6769
.build()
6870
.apply(getCursor(),
6971
method.getCoordinates().replace(),
7072
method.getArguments().get(0));
71-
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
72-
return genericMethod;
7373
}
74-
// Fallback is not type is found.
75-
return JavaTemplate.builder("Predicate.isEqual(#{any(java.lang.Object)})")
74+
75+
String typeString = Objects.requireNonNull(argumentType).toString();
76+
J.MethodInvocation genericMethod = JavaTemplate.builder("Predicate.<" + typeString + ">isEqual(#{any(java.lang.Object)})")
7677
.imports("java.util.function.Predicate")
7778
.build()
7879
.apply(getCursor(),
7980
method.getCoordinates().replace(),
8081
method.getArguments().get(0));
82+
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(genericMethod));
83+
return genericMethod;
8184
}
8285
return super.visitMethodInvocation(method, ctx);
8386
}

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)