Adapt to Spring AOT changes

This commit is contained in:
Stephane Nicoll 2022-09-20 17:06:52 +02:00 committed by Brian Clozel
parent d478a020df
commit 3702c71fc1
11 changed files with 29 additions and 30 deletions

View File

@ -27,8 +27,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.aot.AotDetector; import org.springframework.aot.AotDetector;
import org.springframework.aot.test.generate.TestGenerationContext; import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess;
import org.springframework.aot.test.generate.compile.TestCompiler;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
@ -43,6 +41,8 @@ import org.springframework.boot.web.servlet.context.ServletWebServerApplicationC
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.aot.ApplicationContextAotGenerator; import org.springframework.context.aot.ApplicationContextAotGenerator;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName; import org.springframework.javapoet.ClassName;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -65,7 +65,7 @@ class ChildManagementContextInitializerAotTests {
} }
@Test @Test
@CompileWithTargetClassAccess @CompileWithForkedClassLoader
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void aotContributedInitializerStartsManagementContext(CapturedOutput output) { void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
@ -80,7 +80,7 @@ class ChildManagementContextInitializerAotTests {
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext); (GenericApplicationContext) context.getSourceApplicationContext(), generationContext);
generationContext.writeGeneratedContent(); generationContext.writeGeneratedContent();
TestCompiler compiler = TestCompiler.forSystem(); TestCompiler compiler = TestCompiler.forSystem();
compiler.withFiles(generationContext.getGeneratedFiles()).compile((compiled) -> { compiler.with(generationContext).compile((compiled) -> {
ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext(); ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext();
TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext); TestPropertyValues.of("server.port=0", "management.server.port=0").applyTo(freshApplicationContext);
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled ApplicationContextInitializer<GenericApplicationContext> initializer = compiled

View File

@ -22,8 +22,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.aot.AotDetector; import org.springframework.aot.AotDetector;
import org.springframework.aot.generate.InMemoryGeneratedFiles; import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess; import org.springframework.aot.test.generate.CompilerFiles;
import org.springframework.aot.test.generate.compile.TestCompiler;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.SpringBootConfiguration;
@ -32,6 +31,8 @@ import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.test.context.BootstrapUtils; import org.springframework.test.context.BootstrapUtils;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextBootstrapper; import org.springframework.test.context.TestContextBootstrapper;
@ -49,7 +50,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
@CompileWithTargetClassAccess @CompileWithForkedClassLoader
class SpringBootContextLoaderAotTests { class SpringBootContextLoaderAotTests {
@Test @Test
@ -58,7 +59,7 @@ class SpringBootContextLoaderAotTests {
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
Class<?> testClass = ExampleTest.class; Class<?> testClass = ExampleTest.class;
generator.processAheadOfTime(Stream.of(testClass)); generator.processAheadOfTime(Stream.of(testClass));
TestCompiler.forSystem().withFiles(generatedFiles).printFiles(System.out) TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles))
.compile(ThrowingConsumer.of((compiled) -> assertCompiledTest(testClass))); .compile(ThrowingConsumer.of((compiled) -> assertCompiledTest(testClass)));
} }

View File

@ -125,7 +125,7 @@ public class AotProcessor {
private void performAotProcessing(GenericApplicationContext applicationContext) { private void performAotProcessing(GenericApplicationContext applicationContext) {
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot); FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot);
DefaultGenerationContext generationContext = new DefaultGenerationContext( DefaultGenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(this.application), generatedFiles); new ClassNameGenerator(ClassName.get(this.application)), generatedFiles);
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator(); ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
ClassName generatedInitializerClassName = generator.processAheadOfTime(applicationContext, generationContext); ClassName generatedInitializerClassName = generator.processAheadOfTime(applicationContext, generationContext);
registerEntryPointHint(generationContext, generatedInitializerClassName); registerEntryPointHint(generationContext, generatedInitializerClassName);

View File

@ -28,6 +28,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode; import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments; import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments;
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsDecorator;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.InstanceSupplier; import org.springframework.beans.factory.support.InstanceSupplier;
import org.springframework.beans.factory.support.RegisteredBean; import org.springframework.beans.factory.support.RegisteredBean;
@ -48,7 +49,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessor implements BeanRegistr
if (!isImmutableConfigurationPropertiesBeanDefinition(registeredBean.getMergedBeanDefinition())) { if (!isImmutableConfigurationPropertiesBeanDefinition(registeredBean.getMergedBeanDefinition())) {
return null; return null;
} }
return BeanRegistrationAotContribution.ofBeanRegistrationCodeFragmentsCustomizer( return BeanRegistrationAotContribution.withCustomCodeFragments(
(codeFragments) -> new ConfigurationPropertiesBeanRegistrationCodeFragments(codeFragments, (codeFragments) -> new ConfigurationPropertiesBeanRegistrationCodeFragments(codeFragments,
registeredBean)); registeredBean));
@ -59,7 +60,8 @@ class ConfigurationPropertiesBeanRegistrationAotProcessor implements BeanRegistr
&& BindMethod.VALUE_OBJECT.equals(beanDefinition.getAttribute(BindMethod.class.getName())); && BindMethod.VALUE_OBJECT.equals(beanDefinition.getAttribute(BindMethod.class.getName()));
} }
private static class ConfigurationPropertiesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments { private static class ConfigurationPropertiesBeanRegistrationCodeFragments
extends BeanRegistrationCodeFragmentsDecorator {
private static final Predicate<String> INCLUDE_BIND_METHOD_ATTRIBUTE_FILTER = (name) -> name private static final Predicate<String> INCLUDE_BIND_METHOD_ATTRIBUTE_FILTER = (name) -> name
.equals(BindMethod.class.getName()); .equals(BindMethod.class.getName());

View File

@ -110,9 +110,8 @@ class AotProcessorTests {
assertThat(directory.resolve( assertThat(directory.resolve(
"source/org/springframework/boot/AotProcessorTests_SampleApplication__ApplicationContextInitializer.java")) "source/org/springframework/boot/AotProcessorTests_SampleApplication__ApplicationContextInitializer.java"))
.exists().isRegularFile(); .exists().isRegularFile();
assertThat(directory.resolve( assertThat(directory.resolve("source/org/springframework/boot/AotProcessorTests__BeanDefinitions.java"))
"source/org/springframework/boot/AotProcessorTests_SampleApplication__BeanDefinitions.java")) .exists().isRegularFile();
.exists().isRegularFile();
assertThat(directory.resolve( assertThat(directory.resolve(
"source/org/springframework/boot/AotProcessorTests_SampleApplication__BeanFactoryRegistrations.java")) "source/org/springframework/boot/AotProcessorTests_SampleApplication__BeanFactoryRegistrations.java"))
.exists().isRegularFile(); .exists().isRegularFile();

View File

@ -33,7 +33,7 @@ import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.mock.MockSpringFactoriesLoader; import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
import org.springframework.mock.env.MockPropertySource; import org.springframework.mock.env.MockPropertySource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;

View File

@ -37,7 +37,7 @@ import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.mock.MockSpringFactoriesLoader; import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@ -29,7 +29,7 @@ import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.env.PropertySourceLoader; import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.mock.MockSpringFactoriesLoader; import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;

View File

@ -26,15 +26,13 @@ import java.util.function.Consumer;
import org.junit.jupiter.api.Test; 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.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.ExecutableHint; import org.springframework.aot.hint.ExecutableHint;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint; import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.aot.AotServices; import org.springframework.beans.factory.aot.AotServices;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
@ -278,8 +276,7 @@ class ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests {
private RuntimeHints process(ConfigurableListableBeanFactory beanFactory) { private RuntimeHints process(ConfigurableListableBeanFactory beanFactory) {
BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory); BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory);
assertThat(contribution).isNotNull(); assertThat(contribution).isNotNull();
GenerationContext generationContext = new DefaultGenerationContext(new ClassNameGenerator(Object.class), GenerationContext generationContext = new TestGenerationContext();
new InMemoryGeneratedFiles());
contribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class)); contribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class));
return generationContext.getRuntimeHints(); return generationContext.getRuntimeHints();
} }

