Skip to content

Commit

Permalink
ReplaceLambdaWithMethodReference skip parameterized method return typ…
Browse files Browse the repository at this point in the history
…es (#240)

* ReplaceLambdaWithMethodReference skip parameterized method return types

Fixes #237

* Apply suggestions from code review
  • Loading branch information
timtebeek authored Feb 5, 2024
1 parent cdcd042 commit f7e6121
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ public J visitLambda(J.Lambda lambda, ExecutionContext ctx) {
if (methodType != null && !isMethodReferenceAmbiguous(methodType)) {
if (methodType.hasFlags(Flag.Static) ||
methodSelectMatchesFirstLambdaParameter(method, lambda)) {
if (method.getType() instanceof JavaType.Parameterized &&
((JavaType.Parameterized) method.getType()).getTypeParameters().stream()
.anyMatch(JavaType.GenericTypeVariable.class::isInstance)) {
return l;
}
J.MemberReference updated = newStaticMethodReference(methodType, true, lambda.getType()).withPrefix(lambda.getPrefix());
doAfterVisit(service(ImportService.class).shortenFullyQualifiedTypeReferencesIn(updated));
return updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1318,4 +1318,31 @@ void bar(Stream<String> stream) {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/237")
void groupingByGetClass() {
rewriteRun(
//language=java
java(
"""
import java.util.*;
import java.util.stream.*;
class Animal {}
class Cat extends Animal {}
class Dog extends Animal {}
class Test {
public void groupOnGetClass() {
List<Animal> animals = List.of(new Cat(), new Dog());
Map<Class<? extends Animal>, List<Animal>> collect;
collect = animals.stream().collect(Collectors.groupingBy(a -> a.getClass()));
}
}
"""
)
);
}

}

0 comments on commit f7e6121

Please sign in to comment.