Skip to content

Commit 4dc93bc

Browse files
committed
Avoid ambiguous call with BeanInstanceSupplier#withGenerator
Previously, BeanInstanceSupplier had three variants of the `withGenerator` callback, one with a bi function, one with a function, and with a supplier. This could lead to compilation failure when the target type has a method with the same name and a number of arguments that match another variant. It turns out the supplier-based variant is only used a shortcut. This commit deprecates it and update ghe code generation to use the function instead. Closes gh-29278
1 parent 68f2b0c commit 4dc93bc

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ public BeanInstanceSupplier<T> withGenerator(ThrowingFunction<RegisteredBean, T>
172172
* {@code generator} supplier to instantiate the underlying bean.
173173
* @param generator a {@link ThrowingSupplier} to instantiate the underlying bean
174174
* @return a new {@link BeanInstanceSupplier} instance with the specified generator
175+
* @deprecated in favor of {@link #withGenerator(ThrowingFunction)}
175176
*/
177+
@Deprecated(since = "6.0.11", forRemoval = true)
176178
public BeanInstanceSupplier<T> withGenerator(ThrowingSupplier<T> generator) {
177179
Assert.notNull(generator, "'generator' must not be null");
178180
return new BeanInstanceSupplier<>(this.lookup,

spring-beans/src/main/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ private CodeBlock generateCodeForAccessibleFactoryMethod(String beanName,
220220
CodeBlock.Builder code = CodeBlock.builder();
221221
code.add("$T.<$T>forFactoryMethod($T.class, $S)", BeanInstanceSupplier.class,
222222
suppliedType, declaringClass, factoryMethod.getName());
223-
code.add(".withGenerator($T::$L)", declaringClass, factoryMethod.getName());
223+
code.add(".withGenerator(($L) -> $T.$L())", REGISTERED_BEAN_PARAMETER_NAME,
224+
declaringClass, factoryMethod.getName());
224225
return code.build();
225226
}
226227

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanInstanceSupplierTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ void withGeneratorWhenFunctionIsNullThrowsException() {
186186
}
187187

188188
@Test
189+
@Deprecated
190+
@SuppressWarnings("removal")
189191
void withGeneratorWhenSupplierIsNullThrowsException() {
190192
BeanInstanceSupplier<Object> resolver = BeanInstanceSupplier
191193
.forConstructor();
@@ -245,6 +247,8 @@ void getWithGeneratorCallsFunction() throws Exception {
245247
}
246248

247249
@Test
250+
@Deprecated
251+
@SuppressWarnings("removal")
248252
void getWithGeneratorCallsSupplier() throws Exception {
249253
BeanRegistrar registrar = new BeanRegistrar(SingleArgConstructor.class);
250254
this.beanFactory.registerSingleton("one", "1");

spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -223,7 +223,7 @@ void generateWhenHasStaticFactoryMethodWithNoArg() {
223223
assertThat(bean).isInstanceOf(Integer.class);
224224
assertThat(bean).isEqualTo(42);
225225
assertThat(compiled.getSourceFile())
226-
.contains("SimpleConfiguration::integerBean");
226+
.contains("(registeredBean) -> SimpleConfiguration.integerBean()");
227227
});
228228
assertThat(getReflectionHints().getTypeHint(SimpleConfiguration.class))
229229
.satisfies(hasMethodWithMode(ExecutableMode.INTROSPECT));

0 commit comments

Comments
 (0)