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.Introspector;
|
||||||
import java.beans.PropertyDescriptor;
|
import java.beans.PropertyDescriptor;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -73,6 +74,7 @@ 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 {
|
||||||
|
|
@ -143,13 +145,14 @@ class BeanDefinitionPropertiesCodeGenerator {
|
||||||
Class<?> beanType = ClassUtils
|
Class<?> beanType = ClassUtils
|
||||||
.getUserClass(beanDefinition.getResolvableType().toClass());
|
.getUserClass(beanDefinition.getResolvableType().toClass());
|
||||||
Builder arguments = CodeBlock.builder();
|
Builder arguments = CodeBlock.builder();
|
||||||
for (int i = 0; i < methodNames.length; i++) {
|
String[] filteredMethodNames = Arrays.stream(methodNames)
|
||||||
String methodName = methodNames[i];
|
.filter(methodName -> !AbstractBeanDefinition.INFER_METHOD.equals(methodName))
|
||||||
if (!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);
|
arguments.add((i != 0) ? ", $S" : "$S", methodName);
|
||||||
addInitDestroyHint(beanType, methodName);
|
addInitDestroyHint(beanType, methodName);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!arguments.isEmpty()) {
|
if (!arguments.isEmpty()) {
|
||||||
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
|
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
*/
|
*/
|
||||||
class BeanDefinitionPropertiesCodeGeneratorTests {
|
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) {
|
private void compile(BiConsumer<RootBeanDefinition, Compiled> result) {
|
||||||
compile(attribute -> true, result);
|
compile(attribute -> true, result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue