[fixes #3225] Handle ambiguous extension methods

This commit is contained in:
Rawi01 authored and Roel Spilker committed 2024-03-18 00:14:06 +01:00
1 parent fc1c018675
commit 566ef72949
6 files changed
+64 -3

No files matched your search

@@ -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 class PatchExtensionMethod {
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
@@ -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 <T, R> void ambiguous(T t, Function<T, R> function) {
}
public static <T> void ambiguous(T t, Consumer<T> function) {
}
}
}
@@ -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 <T, R>void ambiguous(T t, Function<T, R> function) {
}
public static <T>void ambiguous(T t, Consumer<T> function) {
}
}
ExtensionMethodAmbiguousFunctional() {
super();
}
public void test() {
"".ambiguous(System.out::println);
}
}
@@ -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 <T, R> void ambiguous(T t, Function<T, R> function) {
}
public static <T> void ambiguous(T t, Consumer<T> function) {
}
}
}
@@ -0,0 +1 @@
9 reference to ambiguous is ambiguous both method <T,R>ambiguous(T,java.util.function.Function<T,R>) in ExtensionMethodAmbiguousFunctional.Extensions and method <T>ambiguous(T,java.util.function.Consumer<T>) in ExtensionMethodAmbiguousFunctional.Extensions match
@@ -0,0 +1 @@
10 The method ambiguous(String, Function<String,Object>) is ambiguous for the type ExtensionMethodAmbiguousFunctional.Extensions