From 0dba7298232106cf22f91e6e0579b45c56d37f09 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 12 Sep 2022 16:24:50 +0200 Subject: [PATCH] Polish "Allow MethodReference to support a more flexible signature" See gh-29005 --- ...ionContextInitializationCodeGenerator.java | 2 +- .../ApplicationContextAotGeneratorTests.java | 16 ++++++++++++ ...ntextInitializationCodeGeneratorTests.java | 2 +- .../PropertySourceConfiguration.java | 26 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/PropertySourceConfiguration.java 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 1be6885e9f..3a7253b044 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 @@ -139,7 +139,7 @@ class ApplicationContextInitializationCodeGenerator implements BeanFactoryInitia } else if (name.equals(ConfigurableEnvironment.class.getName()) || name.equals(Environment.class.getName())) { - return CodeBlock.of("$L.getConfigurableEnvironment()", APPLICATION_CONTEXT_VARIABLE); + return CodeBlock.of("$L.getEnvironment()", APPLICATION_CONTEXT_VARIABLE); } else if (name.equals(ResourceLoader.class.getName())) { return CodeBlock.of(APPLICATION_CONTEXT_VARIABLE); 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 b68a4a2020..22e84a63cc 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 @@ -61,7 +61,10 @@ import org.springframework.context.testfixture.context.generator.annotation.Lazy import org.springframework.context.testfixture.context.generator.annotation.LazyAutowiredMethodComponent; import org.springframework.context.testfixture.context.generator.annotation.LazyConstructorArgumentComponent; import org.springframework.context.testfixture.context.generator.annotation.LazyFactoryMethodArgumentComponent; +import org.springframework.context.testfixture.context.generator.annotation.PropertySourceConfiguration; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; import org.springframework.core.io.ResourceLoader; import static org.assertj.core.api.Assertions.assertThat; @@ -279,6 +282,19 @@ class ApplicationContextAotGeneratorTests { .withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(context.getRuntimeHints()); } + @Test + void processAheadOfTimeWithPropertySource() { + GenericApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + applicationContext.registerBean(PropertySourceConfiguration.class); + testCompiledResult(applicationContext, (initializer, compiled) -> { + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); + ConfigurableEnvironment environment = freshApplicationContext.getEnvironment(); + PropertySource propertySource = environment.getPropertySources().get("testp1"); + assertThat(propertySource).isNotNull(); + assertThat(propertySource.getProperty("from.p1")).isEqualTo("p1Value"); + }); + } + private Consumer> doesNotHaveProxyFor(Class target) { return hints -> assertThat(hints).noneMatch(hint -> hint.getProxiedInterfaces().get(0).equals(target)); } diff --git a/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextInitializationCodeGeneratorTests.java b/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextInitializationCodeGeneratorTests.java index 1155be0952..042658487b 100644 --- a/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextInitializationCodeGeneratorTests.java +++ b/spring-context/src/test/java/org/springframework/context/aot/ApplicationContextInitializationCodeGeneratorTests.java @@ -65,7 +65,7 @@ class ApplicationContextInitializationCodeGeneratorTests { static Stream methodArguments() { String applicationContext = "applicationContext"; - String environment = applicationContext + ".getConfigurableEnvironment()"; + String environment = applicationContext + ".getEnvironment()"; return Stream.of( Arguments.of(DefaultListableBeanFactory.class, "beanFactory"), Arguments.of(ConfigurableListableBeanFactory.class, "beanFactory"), diff --git a/spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/PropertySourceConfiguration.java b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/PropertySourceConfiguration.java new file mode 100644 index 0000000000..87670d4db4 --- /dev/null +++ b/spring-context/src/testFixtures/java/org/springframework/context/testfixture/context/generator/annotation/PropertySourceConfiguration.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.testfixture.context.generator.annotation; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration(proxyBeanMethods = false) +@PropertySource(name = "testp1", value = "classpath:org/springframework/context/annotation/p1.properties") +public class PropertySourceConfiguration { + +}