Skip to content

Commit e464614

Browse files
committed
Avoid generic type issues by not making any changes just yet
- Fixes #903
1 parent 6f906af commit e464614

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ private J handlePredicatesMethod(J.MethodInvocation method, String operation) {
7878
// Build the chain: first.operation(second).operation(third)...
7979
Expression result = arguments.get(0);
8080

81+
// Avoid generic type issues by not making any changes just yet
82+
if (result instanceof J.MethodInvocation) {
83+
return method;
84+
}
85+
8186
// If the first argument is a method reference, wrap it with a cast
8287
if (result instanceof J.MemberReference && result.getType() != null) {
8388
String typeString = result.getType().toString().replace("com.google.common.base.", "");

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,29 @@ class Test {
237237
)
238238
);
239239
}
240+
241+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/903")
242+
@Test
243+
void doNotConvertPredicatesEqualToWhenUsedInMethodChain() {
244+
// Converting Predicates.equalTo() to Predicate.isEqual() breaks type inference when chained with .and()
245+
// The issue is that Predicate.isEqual() returns Predicate<? super T> which causes compilation errors
246+
// when chained with other methods
247+
rewriteRun(
248+
//language=java
249+
java(
250+
"""
251+
import com.google.common.base.Predicate;
252+
import com.google.common.base.Predicates;
253+
import java.util.Collection;
254+
255+
class A {
256+
public static Predicate<Collection<String>> combinedPredicate(Collection<String> aCollection) {
257+
Predicate<Collection<String>> anotherPredicate = c -> !c.isEmpty();
258+
return Predicates.and(Predicates.equalTo(aCollection), anotherPredicate);
259+
}
260+
}
261+
"""
262+
)
263+
);
264+
}
240265
}

0 commit comments

Comments
 (0)