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

@@ -49,7 +49,7 @@ public class ApplicationContextAotGenerator {
GenerationContext generationContext,
ClassName generatedInitializerClassName) {
generateApplicationContext(applicationContext, null, generationContext,
generateApplicationContext(applicationContext, null, null, generationContext,
generatedInitializerClassName);
}
@@ -58,20 +58,21 @@ public class ApplicationContextAotGenerator {
* necessary code to restore the state of its {@link BeanFactory}, using the
* specified {@link GenerationContext}.
* @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 generatedInitializerClassName the class name to use for the
* generated application context initializer
*/
public void generateApplicationContext(GenericApplicationContext applicationContext,
@Nullable Class<?> target, GenerationContext generationContext,
@Nullable Class<?> target, @Nullable String name, GenerationContext generationContext,
ClassName generatedInitializerClassName) {
applicationContext.refreshForAotProcessing();
DefaultListableBeanFactory beanFactory = applicationContext
.getDefaultListableBeanFactory();
ApplicationContextInitializationCodeGenerator codeGenerator = new ApplicationContextInitializationCodeGenerator(
target, applicationContext.getId());
target, name);
new BeanFactoryInitializationAotContributions(beanFactory).applyTo(generationContext,
codeGenerator);
JavaFile javaFile = codeGenerator.generateJavaFile(generatedInitializerClassName);

View File

@@ -35,6 +35,7 @@ import org.springframework.javapoet.JavaFile;
import org.springframework.javapoet.MethodSpec;
import org.springframework.javapoet.ParameterizedTypeName;
import org.springframework.javapoet.TypeSpec;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
@@ -51,16 +52,16 @@ class ApplicationContextInitializationCodeGenerator
private final Class<?> target;
private final String id;
private final String name;
private final GeneratedMethods generatedMethods = new GeneratedMethods();
private final List<MethodReference> initializers = new ArrayList<>();
ApplicationContextInitializationCodeGenerator(Class<?> target, String id) {
ApplicationContextInitializationCodeGenerator(Class<?> target, @Nullable String name) {
this.target=target;
this.id = (!StringUtils.hasText(id)) ? "" : id;
this.name = (!StringUtils.hasText(name)) ? "" : name;
}
@@ -70,8 +71,8 @@ class ApplicationContextInitializationCodeGenerator
}
@Override
public String getId() {
return this.id;
public String getName() {
return this.name;
}
@Override