From 853407b6f8956729f33f1fe9fd01ff7601f0ed55 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 5 Aug 2022 11:54:03 +0300 Subject: [PATCH] Polishing --- .../aot/ApplicationContextAotGenerator.java | 9 +- ...ionContextInitializationCodeGenerator.java | 9 +- .../ApplicationContextAotGeneratorTests.java | 89 +++++++------------ 3 files changed, 41 insertions(+), 66 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java b/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java index 1ea201fe1697..de976b2f8b39 100644 --- a/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java +++ b/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java @@ -36,13 +36,14 @@ public class ApplicationContextAotGenerator { /** - * Process the specified {@link GenericApplicationContext} instance - * ahead-of-time using the specified {@link GenerationContext}. + * Process the specified {@link GenericApplicationContext} ahead-of-time using + * the specified {@link GenerationContext}. *

Return the {@link ClassName} of the {@link ApplicationContextInitializer} * to use to restore an optimized state of the application context. - * @param applicationContext the non-refreshed application context to handle + * @param applicationContext the non-refreshed application context to process * @param generationContext the generation context to use - * @return the class name of the {@link ApplicationContextInitializer} entry point + * @return the {@code ClassName} of the {@code ApplicationContextInitializer} + * entry point */ public ClassName processAheadOfTime(GenericApplicationContext applicationContext, GenerationContext generationContext) { diff --git a/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextInitializationCodeGenerator.java b/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextInitializationCodeGenerator.java index e22160f7174a..460a1a46be13 100644 --- a/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextInitializationCodeGenerator.java +++ b/spring-context/src/main/java/org/springframework/context/aot/ApplicationContextInitializationCodeGenerator.java @@ -36,13 +36,12 @@ import org.springframework.javapoet.TypeSpec; /** - * Internal code generator to create the application context initializer. + * Internal code generator to create the {@link ApplicationContextInitializer}. * * @author Phillip Webb * @since 6.0 */ -class ApplicationContextInitializationCodeGenerator - implements BeanFactoryInitializationCode { +class ApplicationContextInitializationCodeGenerator implements BeanFactoryInitializationCode { private static final String INITIALIZE_METHOD = "initialize"; @@ -70,13 +69,11 @@ private void generateType(TypeSpec.Builder type) { type.addMethod(generateInitializeMethod()); } - private MethodSpec generateInitializeMethod() { MethodSpec.Builder method = MethodSpec.methodBuilder(INITIALIZE_METHOD); method.addAnnotation(Override.class); method.addModifiers(Modifier.PUBLIC); - method.addParameter(GenericApplicationContext.class, - APPLICATION_CONTEXT_VARIABLE); + method.addParameter(GenericApplicationContext.class, APPLICATION_CONTEXT_VARIABLE); method.addCode(generateInitializeCode()); return method.build(); } diff --git a/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java b/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java index 0d78e6d4c0cb..d6899c7931df 100644 --- a/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java +++ b/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java @@ -57,40 +57,31 @@ class ApplicationContextAotGeneratorTests { @Test void generateApplicationContextWhenHasSimpleBean() { GenericApplicationContext applicationContext = new GenericApplicationContext(); - applicationContext.registerBeanDefinition("test", - new RootBeanDefinition(SimpleComponent.class)); + applicationContext.registerBeanDefinition("test", new RootBeanDefinition(SimpleComponent.class)); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); - assertThat(freshApplicationContext.getBeanDefinitionNames()) - .containsOnly("test"); - assertThat(freshApplicationContext.getBean("test")) - .isInstanceOf(SimpleComponent.class); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); + assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("test"); + assertThat(freshApplicationContext.getBean("test")).isInstanceOf(SimpleComponent.class); }); } @Test void generateApplicationContextWhenHasAutowiring() { GenericApplicationContext applicationContext = new GenericApplicationContext(); - applicationContext.registerBeanDefinition( - AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME, + applicationContext.registerBeanDefinition(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME, BeanDefinitionBuilder - .rootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class) - .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); - applicationContext.registerBeanDefinition("autowiredComponent", - new RootBeanDefinition(AutowiredComponent.class)); + .rootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class) + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); + applicationContext.registerBeanDefinition("autowiredComponent", new RootBeanDefinition(AutowiredComponent.class)); applicationContext.registerBeanDefinition("number", - BeanDefinitionBuilder.rootBeanDefinition(Integer.class, "valueOf") - .addConstructorArgValue("42").getBeanDefinition()); + BeanDefinitionBuilder + .rootBeanDefinition(Integer.class, "valueOf") + .addConstructorArgValue("42").getBeanDefinition()); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); - assertThat(freshApplicationContext.getBeanDefinitionNames()) - .containsOnly("autowiredComponent", "number"); - AutowiredComponent bean = freshApplicationContext - .getBean(AutowiredComponent.class); - assertThat(bean.getEnvironment()) - .isSameAs(freshApplicationContext.getEnvironment()); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); + assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("autowiredComponent", "number"); + AutowiredComponent bean = freshApplicationContext.getBean(AutowiredComponent.class); + assertThat(bean.getEnvironment()).isSameAs(freshApplicationContext.getEnvironment()); assertThat(bean.getCounter()).isEqualTo(42); }); } @@ -101,17 +92,14 @@ void generateApplicationContextWhenHasInitDestroyMethods() { applicationContext.registerBeanDefinition( AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME, BeanDefinitionBuilder - .rootBeanDefinition(CommonAnnotationBeanPostProcessor.class) - .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); + .rootBeanDefinition(CommonAnnotationBeanPostProcessor.class) + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); applicationContext.registerBeanDefinition("initDestroyComponent", new RootBeanDefinition(InitDestroyComponent.class)); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); - assertThat(freshApplicationContext.getBeanDefinitionNames()) - .containsOnly("initDestroyComponent"); - InitDestroyComponent bean = freshApplicationContext - .getBean(InitDestroyComponent.class); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); + assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("initDestroyComponent"); + InitDestroyComponent bean = freshApplicationContext.getBean(InitDestroyComponent.class); assertThat(bean.events).containsExactly("init"); freshApplicationContext.close(); assertThat(bean.events).containsExactly("init", "destroy"); @@ -124,24 +112,19 @@ void generateApplicationContextWhenHasMultipleInitDestroyMethods() { applicationContext.registerBeanDefinition( AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME, BeanDefinitionBuilder - .rootBeanDefinition(CommonAnnotationBeanPostProcessor.class) - .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); - RootBeanDefinition beanDefinition = new RootBeanDefinition( - InitDestroyComponent.class); + .rootBeanDefinition(CommonAnnotationBeanPostProcessor.class) + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); + RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyComponent.class); beanDefinition.setInitMethodName("customInit"); beanDefinition.setDestroyMethodName("customDestroy"); applicationContext.registerBeanDefinition("initDestroyComponent", beanDefinition); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); - assertThat(freshApplicationContext.getBeanDefinitionNames()) - .containsOnly("initDestroyComponent"); - InitDestroyComponent bean = freshApplicationContext - .getBean(InitDestroyComponent.class); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); + assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("initDestroyComponent"); + InitDestroyComponent bean = freshApplicationContext.getBean(InitDestroyComponent.class); assertThat(bean.events).containsExactly("customInit", "init"); freshApplicationContext.close(); - assertThat(bean.events).containsExactly("customInit", "init", "customDestroy", - "destroy"); + assertThat(bean.events).containsExactly("customInit", "init", "customDestroy", "destroy"); }); } @@ -149,8 +132,7 @@ void generateApplicationContextWhenHasMultipleInitDestroyMethods() { void generateApplicationContextWhenHasNoAotContributions() { GenericApplicationContext applicationContext = new GenericApplicationContext(); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); assertThat(freshApplicationContext.getBeanDefinitionNames()).isEmpty(); assertThat(compiled.getSourceFile()).contains( "beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver())"); @@ -163,8 +145,7 @@ void generateApplicationContextWhenHasBeanFactoryInitializationAotProcessorExclu applicationContext.registerBeanDefinition("test", new RootBeanDefinition(NoOpBeanFactoryInitializationAotProcessor.class)); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); assertThat(freshApplicationContext.getBeanDefinitionNames()).isEmpty(); }); } @@ -175,8 +156,7 @@ void generateApplicationContextWhenHasBeanRegistrationAotProcessorExcludesProces applicationContext.registerBeanDefinition("test", new RootBeanDefinition(NoOpBeanRegistrationAotProcessor.class)); testCompiledResult(applicationContext, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); assertThat(freshApplicationContext.getBeanDefinitionNames()).isEmpty(); }); } @@ -205,13 +185,11 @@ static class NoOpBeanFactoryInitializationAotProcessor implements BeanFactoryPostProcessor, BeanFactoryInitializationAotProcessor { @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) - throws BeansException { + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } @Override - public BeanFactoryInitializationAotContribution processAheadOfTime( - ConfigurableListableBeanFactory beanFactory) { + public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { return null; } @@ -222,8 +200,7 @@ static class NoOpBeanRegistrationAotProcessor implements BeanPostProcessor, BeanRegistrationAotProcessor { @Override - public BeanRegistrationAotContribution processAheadOfTime( - RegisteredBean registeredBean) { + public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { return null; }