View File

@ -22,8 +22,6 @@ import java.util.function.Consumer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.aot.test.generate.TestGenerationContext; import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.aot.test.generate.compile.CompileWithTargetClassAccess;
import org.springframework.aot.test.generate.compile.TestCompiler;
import org.springframework.beans.factory.aot.AotServices; import org.springframework.beans.factory.aot.AotServices;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
@ -38,6 +36,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.aot.ApplicationContextAotGenerator; import org.springframework.context.aot.ApplicationContextAotGenerator;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName; import org.springframework.javapoet.ClassName;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
@ -90,7 +90,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
} }
@Test @Test
@CompileWithTargetClassAccess @CompileWithForkedClassLoader
void aotContributedInitializerBindsValueObject() { void aotContributedInitializerBindsValueObject() {
compile(createContext(ValueObjectSampleBeanConfiguration.class), (freshContext) -> { compile(createContext(ValueObjectSampleBeanConfiguration.class), (freshContext) -> {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "test.name=Hello"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "test.name=Hello");
@ -101,7 +101,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
} }
@Test @Test
@CompileWithTargetClassAccess @CompileWithForkedClassLoader
void aotContributedInitializerBindsJavaBean() { void aotContributedInitializerBindsJavaBean() {
compile(createContext(JavaBeanSampleBeanConfiguration.class), (freshContext) -> { compile(createContext(JavaBeanSampleBeanConfiguration.class), (freshContext) -> {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "test.name=Hello"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "test.name=Hello");
@ -112,7 +112,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
} }
@Test @Test
@CompileWithTargetClassAccess @CompileWithForkedClassLoader
void aotContributedInitializerBindsScannedValueObject() { void aotContributedInitializerBindsScannedValueObject() {
compile(createContext(ScanTestConfiguration.class), (freshContext) -> { compile(createContext(ScanTestConfiguration.class), (freshContext) -> {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "b.first.name=Hello"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "b.first.name=Hello");
@ -123,7 +123,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
} }
@Test @Test
@CompileWithTargetClassAccess @CompileWithForkedClassLoader
void aotContributedInitializerBindsScannedJavaBean() { void aotContributedInitializerBindsScannedJavaBean() {
compile(createContext(ScanTestConfiguration.class), (freshContext) -> { compile(createContext(ScanTestConfiguration.class), (freshContext) -> {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "b.second.number=42"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(freshContext, "b.second.number=42");
@ -145,7 +145,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessorTests {
TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class); TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class);
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext); ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
generationContext.writeGeneratedContent(); generationContext.writeGeneratedContent();
TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile((compiled) -> { TestCompiler.forSystem().with(generationContext).compile((compiled) -> {
GenericApplicationContext freshApplicationContext = new GenericApplicationContext(); GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
.getInstance(ApplicationContextInitializer.class, className.toString()); .getInstance(ApplicationContextInitializer.class, className.toString());

View File

@ -27,7 +27,7 @@ import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.mock.MockSpringFactoriesLoader; import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.same; import static org.mockito.ArgumentMatchers.same;