Add support for ImportAware callback

This commit adds a way for a BeanFactoryPostProcessor to participate to
AOT optimizations by contributing code that replaces its runtime
behaviour.

ConfigurationClassPostProcessor does implement this new interface and
computes a mapping of the ImportAware configuration classes. The mapping
is generated for latter reuse by ImportAwareAotBeanPostProcessor.

Closes gh-2811
This commit is contained in:
Stephane Nicoll
2022-03-06 17:44:42 +01:00
parent ec6a19fc6b
commit 9ba927215e
7 changed files with 467 additions and 2 deletions

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.core.type.AnnotationMetadata;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link ImportAwareAotBeanPostProcessor}.
*
* @author Stephane Nicoll
*/
class ImportAwareAotBeanPostProcessorTests {
@Test
void postProcessOnMatchingCandidate() {
ImportAwareAotBeanPostProcessor postProcessor = new ImportAwareAotBeanPostProcessor(
Map.of(TestImportAware.class.getName(), ImportAwareAotBeanPostProcessorTests.class.getName()));
TestImportAware importAware = new TestImportAware();
postProcessor.postProcessBeforeInitialization(importAware, "test");
assertThat(importAware.importMetadata).isNotNull();
assertThat(importAware.importMetadata.getClassName())
.isEqualTo(ImportAwareAotBeanPostProcessorTests.class.getName());
}
@Test
void postProcessOnMatchingCandidateWithNestedClass() {
ImportAwareAotBeanPostProcessor postProcessor = new ImportAwareAotBeanPostProcessor(
Map.of(TestImportAware.class.getName(), TestImporting.class.getName()));
TestImportAware importAware = new TestImportAware();
postProcessor.postProcessBeforeInitialization(importAware, "test");
assertThat(importAware.importMetadata).isNotNull();
assertThat(importAware.importMetadata.getClassName())
.isEqualTo(TestImporting.class.getName());
}
@Test
void postProcessOnNoCandidateDoesNotInvokeCallback() {
ImportAwareAotBeanPostProcessor postProcessor = new ImportAwareAotBeanPostProcessor(
Map.of(String.class.getName(), ImportAwareAotBeanPostProcessorTests.class.getName()));
TestImportAware importAware = new TestImportAware();
postProcessor.postProcessBeforeInitialization(importAware, "test");
assertThat(importAware.importMetadata).isNull();
}
@Test
void postProcessOnMatchingCandidateWithNoMetadata() {
ImportAwareAotBeanPostProcessor postProcessor = new ImportAwareAotBeanPostProcessor(
Map.of(TestImportAware.class.getName(), "com.example.invalid.DoesNotExist"));
TestImportAware importAware = new TestImportAware();
assertThatIllegalStateException().isThrownBy(() -> postProcessor.postProcessBeforeInitialization(importAware, "test"))
.withMessageContaining("Failed to read metadata for 'com.example.invalid.DoesNotExist'");
}
static class TestImportAware implements ImportAware {
private AnnotationMetadata importMetadata;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.importMetadata = importMetadata;
}
}
static class TestImporting {
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation;
import java.io.IOException;
import java.io.StringWriter;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generator.DefaultGeneratedTypeContext;
import org.springframework.aot.generator.GeneratedType;
import org.springframework.aot.generator.GeneratedTypeContext;
import org.springframework.beans.factory.generator.BeanFactoryContribution;
import org.springframework.beans.factory.generator.BeanFactoryInitialization;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.factory.generator.SimpleConfiguration;
import org.springframework.context.testfixture.context.generator.annotation.ImportConfiguration;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.support.CodeSnippet;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@code ImportAwareBeanFactoryConfiguration}.
*
* @author Stephane Nicoll
*/
public class ImportAwareBeanFactoryContributionTests {
@Test
void contributeWithImportAwareConfigurationRegistersBeanPostProcessor() {
BeanFactoryContribution contribution = createContribution(ImportConfiguration.class);
assertThat(contribution).isNotNull();
BeanFactoryInitialization initialization = new BeanFactoryInitialization(createGenerationContext());
contribution.applyTo(initialization);
assertThat(CodeSnippet.of(initialization.toCodeBlock()).getSnippet()).isEqualTo("""
beanFactory.addBeanPostProcessor(createImportAwareBeanPostProcessor());
""");
}
@Test
void contributeWithImportAwareConfigurationCreateMappingsMethod() {
BeanFactoryContribution contribution = createContribution(ImportConfiguration.class);
assertThat(contribution).isNotNull();
GeneratedTypeContext generationContext = createGenerationContext();
contribution.applyTo(new BeanFactoryInitialization(generationContext));
assertThat(codeOf(generationContext.getMainGeneratedType())).contains("""
private ImportAwareAotBeanPostProcessor createImportAwareBeanPostProcessor() {
Map<String, String> mappings = new HashMap<>();
mappings.put("org.springframework.context.testfixture.context.generator.annotation.ImportAwareConfiguration", "org.springframework.context.testfixture.context.generator.annotation.ImportConfiguration");
return new ImportAwareAotBeanPostProcessor(mappings);
}
""");
}
@Test
void contributeWithImportAwareConfigurationRegisterBytecodeResourceHint() {
BeanFactoryContribution contribution = createContribution(ImportConfiguration.class);
assertThat(contribution).isNotNull();
GeneratedTypeContext generationContext = createGenerationContext();
contribution.applyTo(new BeanFactoryInitialization(generationContext));
assertThat(generationContext.runtimeHints().resources().resourcePatterns())
.singleElement().satisfies(resourceHint -> assertThat(resourceHint.getIncludes()).containsOnly(
"org/springframework/context/testfixture/context/generator/annotation/ImportConfiguration.class"));
}
@Test
void contributeWithNoImportAwareConfigurationReturnsNull() {
assertThat(createContribution(SimpleConfiguration.class)).isNull();
}
@Nullable
private BeanFactoryContribution createContribution(Class<?> type) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition("configuration", new RootBeanDefinition(type));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
return pp.contribute(beanFactory);
}
private GeneratedTypeContext createGenerationContext() {
return new DefaultGeneratedTypeContext("com.example", packageName ->
GeneratedType.of(ClassName.get(packageName, "Test")));
}
private String codeOf(GeneratedType type) {
try {
StringWriter out = new StringWriter();
type.toJavaFile().writeTo(out);
return out.toString();
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}