|
19 | 19 | import java.lang.reflect.Method; |
20 | 20 | import java.util.ArrayList; |
21 | 21 | import java.util.Arrays; |
22 | | -import java.util.Collection; |
23 | 22 | import java.util.Collections; |
24 | 23 | import java.util.List; |
25 | 24 | import java.util.function.Consumer; |
26 | 25 |
|
27 | | -import org.springframework.aot.generate.GenerationContext; |
28 | 26 | import org.springframework.aot.hint.ExecutableMode; |
29 | | -import org.springframework.aot.hint.ReflectionHints; |
| 27 | +import org.springframework.aot.hint.RuntimeHints; |
30 | 28 | import org.springframework.beans.BeanUtils; |
31 | | -import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
32 | | -import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; |
33 | | -import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; |
34 | | -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
35 | 29 | import org.springframework.boot.ApplicationContextFactory; |
36 | 30 | import org.springframework.boot.Banner; |
37 | 31 | import org.springframework.boot.ConfigurableBootstrapContext; |
@@ -113,28 +107,33 @@ public class SpringBootContextLoader extends AbstractContextLoader implements Ao |
113 | 107 |
|
114 | 108 | @Override |
115 | 109 | public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
116 | | - return loadContext(mergedConfig, Mode.STANDARD, null); |
| 110 | + return loadContext(mergedConfig, Mode.STANDARD, null, null); |
117 | 111 | } |
118 | 112 |
|
119 | 113 | @Override |
120 | | - public ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception { |
121 | | - return loadContext(mergedConfig, Mode.AOT_PROCESSING, null); |
| 114 | + public ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig, |
| 115 | + RuntimeHints runtimeHints) throws Exception { |
| 116 | + return loadContext(mergedConfig, Mode.AOT_PROCESSING, null, runtimeHints); |
122 | 117 | } |
123 | 118 |
|
124 | 119 | @Override |
125 | 120 | public ApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig, |
126 | 121 | ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception { |
127 | | - return loadContext(mergedConfig, Mode.AOT_RUNTIME, initializer); |
| 122 | + return loadContext(mergedConfig, Mode.AOT_RUNTIME, initializer, null); |
128 | 123 | } |
129 | 124 |
|
130 | 125 | private ApplicationContext loadContext(MergedContextConfiguration mergedConfig, Mode mode, |
131 | | - ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception { |
| 126 | + ApplicationContextInitializer<ConfigurableApplicationContext> initializer, RuntimeHints runtimeHints) |
| 127 | + throws Exception { |
132 | 128 | assertHasClassesOrLocations(mergedConfig); |
133 | 129 | SpringBootTestAnnotation annotation = SpringBootTestAnnotation.get(mergedConfig); |
134 | 130 | String[] args = annotation.getArgs(); |
135 | 131 | UseMainMethod useMainMethod = annotation.getUseMainMethod(); |
136 | 132 | Method mainMethod = getMainMethod(mergedConfig, useMainMethod); |
137 | 133 | if (mainMethod != null) { |
| 134 | + if (runtimeHints != null) { |
| 135 | + runtimeHints.reflection().registerMethod(mainMethod, ExecutableMode.INVOKE); |
| 136 | + } |
138 | 137 | ContextLoaderHook hook = new ContextLoaderHook(mode, initializer, |
139 | 138 | (application) -> configure(mergedConfig, application)); |
140 | 139 | return hook.runMain(() -> ReflectionUtils.invokeMethod(mainMethod, null, new Object[] { args })); |
@@ -585,39 +584,4 @@ private ApplicationContext run(ThrowingSupplier<ConfigurableApplicationContext> |
585 | 584 |
|
586 | 585 | } |
587 | 586 |
|
588 | | - static class MainMethodBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor { |
589 | | - |
590 | | - @Override |
591 | | - public BeanFactoryInitializationAotContribution processAheadOfTime( |
592 | | - ConfigurableListableBeanFactory beanFactory) { |
593 | | - List<Method> mainMethods = new ArrayList<>(); |
594 | | - for (String beanName : beanFactory.getBeanDefinitionNames()) { |
595 | | - Class<?> beanType = beanFactory.getType(beanName); |
596 | | - Method mainMethod = findMainMethod(beanType); |
597 | | - if (mainMethod != null) { |
598 | | - mainMethods.add(mainMethod); |
599 | | - } |
600 | | - } |
601 | | - return !mainMethods.isEmpty() ? new AotContribution(mainMethods) : null; |
602 | | - } |
603 | | - |
604 | | - static class AotContribution implements BeanFactoryInitializationAotContribution { |
605 | | - |
606 | | - private final Collection<Method> mainMethods; |
607 | | - |
608 | | - AotContribution(Collection<Method> mainMethods) { |
609 | | - this.mainMethods = mainMethods; |
610 | | - } |
611 | | - |
612 | | - @Override |
613 | | - public void applyTo(GenerationContext generationContext, |
614 | | - BeanFactoryInitializationCode beanFactoryInitializationCode) { |
615 | | - ReflectionHints reflectionHints = generationContext.getRuntimeHints().reflection(); |
616 | | - this.mainMethods.forEach((method) -> reflectionHints.registerMethod(method, ExecutableMode.INVOKE)); |
617 | | - } |
618 | | - |
619 | | - } |
620 | | - |
621 | | - } |
622 | | - |
623 | 587 | } |
0 commit comments