Skip to content

Commit 85173b5

Browse files
committed
Polish "Handle inferred init/destroy method consistently"
See gh-28843
1 parent 68e28a5 commit 85173b5

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Arrays;
2525
import java.util.Collections;
2626
import java.util.HashMap;
27+
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Objects;
2930
import java.util.function.BiFunction;
@@ -74,7 +75,6 @@
7475
*
7576
* @author Phillip Webb
7677
* @author Stephane Nicoll
77-
* @author Olga Maciaszek-Sharma
7878
* @since 6.0
7979
*/
8080
class BeanDefinitionPropertiesCodeGenerator {
@@ -144,17 +144,14 @@ private void addInitDestroyMethods(Builder builder,
144144
if (!ObjectUtils.isEmpty(methodNames)) {
145145
Class<?> beanType = ClassUtils
146146
.getUserClass(beanDefinition.getResolvableType().toClass());
147-
Builder arguments = CodeBlock.builder();
148-
String[] filteredMethodNames = Arrays.stream(methodNames)
149-
.filter(methodName -> !AbstractBeanDefinition.INFER_METHOD.equals(methodName))
150-
.toArray(String[]::new);
151-
for (int i = 0; i < filteredMethodNames.length; i++) {
152-
String methodName = filteredMethodNames[i];
153-
arguments.add((i != 0) ? ", $S" : "$S", methodName);
154-
addInitDestroyHint(beanType, methodName);
155-
}
156-
if (!arguments.isEmpty()) {
157-
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
147+
List<String> filteredMethodNames = Arrays.stream(methodNames)
148+
.filter(candidate -> !AbstractBeanDefinition.INFER_METHOD.equals(candidate))
149+
.toList();
150+
if (!ObjectUtils.isEmpty(filteredMethodNames)) {
151+
filteredMethodNames.forEach(methodName -> addInitDestroyHint(beanType, methodName));
152+
CodeBlock arguments = CodeBlock.join(filteredMethodNames.stream()
153+
.map(name -> CodeBlock.of("$S", name)).toList(), ", ");
154+
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments);
158155
}
159156
}
160157
}
@@ -277,11 +274,11 @@ private CodeBlock toStringVarArgs(String[] strings) {
277274

278275
private Object toRole(int value) {
279276
return switch (value) {
280-
case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder()
281-
.add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build();
282-
case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder()
283-
.add("$T.ROLE_SUPPORT", BeanDefinition.class).build();
284-
default -> value;
277+
case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder()
278+
.add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build();
279+
case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder()
280+
.add("$T.ROLE_SUPPORT", BeanDefinition.class).build();
281+
default -> value;
285282
};
286283
}
287284

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ void setInitMethodWhenMultipleInitMethods() {
245245
assertHasMethodInvokeHints(InitDestroyBean.class, methodNames);
246246
}
247247

248+
@Test
249+
void setInitMethodWithInferredMethodFirst() {
250+
this.beanDefinition.setInitMethodNames(AbstractBeanDefinition.INFER_METHOD, "init");
251+
compile((actual, compiled) -> assertThat(compiled.getSourceFile().getContent())
252+
.contains("beanDefinition.setInitMethodNames(\"init\");"));
253+
}
254+
248255
@Test
249256
void setDestroyMethodWhenDestroyInitMethod() {
250257
this.beanDefinition.setTargetType(InitDestroyBean.class);
@@ -274,6 +281,13 @@ void setDestroyMethodWhenMultipleDestroyMethods() {
274281
assertHasMethodInvokeHints(InitDestroyBean.class, methodNames);
275282
}
276283

284+
@Test
285+
void setDestroyMethodWithInferredMethodFirst() {
286+
this.beanDefinition.setDestroyMethodNames(AbstractBeanDefinition.INFER_METHOD, "destroy");
287+
compile((actual, compiled) -> assertThat(compiled.getSourceFile().getContent())
288+
.contains("beanDefinition.setDestroyMethodNames(\"destroy\");"));
289+
}
290+
277291
private void assertHasMethodInvokeHints(Class<?> beanType, String... methodNames) {
278292
assertThat(methodNames).allMatch(methodName -> RuntimeHintsPredicates.reflection()
279293
.onMethod(beanType, methodName).invoke()
@@ -395,18 +409,6 @@ void multipleItems() {
395409
});
396410
}
397411

398-
@Test
399-
void inferredMethodsAtTheBeginning() {
400-
this.beanDefinition.setInitMethodNames(AbstractBeanDefinition.INFER_METHOD, "init");
401-
this.beanDefinition.setDestroyMethodNames(AbstractBeanDefinition.INFER_METHOD, "destroy");
402-
compile((actual, compiled) -> {
403-
assertThat(compiled.getSourceFile().getContent())
404-
.contains("beanDefinition.setInitMethodNames(\"init\");");
405-
assertThat(compiled.getSourceFile().getContent())
406-
.contains("beanDefinition.setDestroyMethodNames(\"destroy\");");
407-
});
408-
}
409-
410412
private void compile(BiConsumer<RootBeanDefinition, Compiled> result) {
411413
compile(attribute -> true, result);
412414
}

0 commit comments

Comments
 (0)