Refine ApplicationContextAotGenerator class name generation

Refine the class name logic so that the name is passed in rather
than using `ApplicationContext.getId()`. Also propagate the name
so that the generated classes use it.

See gh-28565
This commit is contained in:
Phillip Webb
2022-06-04 11:57:02 -07:00
parent 4bd33cb6e0
commit 172102d225
9 changed files with 81 additions and 35 deletions

View File

@@ -98,7 +98,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
assertThat(sourceFile).contains("Get the bean definition for 'testBean'");
@@ -116,7 +116,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
assertThat(actual.getResolvableType().resolve()).isEqualTo(GenericBean.class);
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
@@ -149,7 +149,7 @@ class BeanDefinitionMethodGeneratorTests {
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null, aotContributions);
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
InstanceSupplier<?> supplier = (InstanceSupplier<?>) actual
@@ -175,7 +175,7 @@ class BeanDefinitionMethodGeneratorTests {
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null, aotContributions);
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
@@ -215,7 +215,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, registeredBean, null,
aotContributions);
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
assertThat(actual.getAttribute("a")).isEqualTo("A");
assertThat(actual.getAttribute("b")).isNull();
@@ -248,7 +248,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, innerBean, "testInnerBean",
Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
assertThat(compiled.getSourceFile(".*BeanDefinitions"))
.contains("Get the inner-bean definition for 'testInnerBean'");
@@ -269,7 +269,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual
.getPropertyValues().get("name");
@@ -303,7 +303,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual
.getConstructorArgumentValues()
@@ -336,7 +336,7 @@ class BeanDefinitionMethodGeneratorTests {
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null, aotContributions);
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, (actual, compiled) -> {
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
assertThat(sourceFile).contains("AotContributedMethod()");
@@ -353,7 +353,7 @@ class BeanDefinitionMethodGeneratorTests {
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
this.generationContext, "", this.beanRegistrationsCode);
testCompiledResult(method, false, (actual, compiled) -> {
DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory();
freshBeanFactory.registerBeanDefinition("test", actual);

View File

@@ -37,6 +37,7 @@ import org.springframework.aot.generate.MethodGenerator;
import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.aot.test.generator.file.SourceFile;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -67,7 +68,7 @@ class BeanRegistrationsAotContributionTests {
private BeanDefinitionMethodGeneratorFactory methodGeneratorFactory;
private final MockBeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode();
private MockBeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode();
@BeforeEach
void setup() {
@@ -98,6 +99,25 @@ class BeanRegistrationsAotContributionTests {
});
}
@Test
void applyToWhenHasNameGeneratesPrefixedFeatureName() {
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode("Management");
Map<String, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
RegisteredBean registeredBean = registerBean(
new RootBeanDefinition(TestBean.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
registrations.put("testBean", generator);
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution(
registrations);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
testCompiledResult((consumer, compiled) -> {
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
assertThat(sourceFile.getClassName()).endsWith("__ManagementBeanDefinitions");
});
}
@Test
void applyToCallsRegistrationsWithBeanRegistrationsCode() {
List<BeanRegistrationsCode> beanRegistrationsCodes = new ArrayList<>();
@@ -110,11 +130,11 @@ class BeanRegistrationsAotContributionTests {
@Override
MethodReference generateBeanDefinitionMethod(
GenerationContext generationContext,
GenerationContext generationContext, String featureNamePrefix,
BeanRegistrationsCode beanRegistrationsCode) {
beanRegistrationsCodes.add(beanRegistrationsCode);
return super.generateBeanDefinitionMethod(generationContext,
beanRegistrationsCode);
featureNamePrefix, beanRegistrationsCode);
}
};
@@ -163,6 +183,21 @@ class BeanRegistrationsAotContributionTests {
private final List<MethodReference> initializers = new ArrayList<>();
private final String name;
MockBeanFactoryInitializationCode() {
this("");
}
MockBeanFactoryInitializationCode(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public MethodGenerator getMethodGenerator() {
return this.generatedMethods;