From 4dc93bc4852641417db34360330be293b878410e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 12 Jul 2023 16:40:22 +0200 Subject: [PATCH] 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 --- .../beans/factory/aot/BeanInstanceSupplier.java | 2 ++ .../beans/factory/aot/InstanceSupplierCodeGenerator.java | 3 ++- .../beans/factory/aot/BeanInstanceSupplierTests.java | 4 ++++ .../beans/factory/aot/InstanceSupplierCodeGeneratorTests.java | 4 ++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java index 020164115da0..291cbba4f2b6 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java @@ -172,7 +172,9 @@ public BeanInstanceSupplier withGenerator(ThrowingFunction * {@code generator} supplier to instantiate the underlying bean. * @param generator a {@link ThrowingSupplier} to instantiate the underlying bean * @return a new {@link BeanInstanceSupplier} instance with the specified generator + * @deprecated in favor of {@link #withGenerator(ThrowingFunction)} */ + @Deprecated(since = "6.0.11", forRemoval = true) public BeanInstanceSupplier withGenerator(ThrowingSupplier generator) { Assert.notNull(generator, "'generator' must not be null"); return new BeanInstanceSupplier<>(this.lookup, diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGenerator.java index 171a92061283..bd1ff37f77f2 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGenerator.java @@ -220,7 +220,8 @@ private CodeBlock generateCodeForAccessibleFactoryMethod(String beanName, CodeBlock.Builder code = CodeBlock.builder(); code.add("$T.<$T>forFactoryMethod($T.class, $S)", BeanInstanceSupplier.class, suppliedType, declaringClass, factoryMethod.getName()); - code.add(".withGenerator($T::$L)", declaringClass, factoryMethod.getName()); + code.add(".withGenerator(($L) -> $T.$L())", REGISTERED_BEAN_PARAMETER_NAME, + declaringClass, factoryMethod.getName()); return code.build(); } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanInstanceSupplierTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanInstanceSupplierTests.java index ce613cc00887..422de9d4420d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanInstanceSupplierTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanInstanceSupplierTests.java @@ -186,6 +186,8 @@ void withGeneratorWhenFunctionIsNullThrowsException() { } @Test + @Deprecated + @SuppressWarnings("removal") void withGeneratorWhenSupplierIsNullThrowsException() { BeanInstanceSupplier resolver = BeanInstanceSupplier .forConstructor(); @@ -245,6 +247,8 @@ void getWithGeneratorCallsFunction() throws Exception { } @Test + @Deprecated + @SuppressWarnings("removal") void getWithGeneratorCallsSupplier() throws Exception { BeanRegistrar registrar = new BeanRegistrar(SingleArgConstructor.class); this.beanFactory.registerSingleton("one", "1"); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java index 43e21771651b..b849b3702209 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -223,7 +223,7 @@ void generateWhenHasStaticFactoryMethodWithNoArg() { assertThat(bean).isInstanceOf(Integer.class); assertThat(bean).isEqualTo(42); assertThat(compiled.getSourceFile()) - .contains("SimpleConfiguration::integerBean"); + .contains("(registeredBean) -> SimpleConfiguration.integerBean()"); }); assertThat(getReflectionHints().getTypeHint(SimpleConfiguration.class)) .satisfies(hasMethodWithMode(ExecutableMode.INTROSPECT));