Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ private J handlePredicatesMethod(J.MethodInvocation method, String operation) {
return method;
}

// If the first argument is a method reference, wrap it with a cast
if (result instanceof J.MemberReference && result.getType() != null) {
// If the first argument is a method reference or a lambda, wrap it with a cast
if ((result instanceof J.MemberReference || result instanceof J.Lambda) && result.getType() != null) {
String typeString = result.getType().toString().replace("com.google.common.base.", "");
result = JavaTemplate.apply("((" + typeString + ") #{any()})", getCursor(), method.getCoordinates().replace(), result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,38 @@ void replacePredicatesAndWithLambdas() {
import com.google.common.base.Predicates;

class Test {
Predicate<String> isNotNull = s -> s != null;
Predicate<String> isLong = s -> s.length() > 5;
Predicate<String> combined = Predicates.and(isNotNull, isLong);
Predicate<String> combined = Predicates.and(s -> s != null, s -> s.length() > 5);
}
""",
"""
import com.google.common.base.Predicate;

class Test {
Predicate<String> isNotNull = s -> s != null;
Predicate<String> isLong = s -> s.length() > 5;
Predicate<String> combined = isNotNull.and(isLong);
Predicate<String> combined = ((Predicate<String>) s -> s != null).and(s -> s.length() > 5);
}
"""
)
);
}

@Test
void replacePredicatesOrWithLambdas() {
//language=java
rewriteRun(
java(
"""
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

class Test {
Predicate<String> combined = Predicates.or(s -> s != null, s -> s.length() > 5);
}
""",
"""
import com.google.common.base.Predicate;

class Test {
Predicate<String> combined = ((Predicate<String>) s -> s != null).or(s -> s.length() > 5);
}
"""
)
Expand Down
Loading