From 091c9a3fd7567468c0bd7dbab0580f2d08531be0 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Fri, 1 Mar 2024 20:08:02 +0100 Subject: [PATCH] [fixes #3225] Handle ambiguous extension methods --- .../eclipse/agent/PatchExtensionMethod.java | 8 +++++--- .../ExtensionMethodAmbiguousFunctional.java | 17 ++++++++++++++++ .../ExtensionMethodAmbiguousFunctional.java | 20 +++++++++++++++++++ .../ExtensionMethodAmbiguousFunctional.java | 20 +++++++++++++++++++ ...ionMethodAmbiguousFunctional.java.messages | 1 + ...ionMethodAmbiguousFunctional.java.messages | 1 + 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 test/transform/resource/after-delombok/ExtensionMethodAmbiguousFunctional.java create mode 100644 test/transform/resource/after-ecj/ExtensionMethodAmbiguousFunctional.java create mode 100644 test/transform/resource/before/ExtensionMethodAmbiguousFunctional.java create mode 100644 test/transform/resource/messages-delombok/ExtensionMethodAmbiguousFunctional.java.messages create mode 100644 test/transform/resource/messages-ecj/ExtensionMethodAmbiguousFunctional.java.messages diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java index 3b89c0af0..555c84b0c 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchExtensionMethod.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2023 The Project Lombok Authors. + * Copyright (C) 2012-2024 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -321,9 +321,11 @@ public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend meth MethodBinding fixedBinding = scope.getMethod(extensionMethod.declaringClass, methodCall.selector, argumentTypes.toArray(new TypeBinding[0]), methodCall); if (fixedBinding instanceof ProblemMethodBinding) { methodCall.arguments = originalArgs; - if (fixedBinding.declaringClass != null) { - PostponedInvalidMethodError.invoke(scope.problemReporter(), methodCall, fixedBinding, scope); + // Sometimes the declaring class is null, in that case we have to create a new ProblemMethodBinding using the extension method's declaring class + if (fixedBinding.declaringClass == null) { + fixedBinding = new ProblemMethodBinding(fixedBinding.selector, fixedBinding.parameters, extensionMethod.declaringClass, fixedBinding.problemId()); } + PostponedInvalidMethodError.invoke(scope.problemReporter(), methodCall, fixedBinding, scope); } else { // If the extension method uses varargs, the last fixed binding parameter is an array but // the method arguments are not. Even thought we already know that the method is fine we still diff --git a/test/transform/resource/after-delombok/ExtensionMethodAmbiguousFunctional.java b/test/transform/resource/after-delombok/ExtensionMethodAmbiguousFunctional.java new file mode 100644 index 000000000..8c93c03db --- /dev/null +++ b/test/transform/resource/after-delombok/ExtensionMethodAmbiguousFunctional.java @@ -0,0 +1,17 @@ +//skip-idempotent +import java.util.function.Consumer; +import java.util.function.Function; + +class ExtensionMethodAmbiguousFunctional { + public void test() { + ExtensionMethodAmbiguousFunctional.Extensions.ambiguous("", System.out::println); + } + + static class Extensions { + public static void ambiguous(T t, Function function) { + } + + public static void ambiguous(T t, Consumer function) { + } + } +} diff --git a/test/transform/resource/after-ecj/ExtensionMethodAmbiguousFunctional.java b/test/transform/resource/after-ecj/ExtensionMethodAmbiguousFunctional.java new file mode 100644 index 000000000..80d216b26 --- /dev/null +++ b/test/transform/resource/after-ecj/ExtensionMethodAmbiguousFunctional.java @@ -0,0 +1,20 @@ +import java.util.function.Consumer; +import java.util.function.Function; +import lombok.experimental.ExtensionMethod; +@ExtensionMethod({ExtensionMethodAmbiguousFunctional.Extensions.class}) class ExtensionMethodAmbiguousFunctional { + static class Extensions { + Extensions() { + super(); + } + public static void ambiguous(T t, Function function) { + } + public static void ambiguous(T t, Consumer function) { + } + } + ExtensionMethodAmbiguousFunctional() { + super(); + } + public void test() { + "".ambiguous(System.out::println); + } +} diff --git a/test/transform/resource/before/ExtensionMethodAmbiguousFunctional.java b/test/transform/resource/before/ExtensionMethodAmbiguousFunctional.java new file mode 100644 index 000000000..65c08404d --- /dev/null +++ b/test/transform/resource/before/ExtensionMethodAmbiguousFunctional.java @@ -0,0 +1,20 @@ +//version 8: +import java.util.function.Consumer; +import java.util.function.Function; + +import lombok.experimental.ExtensionMethod; + +@ExtensionMethod({ExtensionMethodAmbiguousFunctional.Extensions.class}) +class ExtensionMethodAmbiguousFunctional { + public void test() { + "".ambiguous(System.out::println); + } + + static class Extensions { + public static void ambiguous(T t, Function function) { + } + + public static void ambiguous(T t, Consumer function) { + } + } +} diff --git a/test/transform/resource/messages-delombok/ExtensionMethodAmbiguousFunctional.java.messages b/test/transform/resource/messages-delombok/ExtensionMethodAmbiguousFunctional.java.messages new file mode 100644 index 000000000..9fa3414f7 --- /dev/null +++ b/test/transform/resource/messages-delombok/ExtensionMethodAmbiguousFunctional.java.messages @@ -0,0 +1 @@ +9 reference to ambiguous is ambiguous both method ambiguous(T,java.util.function.Function) in ExtensionMethodAmbiguousFunctional.Extensions and method ambiguous(T,java.util.function.Consumer) in ExtensionMethodAmbiguousFunctional.Extensions match diff --git a/test/transform/resource/messages-ecj/ExtensionMethodAmbiguousFunctional.java.messages b/test/transform/resource/messages-ecj/ExtensionMethodAmbiguousFunctional.java.messages new file mode 100644 index 000000000..63e76c93a --- /dev/null +++ b/test/transform/resource/messages-ecj/ExtensionMethodAmbiguousFunctional.java.messages @@ -0,0 +1 @@ +10 The method ambiguous(String, Function) is ambiguous for the type ExtensionMethodAmbiguousFunctional.Extensions