Skip to content

Bean definition contribution includes attributes that are not used at… #28538

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

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 @@ -88,19 +88,16 @@ class BeanDefinitionPropertiesCodeGenerator {

private final RuntimeHints hints;

private final Predicate<String> attributeFilter;

private final BiFunction<String, Object, CodeBlock> customValueCodeGenerator;

private final BeanDefinitionPropertyValueCodeGenerator valueCodeGenerator;


BeanDefinitionPropertiesCodeGenerator(RuntimeHints hints,
Predicate<String> attributeFilter, MethodGenerator methodGenerator,
MethodGenerator methodGenerator,
BiFunction<String, Object, CodeBlock> customValueCodeGenerator) {

this.hints = hints;
this.attributeFilter = attributeFilter;
this.customValueCodeGenerator = customValueCodeGenerator;
this.valueCodeGenerator = new BeanDefinitionPropertyValueCodeGenerator(
methodGenerator);
Expand Down Expand Up @@ -133,7 +130,6 @@ CodeBlock generateCode(BeanDefinition beanDefinition) {
}
addConstructorArgumentValues(builder, beanDefinition);
addPropertyValues(builder, beanDefinition);
addAttributes(builder, beanDefinition);
return builder.build();
}

Expand Down Expand Up @@ -243,21 +239,6 @@ private Method findWriteMethod(BeanInfo beanInfo, String propertyName) {
.filter(Objects::nonNull).findFirst().orElse(null);
}


private void addAttributes(CodeBlock.Builder builder, BeanDefinition beanDefinition) {
String[] attributeNames = beanDefinition.attributeNames();
if (!ObjectUtils.isEmpty(attributeNames)) {
for (String attributeName : attributeNames) {
if (this.attributeFilter.test(attributeName)) {
CodeBlock value = this.valueCodeGenerator
.generateCode(beanDefinition.getAttribute(attributeName));
builder.addStatement("$L.setAttribute($S, $L)",
BEAN_DEFINITION_VARIABLE, attributeName, value);
}
}
}
}

private boolean hasScope(String defaultValue, String actualValue) {
return StringUtils.hasText(actualValue)
&& !ConfigurableBeanFactory.SCOPE_SINGLETON.equals(actualValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public CodeBlock generateSetBeanDefinitionPropertiesCode(
Predicate<String> attributeFilter) {

return new BeanDefinitionPropertiesCodeGenerator(
generationContext.getRuntimeHints(), attributeFilter,
generationContext.getRuntimeHints(),
beanRegistrationCode.getMethodGenerator(),
(name, value) -> generateValueCode(generationContext, name, value))
.generateCode(beanDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,41 +167,22 @@ void generateBeanDefinitionMethodWhenHasInstancePostProcessorGeneratesMethod() {
}

@Test
void generateBeanDefinitionMethodWhenHasAttributeFilterGeneratesMethod() {
void generateBeanDefinitionMethodWhenHasAttributes() {
RootBeanDefinition beanDefinition = new RootBeanDefinition(TestBean.class);
beanDefinition.setAttribute("a", "A");
beanDefinition.setAttribute("b", "B");
RegisteredBean registeredBean = registerBean(beanDefinition);
List<BeanRegistrationCodeFragmentsCustomizer> fragmentCustomizers = Collections
.singletonList(this::customizeWithAttributeFilter);
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList(), fragmentCustomizers);
Collections.emptyList(), Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
assertThat(actual.getAttribute("a")).isEqualTo("A");
assertThat(actual.getAttribute("a")).isNull();
assertThat(actual.getAttribute("b")).isNull();
});
}

private BeanRegistrationCodeFragments customizeWithAttributeFilter(
RegisteredBean registeredBean, BeanRegistrationCodeFragments codeFragments) {
return new BeanRegistrationCodeFragments(codeFragments) {

@Override
public CodeBlock generateSetBeanDefinitionPropertiesCode(
GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode,
RootBeanDefinition beanDefinition,
Predicate<String> attributeFilter) {
return super.generateSetBeanDefinitionPropertiesCode(generationContext,
beanRegistrationCode, beanDefinition, name -> "a".equals(name));
}

};
}

@Test
void generateBeanDefinitionMethodWhenInnerBeanGeneratesMethod() {
RegisteredBean parent = registerBean(new RootBeanDefinition(TestBean.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
private final RuntimeHints hints = new RuntimeHints();

private BeanDefinitionPropertiesCodeGenerator generator = new BeanDefinitionPropertiesCodeGenerator(
this.hints, attribute -> true, this.generatedMethods, (name, value) -> null);
this.hints, this.generatedMethods, (name, value) -> null);


@Test
Expand Down Expand Up @@ -353,33 +353,6 @@ void propertyValuesWhenContainsManagedMap() {
});
}

@Test
void attributesWhenAllFiltered() {
this.beanDefinition.setAttribute("a", "A");
this.beanDefinition.setAttribute("b", "B");
Predicate<String> attributeFilter = attribute -> false;
this.generator = new BeanDefinitionPropertiesCodeGenerator(this.hints,
attributeFilter, this.generatedMethods, (name, value) -> null);
testCompiledResult((actual, compiled) -> {
assertThat(compiled.getSourceFile()).doesNotContain("setAttribute");
assertThat(actual.getAttribute("a")).isNull();
assertThat(actual.getAttribute("b")).isNull();
});
}

@Test
void attributesWhenSomeFiltered() {
this.beanDefinition.setAttribute("a", "A");
this.beanDefinition.setAttribute("b", "B");
Predicate<String> attributeFilter = attribute -> "a".equals(attribute);
this.generator = new BeanDefinitionPropertiesCodeGenerator(this.hints,
attributeFilter, this.generatedMethods, (name, value) -> null);
testCompiledResult(this.beanDefinition, (actual, compiled) -> {
assertThat(actual.getAttribute("a")).isEqualTo("A");
assertThat(actual.getAttribute("b")).isNull();
});
}

@Test
void multipleItems() {
this.beanDefinition.setPrimary(true);
Expand Down