Register runtime hints for ContextLoaders automatically in the TCF

This commit moves the registration of runtime hints for ContextLoaders
from TestContextRuntimeHints to TestContextAotGenerator.

This allows for automatic registration of any ContextLoader used,
including custom loaders declared via the `loader` attribute in
@ContextConfiguration.

See gh-29021
This commit is contained in:
Sam Brannen
2022-09-02 14:02:58 +02:00
parent 41b6f4e351
commit 51daeac86b
4 changed files with 72 additions and 20 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.aot.generate.GeneratedClasses;
import org.springframework.aot.generate.GeneratedFiles;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.context.ApplicationContext;
@@ -173,6 +174,8 @@ public class TestContextAotGenerator {
Consider annotating test class [%s] with @ContextConfiguration or \
@ContextHierarchy.""".formatted(testClass.getName()));
registerDeclaredConstructors(contextLoader.getClass());
if (contextLoader instanceof AotContextLoader aotContextLoader) {
try {
ApplicationContext context = aotContextLoader.loadContextForAotProcessing(mergedConfig);
@@ -224,4 +227,9 @@ public class TestContextAotGenerator {
.registerType(TypeReference.of(className), MemberCategory.INVOKE_PUBLIC_METHODS);
}
private void registerDeclaredConstructors(Class<?> type) {
ReflectionHints reflectionHints = this.runtimeHints.reflection();
reflectionHints.registerType(type, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}

View File

@@ -48,9 +48,7 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
// Loaded reflectively in BootstrapUtils
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.class,
// Loaded reflectively in BootstrapUtils
org.springframework.test.context.support.DefaultBootstrapContext.class,
// Loaded reflectively in AbstractTestContextBootstrapper#resolveContextLoader()
org.springframework.test.context.support.DelegatingSmartContextLoader.class
org.springframework.test.context.support.DefaultBootstrapContext.class
);
registerDeclaredConstructors(reflectionHints,
@@ -59,10 +57,6 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
);
if (servletPresent) {
registerPublicConstructors(reflectionHints,
// Loaded reflectively in AbstractTestContextBootstrapper#resolveContextLoader()
"org.springframework.test.context.web.WebDelegatingSmartContextLoader"
);
registerDeclaredConstructors(reflectionHints,
// Loaded reflectively in BootstrapUtils
"org.springframework.test.context.web.WebTestContextBootstrapper"
@@ -92,10 +86,6 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
registerPublicConstructors(reflectionHints, TypeReference.listOf(types));
}
private static void registerPublicConstructors(ReflectionHints reflectionHints, String... classNames) {
registerPublicConstructors(reflectionHints, listOf(classNames));
}
private static void registerPublicConstructors(ReflectionHints reflectionHints, Iterable<TypeReference> types) {
reflectionHints.registerTypes(types, TypeHint.builtWith(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
}