From fc7ed29838cc092056658e61238ecc49a4ea45ce Mon Sep 17 00:00:00 2001 From: Marc Bruggmann Date: Thu, 4 Jul 2024 17:12:51 +0200 Subject: [PATCH] Don't use lambdas for methods with type parameters. (#310) See the re-enabled testcase. Fixes #309 --- .../staticanalysis/UseLambdaForFunctionalInterface.java | 5 +++++ .../staticanalysis/UseLambdaForFunctionalInterfaceTest.java | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterface.java b/src/main/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterface.java index bd6367896..ba3296dfd 100644 --- a/src/main/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterface.java +++ b/src/main/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterface.java @@ -96,6 +96,11 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) { StringBuilder templateBuilder = new StringBuilder(); J.MethodDeclaration methodDeclaration = (J.MethodDeclaration) n.getBody().getStatements().get(0); + // If the functional interface method has type parameters, we can't replace it with a lambda. + if (methodDeclaration.getTypeParameters() != null && !methodDeclaration.getTypeParameters().isEmpty()) { + return n; + } + if (methodDeclaration.getParameters().get(0) instanceof J.Empty) { templateBuilder.append("() -> {"); } else { diff --git a/src/test/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterfaceTest.java b/src/test/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterfaceTest.java index dd30a98a2..06bc31140 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterfaceTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterfaceTest.java @@ -736,7 +736,6 @@ public MapDropdownChoice(Supplier> choiceMap) { ); } - @Disabled @Test @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/309") void dontUseLambdaForMethodWithTypeParameter() {