Polishing

This commit is contained in:
Sam Brannen
2022-10-10 16:52:22 +02:00
parent 26c6a742d9
commit de609e5d45
4 changed files with 33 additions and 17 deletions

View File

@@ -67,6 +67,15 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
this.application = application; this.application = application;
} }
/**
* Get the the application entry point.
*/
protected Class<?> getApplication() {
return this.application;
}
/** /**
* Invoke the processing by clearing output directories first, followed by * Invoke the processing by clearing output directories first, followed by
* {@link #performAotProcessing(GenericApplicationContext)}. * {@link #performAotProcessing(GenericApplicationContext)}.
@@ -75,7 +84,7 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
*/ */
public ClassName process() { public ClassName process() {
deleteExistingOutput(); deleteExistingOutput();
GenericApplicationContext applicationContext = prepareApplicationContext(this.application); GenericApplicationContext applicationContext = prepareApplicationContext(getApplication());
return performAotProcessing(applicationContext); return performAotProcessing(applicationContext);
} }
@@ -102,7 +111,7 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
registerEntryPointHint(generationContext, generatedInitializerClassName); registerEntryPointHint(generationContext, generatedInitializerClassName);
generationContext.writeGeneratedContent(); generationContext.writeGeneratedContent();
writeHints(generationContext.getRuntimeHints()); writeHints(generationContext.getRuntimeHints());
writeNativeImageProperties(getDefaultNativeImageArguments(this.application.getName())); writeNativeImageProperties(getDefaultNativeImageArguments(getApplication().getName()));
return generatedInitializerClassName; return generatedInitializerClassName;
} }
@@ -113,7 +122,7 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
* @return the class name generator * @return the class name generator
*/ */
protected ClassNameGenerator createClassNameGenerator() { protected ClassNameGenerator createClassNameGenerator() {
return new ClassNameGenerator(ClassName.get(this.application)); return new ClassNameGenerator(ClassName.get(getApplication()));
} }
/** /**
@@ -137,7 +146,7 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
ClassName generatedInitializerClassName) { ClassName generatedInitializerClassName) {
TypeReference generatedType = TypeReference.of(generatedInitializerClassName.canonicalName()); TypeReference generatedType = TypeReference.of(generatedInitializerClassName.canonicalName());
TypeReference applicationType = TypeReference.of(this.application); TypeReference applicationType = TypeReference.of(getApplication());
ReflectionHints reflection = generationContext.getRuntimeHints().reflection(); ReflectionHints reflection = generationContext.getRuntimeHints().reflection();
reflection.registerType(applicationType); reflection.registerType(applicationType);
reflection.registerType(generatedType, typeHint -> typeHint.onReachableType(applicationType) reflection.registerType(generatedType, typeHint -> typeHint.onReachableType(applicationType)

View File

@@ -61,6 +61,14 @@ public abstract class TestAotProcessor extends AbstractAotProcessor {
} }
/**
* Get the classpath roots to scan for test classes.
*/
protected Set<Path> getClasspathRoots() {
return this.classpathRoots;
}
/** /**
* Trigger processing of the test classes by * Trigger processing of the test classes by
* {@linkplain #deleteExistingOutput() clearing output directories} first and * {@linkplain #deleteExistingOutput() clearing output directories} first and
@@ -79,7 +87,7 @@ public abstract class TestAotProcessor extends AbstractAotProcessor {
* components used by the tests. * components used by the tests.
*/ */
protected void performAotProcessing() { protected void performAotProcessing() {
TestClassScanner scanner = new TestClassScanner(this.classpathRoots); TestClassScanner scanner = new TestClassScanner(getClasspathRoots());
Stream<Class<?>> testClasses = scanner.scan(); Stream<Class<?>> testClasses = scanner.scan();
GeneratedFiles generatedFiles = createFileSystemGeneratedFiles(); GeneratedFiles generatedFiles = createFileSystemGeneratedFiles();

View File

@@ -103,7 +103,7 @@ class TestClassScanner {
* absolute path to the project's {@code build/classes/java/test} folder. * absolute path to the project's {@code build/classes/java/test} folder.
* @param classpathRoots the classpath roots to scan * @param classpathRoots the classpath roots to scan
*/ */
public TestClassScanner(Set<Path> classpathRoots) { TestClassScanner(Set<Path> classpathRoots) {
this.classpathRoots = assertPreconditions(classpathRoots); this.classpathRoots = assertPreconditions(classpathRoots);
} }
@@ -111,7 +111,7 @@ class TestClassScanner {
/** /**
* Scan the configured classpath roots for Spring integration test classes. * Scan the configured classpath roots for Spring integration test classes.
*/ */
public Stream<Class<?>> scan() { Stream<Class<?>> scan() {
return scan(new String[0]); return scan(new String[0]);
} }
@@ -206,7 +206,6 @@ class TestClassScanner {
mergedAnnotations.isPresent(BootstrapWith.class)); mergedAnnotations.isPresent(BootstrapWith.class));
} }
private static Set<Path> assertPreconditions(Set<Path> classpathRoots) { private static Set<Path> assertPreconditions(Set<Path> classpathRoots) {
Assert.notEmpty(classpathRoots, "'classpathRoots' must not be null or empty"); Assert.notEmpty(classpathRoots, "'classpathRoots' must not be null or empty");
Assert.noNullElements(classpathRoots, "'classpathRoots' must not contain null elements"); Assert.noNullElements(classpathRoots, "'classpathRoots' must not contain null elements");

View File

@@ -126,8 +126,8 @@ public class TestContextAotGenerator {
}); });
MultiValueMap<ClassName, Class<?>> initializerClassMappings = processAheadOfTime(mergedConfigMappings); MultiValueMap<ClassName, Class<?>> initializerClassMappings = processAheadOfTime(mergedConfigMappings);
generateTestAotMappings(initializerClassMappings); generateAotTestContextInitializerMappings(initializerClassMappings);
generateAotTestAttributes(); generateAotTestAttributeMappings();
} }
finally { finally {
resetAotFactories(); resetAotFactories();
@@ -139,7 +139,9 @@ public class TestContextAotGenerator {
AotTestContextInitializersFactory.reset(); AotTestContextInitializersFactory.reset();
} }
private MultiValueMap<ClassName, Class<?>> processAheadOfTime(MultiValueMap<MergedContextConfiguration, Class<?>> mergedConfigMappings) { private MultiValueMap<ClassName, Class<?>> processAheadOfTime(
MultiValueMap<MergedContextConfiguration, Class<?>> mergedConfigMappings) {
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
MultiValueMap<ClassName, Class<?>> initializerClassMappings = new LinkedMultiValueMap<>(); MultiValueMap<ClassName, Class<?>> initializerClassMappings = new LinkedMultiValueMap<>();
mergedConfigMappings.forEach((mergedConfig, testClasses) -> { mergedConfigMappings.forEach((mergedConfig, testClasses) -> {
@@ -251,9 +253,8 @@ public class TestContextAotGenerator {
return "TestContext%03d_".formatted(this.sequence.incrementAndGet()); return "TestContext%03d_".formatted(this.sequence.incrementAndGet());
} }
private void generateTestAotMappings(MultiValueMap<ClassName, Class<?>> initializerClassMappings) { private void generateAotTestContextInitializerMappings(MultiValueMap<ClassName, Class<?>> initializerClassMappings) {
ClassNameGenerator classNameGenerator = new ClassNameGenerator( ClassNameGenerator classNameGenerator = new ClassNameGenerator(ClassName.get(AotTestContextInitializers.class));
ClassName.get(AotTestContextInitializers.class));
DefaultGenerationContext generationContext = DefaultGenerationContext generationContext =
new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints); new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints);
GeneratedClasses generatedClasses = generationContext.getGeneratedClasses(); GeneratedClasses generatedClasses = generationContext.getGeneratedClasses();
@@ -265,9 +266,8 @@ public class TestContextAotGenerator {
registerPublicMethods(className); registerPublicMethods(className);
} }
private void generateAotTestAttributes() { private void generateAotTestAttributeMappings() {
ClassNameGenerator classNameGenerator = new ClassNameGenerator( ClassNameGenerator classNameGenerator = new ClassNameGenerator(ClassName.get(AotTestAttributes.class));
ClassName.get(AotTestAttributes.class));
DefaultGenerationContext generationContext = DefaultGenerationContext generationContext =
new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints); new DefaultGenerationContext(classNameGenerator, this.generatedFiles, this.runtimeHints);
GeneratedClasses generatedClasses = generationContext.getGeneratedClasses(); GeneratedClasses generatedClasses = generationContext.getGeneratedClasses();