Polish "Handle inferred init/destroy method consistently"
See gh-28843
This commit is contained in:
parent
68e28a5f59
commit
85173b5de3
|
|
@ -24,6 +24,7 @@ import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
@ -74,7 +75,6 @@ import org.springframework.util.StringUtils;
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Olga Maciaszek-Sharma
|
|
||||||
* @since 6.0
|
* @since 6.0
|
||||||
*/
|
*/
|
||||||
class BeanDefinitionPropertiesCodeGenerator {
|
class BeanDefinitionPropertiesCodeGenerator {
|
||||||
|
|
@ -144,17 +144,14 @@ class BeanDefinitionPropertiesCodeGenerator {
|
||||||
if (!ObjectUtils.isEmpty(methodNames)) {
|
if (!ObjectUtils.isEmpty(methodNames)) {
|
||||||
Class<?> beanType = ClassUtils
|
Class<?> beanType = ClassUtils
|
||||||
.getUserClass(beanDefinition.getResolvableType().toClass());
|
.getUserClass(beanDefinition.getResolvableType().toClass());
|
||||||
Builder arguments = CodeBlock.builder();
|
List<String> filteredMethodNames = Arrays.stream(methodNames)
|
||||||
String[] filteredMethodNames = Arrays.stream(methodNames)
|
.filter(candidate -> !AbstractBeanDefinition.INFER_METHOD.equals(candidate))
|
||||||
.filter(methodName -> !AbstractBeanDefinition.INFER_METHOD.equals(methodName))
|
.toList();
|
||||||
.toArray(String[]::new);
|
if (!ObjectUtils.isEmpty(filteredMethodNames)) {
|
||||||
for (int i = 0; i < filteredMethodNames.length; i++) {
|
filteredMethodNames.forEach(methodName -> addInitDestroyHint(beanType, methodName));
|
||||||
String methodName = filteredMethodNames[i];
|
CodeBlock arguments = CodeBlock.join(filteredMethodNames.stream()
|
||||||
arguments.add((i != 0) ? ", $S" : "$S", methodName);
|
.map(name -> CodeBlock.of("$S", name)).toList(), ", ");
|
||||||
addInitDestroyHint(beanType, methodName);
|
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments);
|
||||||
}
|
|
||||||
if (!arguments.isEmpty()) {
|
|
||||||
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -245,6 +245,13 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
|
||||||
assertHasMethodInvokeHints(InitDestroyBean.class, methodNames);
|
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
|
@Test
|
||||||
void setDestroyMethodWhenDestroyInitMethod() {
|
void setDestroyMethodWhenDestroyInitMethod() {
|
||||||
this.beanDefinition.setTargetType(InitDestroyBean.class);
|
this.beanDefinition.setTargetType(InitDestroyBean.class);
|
||||||
|
|
@ -274,6 +281,13 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
|
||||||
assertHasMethodInvokeHints(InitDestroyBean.class, methodNames);
|
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) {
|
private void assertHasMethodInvokeHints(Class<?> beanType, String... methodNames) {
|
||||||
assertThat(methodNames).allMatch(methodName -> RuntimeHintsPredicates.reflection()
|
assertThat(methodNames).allMatch(methodName -> RuntimeHintsPredicates.reflection()
|
||||||
.onMethod(beanType, methodName).invoke()
|
.onMethod(beanType, methodName).invoke()
|
||||||
|
|
@ -395,18 +409,6 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@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<RootBeanDefinition, Compiled> result) {
|
private void compile(BiConsumer<RootBeanDefinition, Compiled> result) {
|
||||||
compile(attribute -> true, result);
|
compile(attribute -> true, result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue