Skip to content

Commit

Permalink
Polish "Handle inferred init/destroy method consistently"
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Jul 20, 2022
1 parent 68e28a5 commit 85173b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -74,7 +75,6 @@
*
* @author Phillip Webb
* @author Stephane Nicoll
* @author Olga Maciaszek-Sharma
* @since 6.0
*/
class BeanDefinitionPropertiesCodeGenerator {
Expand Down Expand Up @@ -144,17 +144,14 @@ private void addInitDestroyMethods(Builder builder,
if (!ObjectUtils.isEmpty(methodNames)) {
Class<?> beanType = ClassUtils
.getUserClass(beanDefinition.getResolvableType().toClass());
Builder arguments = CodeBlock.builder();
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());
List<String> filteredMethodNames = Arrays.stream(methodNames)
.filter(candidate -> !AbstractBeanDefinition.INFER_METHOD.equals(candidate))
.toList();
if (!ObjectUtils.isEmpty(filteredMethodNames)) {
filteredMethodNames.forEach(methodName -> addInitDestroyHint(beanType, methodName));
CodeBlock arguments = CodeBlock.join(filteredMethodNames.stream()
.map(name -> CodeBlock.of("$S", name)).toList(), ", ");
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments);
}
}
}
Expand Down Expand Up @@ -277,11 +274,11 @@ private CodeBlock toStringVarArgs(String[] strings) {

private Object toRole(int value) {
return switch (value) {
case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder()
.add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build();
case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder()
.add("$T.ROLE_SUPPORT", BeanDefinition.class).build();
default -> value;
case BeanDefinition.ROLE_INFRASTRUCTURE -> CodeBlock.builder()
.add("$T.ROLE_INFRASTRUCTURE", BeanDefinition.class).build();
case BeanDefinition.ROLE_SUPPORT -> CodeBlock.builder()
.add("$T.ROLE_SUPPORT", BeanDefinition.class).build();
default -> value;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ void setInitMethodWhenMultipleInitMethods() {
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
void setDestroyMethodWhenDestroyInitMethod() {
this.beanDefinition.setTargetType(InitDestroyBean.class);
Expand Down Expand Up @@ -274,6 +281,13 @@ void setDestroyMethodWhenMultipleDestroyMethods() {
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) {
assertThat(methodNames).allMatch(methodName -> RuntimeHintsPredicates.reflection()
.onMethod(beanType, methodName).invoke()
Expand Down Expand Up @@ -395,18 +409,6 @@ void multipleItems() {
});
}

@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);
}
Expand Down

0 comments on commit 85173b5

Please sign in to comment.