From fe39598e81e01710f709b001342b62606422fb5b Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 22 Jun 2022 17:57:57 +0200 Subject: [PATCH] Adapt to latest API change in Spring Framework See https://github.com/spring-projects/spring-framework/issues/28585 --- .../ChildManagementContextInitializer.java | 17 +++----- ...dManagementContextInitializerAotTests.java | 28 +++---------- .../springframework/boot/AotProcessor.java | 42 ++++--------------- ...actoryInitializationAotProcessorTests.java | 4 +- 4 files changed, 21 insertions(+), 70 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java index 054109f391..bb98fa9630 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java @@ -30,7 +30,6 @@ import org.springframework.beans.factory.aot.BeanRegistrationCode; import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.support.RegisteredBean; -import org.springframework.boot.AotProcessor; import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; @@ -96,10 +95,9 @@ class ChildManagementContextInitializer implements ApplicationListener target = (this.activeAotProcessor != null) ? this.activeAotProcessor.getApplication() : null; - ClassName generatedInitializerClassName = generationContext.getClassNameGenerator() - .generateClassName(target, "ManagementContextRegistrations"); - new ApplicationContextAotGenerator().generateApplicationContext(this.managementContext, target, - "Management", generationContext, generatedInitializerClassName); + GenerationContext managementGenerationContext = generationContext.withName("Management"); + ClassName generatedInitializerClassName = new ApplicationContextAotGenerator() + .generateApplicationContext(this.managementContext, managementGenerationContext); GeneratedMethod postProcessorMethod = beanRegistrationCode.getMethodGenerator() .generateMethod("addManagementInitializer").using((builder) -> { builder.addJavadoc("Use AOT management context initialization"); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java index 8b4e784145..ce8a6bb79e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java @@ -25,13 +25,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.aot.generate.ClassNameGenerator; import org.springframework.aot.generate.DefaultGenerationContext; import org.springframework.aot.generate.InMemoryGeneratedFiles; -import org.springframework.aot.generate.MethodGenerator; -import org.springframework.aot.generate.MethodReference; import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess; import org.springframework.aot.test.generator.compile.TestCompiler; -import org.springframework.beans.factory.aot.BeanRegistrationCode; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration; @@ -79,10 +77,10 @@ class ChildManagementContextInitializerAotTests { EndpointAutoConfiguration.class)); contextRunner.withPropertyValues("server.port=0", "management.server.port=0").prepare((context) -> { InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); - DefaultGenerationContext generationContext = new DefaultGenerationContext(generatedFiles); - ClassName className = ClassName.get("com.example", "TestInitializer"); - new ApplicationContextAotGenerator().generateApplicationContext( - (GenericApplicationContext) context.getSourceApplicationContext(), generationContext, className); + DefaultGenerationContext generationContext = new DefaultGenerationContext( + new ClassNameGenerator(TestTarget.class), generatedFiles); + ClassName className = new ApplicationContextAotGenerator().generateApplicationContext( + (GenericApplicationContext) context.getSourceApplicationContext(), generationContext); generationContext.writeGeneratedContent(); TestCompiler compiler = TestCompiler.forSystem(); compiler.withFiles(generatedFiles).compile((compiled) -> { @@ -105,21 +103,7 @@ class ChildManagementContextInitializerAotTests { }; } - static class MockBeanRegistrationCode implements BeanRegistrationCode { - - @Override - public ClassName getClassName() { - return null; - } - - @Override - public MethodGenerator getMethodGenerator() { - return null; - } - - @Override - public void addInstancePostProcessor(MethodReference methodReference) { - } + static class TestTarget { } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java index 183dd6a9c9..11757bb373 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java @@ -25,10 +25,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; +import org.springframework.aot.generate.ClassNameGenerator; import org.springframework.aot.generate.DefaultGenerationContext; import org.springframework.aot.generate.FileSystemGeneratedFiles; import org.springframework.aot.generate.GeneratedFiles.Kind; @@ -62,8 +61,6 @@ public class AotProcessor { private static final Consumer INVOKE_CONSTRUCTOR_HINT = (hint) -> hint .setModes(ExecutableMode.INVOKE); - private static final Map aotProcessors = new ConcurrentHashMap<>(); - private final Class application; private final String[] applicationArgs; @@ -101,31 +98,17 @@ public class AotProcessor { this.artifactId = artifactId; } - /** - * Return the application class being processed. - * @return the application class - */ - public Class getApplication() { - return this.application; - } - /** * Trigger the processing of the application managed by this instance. */ - void process() { + public void process() { deleteExistingOutput(); AotProcessorHook hook = new AotProcessorHook(); SpringApplicationHooks.withHook(hook, this::callApplicationMainMethod); GenericApplicationContext applicationContext = hook.getApplicationContext(); Assert.notNull(applicationContext, "No application context available after calling main method of '" + this.application.getName() + "'. Does it run a SpringApplication?"); - aotProcessors.put(applicationContext, this); - try { - performAotProcessing(applicationContext); - } - finally { - aotProcessors.remove(applicationContext); - } + performAotProcessing(applicationContext); } private void deleteExistingOutput() { @@ -161,12 +144,11 @@ public class AotProcessor { private void performAotProcessing(GenericApplicationContext applicationContext) { FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot); - DefaultGenerationContext generationContext = new DefaultGenerationContext(generatedFiles); + DefaultGenerationContext generationContext = new DefaultGenerationContext( + new ClassNameGenerator(this.application), generatedFiles); ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator(); - ClassName generatedInitializerClassName = generationContext.getClassNameGenerator() - .generateClassName(this.application, "ApplicationContextInitializer"); - generator.generateApplicationContext(applicationContext, this.application, "", generationContext, - generatedInitializerClassName); + ClassName generatedInitializerClassName = generator.generateApplicationContext(applicationContext, + generationContext); registerEntryPointHint(generationContext, generatedInitializerClassName); generationContext.writeGeneratedContent(); writeHints(generationContext.getRuntimeHints()); @@ -243,16 +225,6 @@ public class AotProcessor { .process(); } - /** - * Return the AOT processor that is actively processing the given - * {@link ApplicationContext}. - * @param applicationContext the application context to check - * @return the {@link AotProcessor} or {@code null} - */ - public static AotProcessor getActive(ApplicationContext applicationContext) { - return aotProcessors.get(applicationContext); - } - /** * Hook used to capture the {@link ApplicationContext} and trigger early exit of main * method. diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests.java index 1751355081..5bbabde9ec 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests.java @@ -25,6 +25,7 @@ import java.util.function.Consumer; import org.junit.jupiter.api.Test; +import org.springframework.aot.generate.ClassNameGenerator; import org.springframework.aot.generate.DefaultGenerationContext; import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.InMemoryGeneratedFiles; @@ -267,7 +268,8 @@ class ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests { private RuntimeHints process(ConfigurableListableBeanFactory beanFactory) { BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory); assertThat(contribution).isNotNull(); - GenerationContext generationContext = new DefaultGenerationContext(new InMemoryGeneratedFiles()); + GenerationContext generationContext = new DefaultGenerationContext(new ClassNameGenerator(Object.class), + new InMemoryGeneratedFiles()); contribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class)); return generationContext.getRuntimeHints(); }