From 68e28a5f5958f9ff74c76183006e5cc2623a0765 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Tue, 19 Jul 2022 19:35:55 +0200 Subject: [PATCH] Handle inferred init/destroy method consistently See gh-28843 --- .../BeanDefinitionPropertiesCodeGenerator.java | 15 +++++++++------ ...eanDefinitionPropertiesCodeGeneratorTests.java | 13 +++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGenerator.java index cc30e470c56c..65310628f75b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGenerator.java @@ -21,6 +21,7 @@ import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -73,6 +74,7 @@ * * @author Phillip Webb * @author Stephane Nicoll + * @author Olga Maciaszek-Sharma * @since 6.0 */ class BeanDefinitionPropertiesCodeGenerator { @@ -143,12 +145,13 @@ private void addInitDestroyMethods(Builder builder, Class beanType = ClassUtils .getUserClass(beanDefinition.getResolvableType().toClass()); Builder arguments = CodeBlock.builder(); - for (int i = 0; i < methodNames.length; i++) { - String methodName = methodNames[i]; - if (!AbstractBeanDefinition.INFER_METHOD.equals(methodName)) { - arguments.add((i != 0) ? ", $S" : "$S", methodName); - addInitDestroyHint(beanType, methodName); - } + String[] filteredMethodNames = Arrays.stream(methodNames) + .filter(methodName -> !AbstractBeanDefinition.INFER_METHOD.equals(methodName)) + .toArray(String[]::new); + for (int i = 0; i < filteredMethodNames.length; i++) { + String methodName = filteredMethodNames[i]; + arguments.add((i != 0) ? ", $S" : "$S", methodName); + addInitDestroyHint(beanType, methodName); } if (!arguments.isEmpty()) { builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build()); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java index 2b1e78d30406..76ddb895af5c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java @@ -57,6 +57,7 @@ * * @author Phillip Webb * @author Stephane Nicoll + * @author Olga Maciaszek-Sharma */ class BeanDefinitionPropertiesCodeGeneratorTests { @@ -394,6 +395,18 @@ void multipleItems() { }); } + @Test + void inferredMethodsAtTheBeginning() { + this.beanDefinition.setInitMethodNames(AbstractBeanDefinition.INFER_METHOD, "init"); + this.beanDefinition.setDestroyMethodNames(AbstractBeanDefinition.INFER_METHOD, "destroy"); + compile((actual, compiled) -> { + assertThat(compiled.getSourceFile().getContent()) + .contains("beanDefinition.setInitMethodNames(\"init\");"); + assertThat(compiled.getSourceFile().getContent()) + .contains("beanDefinition.setDestroyMethodNames(\"destroy\");"); + }); + } + private void compile(BiConsumer result) { compile(attribute -> true, result); }