Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inferred methods code generation #28843

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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