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:
parent
4bd33cb6e0
commit
172102d225
|
|
@ -81,19 +81,21 @@ class BeanDefinitionMethodGenerator {
|
||||||
* Generate the method that returns the {@link BeanDefinition} to be
|
* Generate the method that returns the {@link BeanDefinition} to be
|
||||||
* registered.
|
* registered.
|
||||||
* @param generationContext the generation context
|
* @param generationContext the generation context
|
||||||
|
* @param featureNamePrefix the prefix to use for the feature name
|
||||||
* @param beanRegistrationsCode the bean registrations code
|
* @param beanRegistrationsCode the bean registrations code
|
||||||
* @return a reference to the generated method.
|
* @return a reference to the generated method.
|
||||||
*/
|
*/
|
||||||
MethodReference generateBeanDefinitionMethod(GenerationContext generationContext,
|
MethodReference generateBeanDefinitionMethod(GenerationContext generationContext,
|
||||||
BeanRegistrationsCode beanRegistrationsCode) {
|
String featureNamePrefix, BeanRegistrationsCode beanRegistrationsCode) {
|
||||||
|
|
||||||
BeanRegistrationCodeFragments codeFragments = getCodeFragments(beanRegistrationsCode);
|
BeanRegistrationCodeFragments codeFragments = getCodeFragments(beanRegistrationsCode, featureNamePrefix);
|
||||||
Class<?> target = codeFragments.getTarget(this.registeredBean,
|
Class<?> target = codeFragments.getTarget(this.registeredBean,
|
||||||
this.constructorOrFactoryMethod);
|
this.constructorOrFactoryMethod);
|
||||||
if (!target.getName().startsWith("java.")) {
|
if (!target.getName().startsWith("java.")) {
|
||||||
|
String featureName = featureNamePrefix + "BeanDefinitions";
|
||||||
GeneratedClass generatedClass = generationContext.getClassGenerator()
|
GeneratedClass generatedClass = generationContext.getClassGenerator()
|
||||||
.getOrGenerateClass(new BeanDefinitionsJavaFileGenerator(target),
|
.getOrGenerateClass(new BeanDefinitionsJavaFileGenerator(target),
|
||||||
target, "BeanDefinitions");
|
target, featureName);
|
||||||
MethodGenerator methodGenerator = generatedClass.getMethodGenerator()
|
MethodGenerator methodGenerator = generatedClass.getMethodGenerator()
|
||||||
.withName(getName());
|
.withName(getName());
|
||||||
GeneratedMethod generatedMethod = generateBeanDefinitionMethod(
|
GeneratedMethod generatedMethod = generateBeanDefinitionMethod(
|
||||||
|
|
@ -112,10 +114,11 @@ class BeanDefinitionMethodGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanRegistrationCodeFragments getCodeFragments(
|
private BeanRegistrationCodeFragments getCodeFragments(
|
||||||
BeanRegistrationsCode beanRegistrationsCode) {
|
BeanRegistrationsCode beanRegistrationsCode, String featureNamePrefix) {
|
||||||
|
|
||||||
BeanRegistrationCodeFragments codeFragments = new DefaultBeanRegistrationCodeFragments(
|
BeanRegistrationCodeFragments codeFragments = new DefaultBeanRegistrationCodeFragments(
|
||||||
beanRegistrationsCode, this.registeredBean, this.methodGeneratorFactory);
|
beanRegistrationsCode, this.registeredBean, this.methodGeneratorFactory,
|
||||||
|
featureNamePrefix);
|
||||||
for (BeanRegistrationAotContribution aotContribution : this.aotContributions) {
|
for (BeanRegistrationAotContribution aotContribution : this.aotContributions) {
|
||||||
codeFragments = aotContribution.customizeBeanRegistrationCodeFragments(codeFragments);
|
codeFragments = aotContribution.customizeBeanRegistrationCodeFragments(codeFragments);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@ class BeanDefinitionMethodGeneratorFactory {
|
||||||
* {@link BeanDefinitionMethodGenerator} will include all
|
* {@link BeanDefinitionMethodGenerator} will include all
|
||||||
* {@link BeanRegistrationAotProcessor} provided contributions.
|
* {@link BeanRegistrationAotProcessor} provided contributions.
|
||||||
* @param registeredBean the registered bean
|
* @param registeredBean the registered bean
|
||||||
|
* @param innerBeanPropertyName the inner bean property name or {@code null}
|
||||||
* @return a new {@link BeanDefinitionMethodGenerator} instance or
|
* @return a new {@link BeanDefinitionMethodGenerator} instance or
|
||||||
* {@code null}
|
* {@code null}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,10 @@ public interface BeanFactoryInitializationCode {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the ID of the bean factory or and empty string if no ID is avaialble.
|
* Return the name of the bean factory or and empty string if no ID is available.
|
||||||
* @return the bean factory ID
|
* @return the bean factory name
|
||||||
*/
|
*/
|
||||||
default String getId() {
|
default String getName() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,12 +63,13 @@ class BeanRegistrationsAotContribution
|
||||||
|
|
||||||
ClassName className = generationContext.getClassNameGenerator().generateClassName(
|
ClassName className = generationContext.getClassNameGenerator().generateClassName(
|
||||||
beanFactoryInitializationCode.getTarget(),
|
beanFactoryInitializationCode.getTarget(),
|
||||||
beanFactoryInitializationCode.getId() + "BeanFactoryRegistrations");
|
beanFactoryInitializationCode.getName() + "BeanFactoryRegistrations");
|
||||||
BeanRegistrationsCodeGenerator codeGenerator = new BeanRegistrationsCodeGenerator(
|
BeanRegistrationsCodeGenerator codeGenerator = new BeanRegistrationsCodeGenerator(
|
||||||
className);
|
className);
|
||||||
GeneratedMethod registerMethod = codeGenerator.getMethodGenerator()
|
GeneratedMethod registerMethod = codeGenerator.getMethodGenerator()
|
||||||
.generateMethod("registerBeanDefinitions")
|
.generateMethod("registerBeanDefinitions")
|
||||||
.using(builder -> generateRegisterMethod(builder, generationContext,
|
.using(builder -> generateRegisterMethod(builder, generationContext,
|
||||||
|
beanFactoryInitializationCode.getName(),
|
||||||
codeGenerator));
|
codeGenerator));
|
||||||
JavaFile javaFile = codeGenerator.generatedJavaFile(className);
|
JavaFile javaFile = codeGenerator.generatedJavaFile(className);
|
||||||
generationContext.getGeneratedFiles().addSourceFile(javaFile);
|
generationContext.getGeneratedFiles().addSourceFile(javaFile);
|
||||||
|
|
@ -77,7 +78,7 @@ class BeanRegistrationsAotContribution
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateRegisterMethod(MethodSpec.Builder builder,
|
private void generateRegisterMethod(MethodSpec.Builder builder,
|
||||||
GenerationContext generationContext,
|
GenerationContext generationContext, String featureNamePrefix,
|
||||||
BeanRegistrationsCode beanRegistrationsCode) {
|
BeanRegistrationsCode beanRegistrationsCode) {
|
||||||
|
|
||||||
builder.addJavadoc("Register the bean definitions.");
|
builder.addJavadoc("Register the bean definitions.");
|
||||||
|
|
@ -87,7 +88,7 @@ class BeanRegistrationsAotContribution
|
||||||
CodeBlock.Builder code = CodeBlock.builder();
|
CodeBlock.Builder code = CodeBlock.builder();
|
||||||
this.registrations.forEach((beanName, beanDefinitionMethodGenerator) -> {
|
this.registrations.forEach((beanName, beanDefinitionMethodGenerator) -> {
|
||||||
MethodReference beanDefinitionMethod = beanDefinitionMethodGenerator
|
MethodReference beanDefinitionMethod = beanDefinitionMethodGenerator
|
||||||
.generateBeanDefinitionMethod(generationContext,
|
.generateBeanDefinitionMethod(generationContext, featureNamePrefix,
|
||||||
beanRegistrationsCode);
|
beanRegistrationsCode);
|
||||||
code.addStatement("$L.registerBeanDefinition($S, $L)",
|
code.addStatement("$L.registerBeanDefinition($S, $L)",
|
||||||
BEAN_FACTORY_PARAMETER_NAME, beanName,
|
BEAN_FACTORY_PARAMETER_NAME, beanName,
|
||||||
|
|
|
||||||
|
|
@ -54,14 +54,18 @@ class DefaultBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments
|
||||||
|
|
||||||
private final BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory;
|
private final BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory;
|
||||||
|
|
||||||
|
private final String featureNamePrefix;
|
||||||
|
|
||||||
|
|
||||||
DefaultBeanRegistrationCodeFragments(BeanRegistrationsCode beanRegistrationsCode,
|
DefaultBeanRegistrationCodeFragments(BeanRegistrationsCode beanRegistrationsCode,
|
||||||
RegisteredBean registeredBean,
|
RegisteredBean registeredBean,
|
||||||
BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory) {
|
BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory,
|
||||||
|
String featureNamePrefix) {
|
||||||
|
|
||||||
this.beanRegistrationsCode = beanRegistrationsCode;
|
this.beanRegistrationsCode = beanRegistrationsCode;
|
||||||
this.registeredBean = registeredBean;
|
this.registeredBean = registeredBean;
|
||||||
this.beanDefinitionMethodGeneratorFactory = beanDefinitionMethodGeneratorFactory;
|
this.beanDefinitionMethodGeneratorFactory = beanDefinitionMethodGeneratorFactory;
|
||||||
|
this.featureNamePrefix = featureNamePrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -120,7 +124,7 @@ class DefaultBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments
|
||||||
.getBeanDefinitionMethodGenerator(innerRegisteredBean, name);
|
.getBeanDefinitionMethodGenerator(innerRegisteredBean, name);
|
||||||
Assert.state(methodGenerator != null, "Unexpected filtering of inner-bean");
|
Assert.state(methodGenerator != null, "Unexpected filtering of inner-bean");
|
||||||
MethodReference generatedMethod = methodGenerator
|
MethodReference generatedMethod = methodGenerator
|
||||||
.generateBeanDefinitionMethod(generationContext,
|
.generateBeanDefinitionMethod(generationContext, this.featureNamePrefix,
|
||||||
this.beanRegistrationsCode);
|
this.beanRegistrationsCode);
|
||||||
return generatedMethod.toInvokeCodeBlock();
|
return generatedMethod.toInvokeCodeBlock();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, registeredBean, null,
|
this.methodGeneratorFactory, registeredBean, null,
|
||||||
Collections.emptyList());
|
Collections.emptyList());
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
||||||
assertThat(sourceFile).contains("Get the bean definition for 'testBean'");
|
assertThat(sourceFile).contains("Get the bean definition for 'testBean'");
|
||||||
|
|
@ -116,7 +116,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, registeredBean, null,
|
this.methodGeneratorFactory, registeredBean, null,
|
||||||
Collections.emptyList());
|
Collections.emptyList());
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
assertThat(actual.getResolvableType().resolve()).isEqualTo(GenericBean.class);
|
assertThat(actual.getResolvableType().resolve()).isEqualTo(GenericBean.class);
|
||||||
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
||||||
|
|
@ -149,7 +149,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
||||||
this.methodGeneratorFactory, registeredBean, null, aotContributions);
|
this.methodGeneratorFactory, registeredBean, null, aotContributions);
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
|
assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
|
||||||
InstanceSupplier<?> supplier = (InstanceSupplier<?>) actual
|
InstanceSupplier<?> supplier = (InstanceSupplier<?>) actual
|
||||||
|
|
@ -175,7 +175,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
||||||
this.methodGeneratorFactory, registeredBean, null, aotContributions);
|
this.methodGeneratorFactory, registeredBean, null, aotContributions);
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
|
assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
|
||||||
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
||||||
|
|
@ -215,7 +215,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, registeredBean, null,
|
this.methodGeneratorFactory, registeredBean, null,
|
||||||
aotContributions);
|
aotContributions);
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
assertThat(actual.getAttribute("a")).isEqualTo("A");
|
assertThat(actual.getAttribute("a")).isEqualTo("A");
|
||||||
assertThat(actual.getAttribute("b")).isNull();
|
assertThat(actual.getAttribute("b")).isNull();
|
||||||
|
|
@ -248,7 +248,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, innerBean, "testInnerBean",
|
this.methodGeneratorFactory, innerBean, "testInnerBean",
|
||||||
Collections.emptyList());
|
Collections.emptyList());
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
assertThat(compiled.getSourceFile(".*BeanDefinitions"))
|
assertThat(compiled.getSourceFile(".*BeanDefinitions"))
|
||||||
.contains("Get the inner-bean definition for 'testInnerBean'");
|
.contains("Get the inner-bean definition for 'testInnerBean'");
|
||||||
|
|
@ -269,7 +269,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, registeredBean, null,
|
this.methodGeneratorFactory, registeredBean, null,
|
||||||
Collections.emptyList());
|
Collections.emptyList());
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual
|
RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual
|
||||||
.getPropertyValues().get("name");
|
.getPropertyValues().get("name");
|
||||||
|
|
@ -303,7 +303,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, registeredBean, null,
|
this.methodGeneratorFactory, registeredBean, null,
|
||||||
Collections.emptyList());
|
Collections.emptyList());
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual
|
RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual
|
||||||
.getConstructorArgumentValues()
|
.getConstructorArgumentValues()
|
||||||
|
|
@ -336,7 +336,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
||||||
this.methodGeneratorFactory, registeredBean, null, aotContributions);
|
this.methodGeneratorFactory, registeredBean, null, aotContributions);
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, (actual, compiled) -> {
|
testCompiledResult(method, (actual, compiled) -> {
|
||||||
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
|
||||||
assertThat(sourceFile).contains("AotContributedMethod()");
|
assertThat(sourceFile).contains("AotContributedMethod()");
|
||||||
|
|
@ -353,7 +353,7 @@ class BeanDefinitionMethodGeneratorTests {
|
||||||
this.methodGeneratorFactory, registeredBean, null,
|
this.methodGeneratorFactory, registeredBean, null,
|
||||||
Collections.emptyList());
|
Collections.emptyList());
|
||||||
MethodReference method = generator.generateBeanDefinitionMethod(
|
MethodReference method = generator.generateBeanDefinitionMethod(
|
||||||
this.generationContext, this.beanRegistrationsCode);
|
this.generationContext, "", this.beanRegistrationsCode);
|
||||||
testCompiledResult(method, false, (actual, compiled) -> {
|
testCompiledResult(method, false, (actual, compiled) -> {
|
||||||
DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory();
|
||||||
freshBeanFactory.registerBeanDefinition("test", actual);
|
freshBeanFactory.registerBeanDefinition("test", actual);
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ import org.springframework.aot.generate.MethodGenerator;
|
||||||
import org.springframework.aot.generate.MethodReference;
|
import org.springframework.aot.generate.MethodReference;
|
||||||
import org.springframework.aot.test.generator.compile.Compiled;
|
import org.springframework.aot.test.generator.compile.Compiled;
|
||||||
import org.springframework.aot.test.generator.compile.TestCompiler;
|
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.DefaultListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.RegisteredBean;
|
import org.springframework.beans.factory.support.RegisteredBean;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
|
@ -67,7 +68,7 @@ class BeanRegistrationsAotContributionTests {
|
||||||
|
|
||||||
private BeanDefinitionMethodGeneratorFactory methodGeneratorFactory;
|
private BeanDefinitionMethodGeneratorFactory methodGeneratorFactory;
|
||||||
|
|
||||||
private final MockBeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode();
|
private MockBeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode();
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
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
|
@Test
|
||||||
void applyToCallsRegistrationsWithBeanRegistrationsCode() {
|
void applyToCallsRegistrationsWithBeanRegistrationsCode() {
|
||||||
List<BeanRegistrationsCode> beanRegistrationsCodes = new ArrayList<>();
|
List<BeanRegistrationsCode> beanRegistrationsCodes = new ArrayList<>();
|
||||||
|
|
@ -110,11 +130,11 @@ class BeanRegistrationsAotContributionTests {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
MethodReference generateBeanDefinitionMethod(
|
MethodReference generateBeanDefinitionMethod(
|
||||||
GenerationContext generationContext,
|
GenerationContext generationContext, String featureNamePrefix,
|
||||||
BeanRegistrationsCode beanRegistrationsCode) {
|
BeanRegistrationsCode beanRegistrationsCode) {
|
||||||
beanRegistrationsCodes.add(beanRegistrationsCode);
|
beanRegistrationsCodes.add(beanRegistrationsCode);
|
||||||
return super.generateBeanDefinitionMethod(generationContext,
|
return super.generateBeanDefinitionMethod(generationContext,
|
||||||
beanRegistrationsCode);
|
featureNamePrefix, beanRegistrationsCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
@ -163,6 +183,21 @@ class BeanRegistrationsAotContributionTests {
|
||||||
|
|
||||||
private final List<MethodReference> initializers = new ArrayList<>();
|
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
|
@Override
|
||||||
public MethodGenerator getMethodGenerator() {
|
public MethodGenerator getMethodGenerator() {
|
||||||
return this.generatedMethods;
|
return this.generatedMethods;
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ public class ApplicationContextAotGenerator {
|
||||||
GenerationContext generationContext,
|
GenerationContext generationContext,
|
||||||
ClassName generatedInitializerClassName) {
|
ClassName generatedInitializerClassName) {
|
||||||
|
|
||||||
generateApplicationContext(applicationContext, null, generationContext,
|
generateApplicationContext(applicationContext, null, null, generationContext,
|
||||||
generatedInitializerClassName);
|
generatedInitializerClassName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,20 +58,21 @@ public class ApplicationContextAotGenerator {
|
||||||
* necessary code to restore the state of its {@link BeanFactory}, using the
|
* necessary code to restore the state of its {@link BeanFactory}, using the
|
||||||
* specified {@link GenerationContext}.
|
* specified {@link GenerationContext}.
|
||||||
* @param applicationContext the application context to handle
|
* @param applicationContext the application context to handle
|
||||||
* @param target the target class for the generated initializer
|
* @param target the target class for the generated initializer (used when generating class names)
|
||||||
|
* @param name the name of the application context (used when generating class names)
|
||||||
* @param generationContext the generation context to use
|
* @param generationContext the generation context to use
|
||||||
* @param generatedInitializerClassName the class name to use for the
|
* @param generatedInitializerClassName the class name to use for the
|
||||||
* generated application context initializer
|
* generated application context initializer
|
||||||
*/
|
*/
|
||||||
public void generateApplicationContext(GenericApplicationContext applicationContext,
|
public void generateApplicationContext(GenericApplicationContext applicationContext,
|
||||||
@Nullable Class<?> target, GenerationContext generationContext,
|
@Nullable Class<?> target, @Nullable String name, GenerationContext generationContext,
|
||||||
ClassName generatedInitializerClassName) {
|
ClassName generatedInitializerClassName) {
|
||||||
|
|
||||||
applicationContext.refreshForAotProcessing();
|
applicationContext.refreshForAotProcessing();
|
||||||
DefaultListableBeanFactory beanFactory = applicationContext
|
DefaultListableBeanFactory beanFactory = applicationContext
|
||||||
.getDefaultListableBeanFactory();
|
.getDefaultListableBeanFactory();
|
||||||
ApplicationContextInitializationCodeGenerator codeGenerator = new ApplicationContextInitializationCodeGenerator(
|
ApplicationContextInitializationCodeGenerator codeGenerator = new ApplicationContextInitializationCodeGenerator(
|
||||||
target, applicationContext.getId());
|
target, name);
|
||||||
new BeanFactoryInitializationAotContributions(beanFactory).applyTo(generationContext,
|
new BeanFactoryInitializationAotContributions(beanFactory).applyTo(generationContext,
|
||||||
codeGenerator);
|
codeGenerator);
|
||||||
JavaFile javaFile = codeGenerator.generateJavaFile(generatedInitializerClassName);
|
JavaFile javaFile = codeGenerator.generateJavaFile(generatedInitializerClassName);
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ import org.springframework.javapoet.JavaFile;
|
||||||
import org.springframework.javapoet.MethodSpec;
|
import org.springframework.javapoet.MethodSpec;
|
||||||
import org.springframework.javapoet.ParameterizedTypeName;
|
import org.springframework.javapoet.ParameterizedTypeName;
|
||||||
import org.springframework.javapoet.TypeSpec;
|
import org.springframework.javapoet.TypeSpec;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -51,16 +52,16 @@ class ApplicationContextInitializationCodeGenerator
|
||||||
|
|
||||||
private final Class<?> target;
|
private final Class<?> target;
|
||||||
|
|
||||||
private final String id;
|
private final String name;
|
||||||
|
|
||||||
private final GeneratedMethods generatedMethods = new GeneratedMethods();
|
private final GeneratedMethods generatedMethods = new GeneratedMethods();
|
||||||
|
|
||||||
private final List<MethodReference> initializers = new ArrayList<>();
|
private final List<MethodReference> initializers = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
ApplicationContextInitializationCodeGenerator(Class<?> target, String id) {
|
ApplicationContextInitializationCodeGenerator(Class<?> target, @Nullable String name) {
|
||||||
this.target=target;
|
this.target=target;
|
||||||
this.id = (!StringUtils.hasText(id)) ? "" : id;
|
this.name = (!StringUtils.hasText(name)) ? "" : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -70,8 +71,8 @@ class ApplicationContextInitializationCodeGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getId() {
|
public String getName() {
|
||||||
return this.id;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue