Harmonize generated class name conventions

This commit moves the responsibility of naming classes to the
GenerationContext. This was already largely the case before, except that
the concept of a "mainTarget" and "featureNamePrefix" was specific
to bean factory initialization contributors.

ClassNameGenerator should now be instantiated with a default target
and an optional feature name prefix. As a result, it does no longer
generate class names in the "__" package.

GeneratedClasses can now provide a new, unique, GeneratedClass or
offer a container for retrieving the same GeneratedClass based on an
identifier. This lets all contributors use this facility rather than
creating JavaFile manually. This also means that ClassNameGenerator
is no longer exposed.

Because the naming conventions are now part of the GenerationContext, it
is required to be able to retrieve a specialized version of it if a
code generation round needs to use different naming conventions. A new
withName method has been added to that effect.

Closes gh-28585
This commit is contained in:
Stephane Nicoll
2022-06-22 14:20:00 +02:00
parent b121eed753
commit 6199835d6e
31 changed files with 652 additions and 565 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.beans.testfixture.beans.factory.aot.MockBeanFactoryIn
import org.springframework.beans.testfixture.beans.factory.generator.SimpleConfiguration;
import org.springframework.context.testfixture.context.generator.annotation.ImportAwareConfiguration;
import org.springframework.context.testfixture.context.generator.annotation.ImportConfiguration;
import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.JavaFile;
import org.springframework.javapoet.MethodSpec;
@@ -59,7 +60,7 @@ class ConfigurationClassPostProcessorAotContributionTests {
private InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
private DefaultGenerationContext generationContext = new DefaultGenerationContext(
private DefaultGenerationContext generationContext = new TestGenerationContext(
this.generatedFiles);
private MockBeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode();

View File

@@ -44,7 +44,7 @@ import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.testfixture.context.generator.SimpleComponent;
import org.springframework.context.testfixture.context.generator.annotation.AutowiredComponent;
import org.springframework.context.testfixture.context.generator.annotation.InitDestroyComponent;
import org.springframework.javapoet.ClassName;
import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -56,9 +56,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class ApplicationContextAotGeneratorTests {
private static final ClassName MAIN_GENERATED_TYPE = ClassName.get("__",
"TestInitializer");
@Test
void generateApplicationContextWhenHasSimpleBean() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
@@ -191,10 +188,9 @@ class ApplicationContextAotGeneratorTests {
BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
DefaultGenerationContext generationContext = new DefaultGenerationContext(
DefaultGenerationContext generationContext = new TestGenerationContext(
generatedFiles);
generator.generateApplicationContext(applicationContext, generationContext,
MAIN_GENERATED_TYPE);
generator.generateApplicationContext(applicationContext, generationContext);
generationContext.writeGeneratedContent();
TestCompiler.forSystem().withFiles(generatedFiles)
.compile(compiled -> result.accept(

View File

@@ -24,9 +24,7 @@ import java.lang.annotation.Target;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsPredicates;
@@ -39,6 +37,7 @@ import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.SynthesizedAnnotation;
import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
@@ -54,8 +53,7 @@ class ReflectiveProcessorBeanRegistrationAotProcessorTests {
private final ReflectiveProcessorBeanRegistrationAotProcessor processor = new ReflectiveProcessorBeanRegistrationAotProcessor();
private final GenerationContext generationContext = new DefaultGenerationContext(
new InMemoryGeneratedFiles());
private final GenerationContext generationContext = new TestGenerationContext();
@Test
void shouldIgnoreNonAnnotatedType() {

View File

@@ -25,9 +25,7 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.ResourceBundleHint;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -38,7 +36,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.javapoet.ClassName;
import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
@@ -51,17 +49,13 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
*/
class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
private static final ClassName MAIN_GENERATED_TYPE = ClassName.get("__",
"TestInitializer");
private GenerationContext generationContext;
private ApplicationContextAotGenerator generator;
@BeforeEach
void setup() {
this.generationContext = new DefaultGenerationContext(
new InMemoryGeneratedFiles());
this.generationContext = new TestGenerationContext();
this.generator = new ApplicationContextAotGenerator();
}
@@ -70,7 +64,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
GenericApplicationContext applicationContext = createApplicationContext(
ConfigurationWithHints.class);
this.generator.generateApplicationContext(applicationContext,
this.generationContext, MAIN_GENERATED_TYPE);
this.generationContext);
assertThatSampleRegistrarContributed();
}
@@ -79,7 +73,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
GenericApplicationContext applicationContext = createApplicationContext(
ConfigurationWithBeanDeclaringHints.class);
this.generator.generateApplicationContext(applicationContext,
this.generationContext, MAIN_GENERATED_TYPE);
this.generationContext);
assertThatSampleRegistrarContributed();
}
@@ -89,7 +83,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
applicationContext.setClassLoader(
new TestSpringFactoriesClassLoader("test-runtime-hints-aot.factories"));
this.generator.generateApplicationContext(applicationContext,
this.generationContext, MAIN_GENERATED_TYPE);
this.generationContext);
assertThatSampleRegistrarContributed();
}
@@ -104,7 +98,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
new TestSpringFactoriesClassLoader("test-duplicated-runtime-hints-aot.factories"));
IncrementalRuntimeHintsRegistrar.counter.set(0);
this.generator.generateApplicationContext(applicationContext,
this.generationContext, MAIN_GENERATED_TYPE);
this.generationContext);
RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
assertThat(runtimeHints.resources().resourceBundles().map(ResourceBundleHint::getBaseName))
.containsOnly("com.example.example0", "sample");
@@ -116,7 +110,7 @@ class RuntimeHintsBeanFactoryInitializationAotProcessorTests {
GenericApplicationContext applicationContext = createApplicationContext(
ConfigurationWithIllegalRegistrar.class);
assertThatThrownBy(() -> this.generator.generateApplicationContext(
applicationContext, this.generationContext, MAIN_GENERATED_TYPE))
applicationContext, this.generationContext))
.isInstanceOf(BeanInstantiationException.class);
}