Handle inferred init/destroy method consistently
See gh-28843
This commit is contained in:
parent
d6345db7c9
commit
68e28a5f59
|
|
@ -21,6 +21,7 @@ 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.Map;
|
||||
|
|
@ -73,6 +74,7 @@ import org.springframework.util.StringUtils;
|
|||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @since 6.0
|
||||
*/
|
||||
class BeanDefinitionPropertiesCodeGenerator {
|
||||
|
|
@ -143,12 +145,13 @@ class BeanDefinitionPropertiesCodeGenerator {
|
|||
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());
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @author Olga Maciaszek-Sharma
|
||||
*/
|
||||
class BeanDefinitionPropertiesCodeGeneratorTests {
|
||||
|
||||
|
|
@ -394,6 +395,18 @@ 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) {
|
||||
compile(attribute -> true, result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue