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 cc30e470c5..83274e6c11 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,8 +21,10 @@ import java.beans.IntrospectionException; 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.List; import java.util.Map; import java.util.Objects; import java.util.function.BiFunction; @@ -142,16 +144,14 @@ class BeanDefinitionPropertiesCodeGenerator { if (!ObjectUtils.isEmpty(methodNames)) { 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); - } - } - if (!arguments.isEmpty()) { - builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build()); + List filteredMethodNames = Arrays.stream(methodNames) + .filter(candidate -> !AbstractBeanDefinition.INFER_METHOD.equals(candidate)) + .toList(); + if (!ObjectUtils.isEmpty(filteredMethodNames)) { + filteredMethodNames.forEach(methodName -> addInitDestroyHint(beanType, methodName)); + CodeBlock arguments = CodeBlock.join(filteredMethodNames.stream() + .map(name -> CodeBlock.of("$S", name)).toList(), ", "); + builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments); } } } @@ -274,11 +274,11 @@ class BeanDefinitionPropertiesCodeGenerator { private Object toRole(int value) { return switch (value) { - case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder() - .add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build(); - case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder() - .add("$T.ROLE_SUPPORT", BeanDefinition.class).build(); - default -> value; + case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder() + .add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build(); + case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder() + .add("$T.ROLE_SUPPORT", BeanDefinition.class).build(); + default -> value; }; } 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 2b1e78d304..035a63b24d 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 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb * @author Stephane Nicoll + * @author Olga Maciaszek-Sharma */ class BeanDefinitionPropertiesCodeGeneratorTests { @@ -244,6 +245,13 @@ class BeanDefinitionPropertiesCodeGeneratorTests { assertHasMethodInvokeHints(InitDestroyBean.class, methodNames); } + @Test + void setInitMethodWithInferredMethodFirst() { + this.beanDefinition.setInitMethodNames(AbstractBeanDefinition.INFER_METHOD, "init"); + compile((actual, compiled) -> assertThat(compiled.getSourceFile().getContent()) + .contains("beanDefinition.setInitMethodNames(\"init\");")); + } + @Test void setDestroyMethodWhenDestroyInitMethod() { this.beanDefinition.setTargetType(InitDestroyBean.class); @@ -273,6 +281,13 @@ class BeanDefinitionPropertiesCodeGeneratorTests { assertHasMethodInvokeHints(InitDestroyBean.class, methodNames); } + @Test + void setDestroyMethodWithInferredMethodFirst() { + this.beanDefinition.setDestroyMethodNames(AbstractBeanDefinition.INFER_METHOD, "destroy"); + compile((actual, compiled) -> assertThat(compiled.getSourceFile().getContent()) + .contains("beanDefinition.setDestroyMethodNames(\"destroy\");")); + } + private void assertHasMethodInvokeHints(Class beanType, String... methodNames) { assertThat(methodNames).allMatch(methodName -> RuntimeHintsPredicates.reflection() .onMethod(beanType, methodName).invoke()