Merge pull request #28843 from OlgaMaciaszek

* pr/28843:
  Polish "Handle inferred init/destroy method consistently"
  Handle inferred init/destroy method consistently

Closes gh-28843
This commit is contained in:
Stephane Nicoll 2022-07-20 10:46:47 +02:00
commit 4560313b9d
2 changed files with 30 additions and 15 deletions

View File

@ -21,8 +21,10 @@ 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.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;
@ -142,16 +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)
for (int i = 0; i < methodNames.length; i++) { .filter(candidate -> !AbstractBeanDefinition.INFER_METHOD.equals(candidate))
String methodName = methodNames[i]; .toList();
if (!AbstractBeanDefinition.INFER_METHOD.equals(methodName)) { if (!ObjectUtils.isEmpty(filteredMethodNames)) {
arguments.add((i != 0) ? ", $S" : "$S", methodName); filteredMethodNames.forEach(methodName -> addInitDestroyHint(beanType, methodName));
addInitDestroyHint(beanType, methodName); CodeBlock arguments = CodeBlock.join(filteredMethodNames.stream()
} .map(name -> CodeBlock.of("$S", name)).toList(), ", ");
} builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments);
if (!arguments.isEmpty()) {
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments.build());
} }
} }
} }
@ -274,11 +274,11 @@ class BeanDefinitionPropertiesCodeGenerator {
private Object toRole(int value) { private Object toRole(int value) {
return switch (value) { return switch (value) {
case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder() case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder()
.add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build(); .add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build();
case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder() case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder()
.add("$T.ROLE_SUPPORT", BeanDefinition.class).build(); .add("$T.ROLE_SUPPORT", BeanDefinition.class).build();
default -> value; default -> value;
}; };
} }

View File

@ -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 {
@ -244,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);
@ -273,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()