Apply property hints to FactoryBean if necessary

This commit handles a BeanDefinition that configures the FactoryBean
as the "beanClass", while exposing the actual type in "resolvedType".
While unusual, this is required in certain cases when the factory
bean exposes generic information itself.

Previously, the hints for properties injection were applied on the
user type.

Closes gh-28913
This commit is contained in:
Stephane Nicoll
2022-08-03 10:58:41 +02:00
parent e79a8f6733
commit c9faff7491
2 changed files with 66 additions and 4 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.aot.generate.GeneratedClass;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanReference;
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
@@ -46,6 +47,7 @@ import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.MethodSpec;
import org.springframework.javapoet.ParameterizedTypeName;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
@@ -353,6 +355,20 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
});
}
@Test
void propertyValuesWhenValuesOnFactoryBeanClass() {
this.beanDefinition.setTargetType(String.class);
this.beanDefinition.setBeanClass(PropertyValuesFactoryBean.class);
this.beanDefinition.getPropertyValues().add("prefix", "Hello");
this.beanDefinition.getPropertyValues().add("name", "World");
compile((actual, compiled) -> {
assertThat(actual.getPropertyValues().get("prefix")).isEqualTo("Hello");
assertThat(actual.getPropertyValues().get("name")).isEqualTo("World");
});
String[] methodNames = { "setPrefix", "setName" };
assertHasMethodInvokeHints(PropertyValuesFactoryBean.class, methodNames);
}
@Test
void attributesWhenAllFiltered() {
this.beanDefinition.setAttribute("a", "A");
@@ -460,4 +476,40 @@ class BeanDefinitionPropertiesCodeGeneratorTests {
}
static class PropertyValuesFactoryBean implements FactoryBean<String> {
private Class<?> prefix;
private String name;
public Class<?> getPrefix() {
return this.prefix;
}
public void setPrefix(Class<?> prefix) {
this.prefix = prefix;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Nullable
@Override
public String getObject() throws Exception {
return getPrefix() + " " + getName();
}
@Nullable
@Override
public Class<?> getObjectType() {
return String.class;
}
}
}