Skip to content

Commit

Permalink
Handle inferred init/destroy method consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek authored and snicoll committed Jul 20, 2022
1 parent d6345db commit 68e28a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -73,6 +74,7 @@
*
* @author Phillip Webb
* @author Stephane Nicoll
* @author Olga Maciaszek-Sharma
* @since 6.0
*/
class BeanDefinitionPropertiesCodeGenerator {
Expand Down Expand Up @@ -143,12 +145,13 @@ private void addInitDestroyMethods(Builder builder,
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
*
* @author Phillip Webb
* @author Stephane Nicoll
* @author Olga Maciaszek-Sharma
*/
class BeanDefinitionPropertiesCodeGeneratorTests {

Expand Down Expand Up @@ -394,6 +395,18 @@ 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 68e28a5

Please sign in to comment.