From 8cd9f49fad5effb8d4c970847ded0c2ab4ac7f7a Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 18 Oct 2022 14:29:30 +0200 Subject: [PATCH] Add runtime hints for ConfigDataProperties Closes gh-32608 --- .../ConfigDataPropertiesRuntimeHints.java | 40 +++++++++++++++++ .../resources/META-INF/spring/aot.factories | 1 + ...ConfigDataPropertiesRuntimeHintsTests.java | 44 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHints.java create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHintsTests.java diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHints.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHints.java new file mode 100644 index 0000000000..3464ee128f --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHints.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-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.boot.context.config; + +import org.springframework.aot.hint.ExecutableMode; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.boot.context.properties.ConfigurationPropertiesReflectionHintsProcessor; +import org.springframework.util.ReflectionUtils; + +/** + * {@link RuntimeHintsRegistrar} for {@link ConfigDataProperties}. + * + * @author Moritz Halbritter + */ +class ConfigDataPropertiesRuntimeHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + ConfigurationPropertiesReflectionHintsProcessor.processConfigurationProperties(ConfigDataProperties.class, + hints.reflection()); + hints.reflection().registerMethod(ReflectionUtils.findMethod(ConfigDataLocation.class, "of", String.class), + ExecutableMode.INVOKE); + } + +} diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories b/spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories index 24d607cf90..e1c9f24700 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories @@ -2,6 +2,7 @@ org.springframework.aot.hint.RuntimeHintsRegistrar=\ org.springframework.boot.SpringApplication.SpringApplicationRuntimeHints,\ org.springframework.boot.WebApplicationType.WebApplicationTypeRuntimeHints,\ org.springframework.boot.context.config.ConfigDataLocationRuntimeHints,\ +org.springframework.boot.context.config.ConfigDataPropertiesRuntimeHints,\ org.springframework.boot.env.PropertySourceRuntimeHints,\ org.springframework.boot.json.JacksonRuntimeHints,\ org.springframework.boot.logging.java.JavaLoggingSystemRuntimeHints,\ diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHintsTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHintsTests.java new file mode 100644 index 0000000000..7fad8fd3db --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataPropertiesRuntimeHintsTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-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.boot.context.config; + +import org.junit.jupiter.api.Test; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; +import org.springframework.boot.context.config.ConfigDataProperties.Activate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ConfigDataPropertiesRuntimeHints}. + * + * @author Moritz Halbritter + */ +class ConfigDataPropertiesRuntimeHintsTests { + + @Test + void shouldRegisterHints() { + RuntimeHints hints = new RuntimeHints(); + new ConfigDataPropertiesRuntimeHints().registerHints(hints, getClass().getClassLoader()); + assertThat(RuntimeHintsPredicates.reflection().onType(ConfigDataProperties.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onType(ConfigDataLocation.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onType(Activate.class)).accepts(hints); + assertThat(RuntimeHintsPredicates.reflection().onMethod(ConfigDataLocation.class, "of")).accepts(hints); + } + +}