Skip to content

Commit

Permalink
Register reflection hints for ApplicationProperties
Browse files Browse the repository at this point in the history
Closes gh-42038
  • Loading branch information
mhalbritter committed Aug 28, 2024
1 parent 162c929 commit 45b83d5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;

import org.springframework.boot.Banner.Mode;
import org.springframework.boot.context.properties.bind.BindableRuntimeHintsRegistrar;
import org.springframework.boot.logging.LoggingSystemProperty;
import org.springframework.core.env.Environment;

Expand Down Expand Up @@ -156,4 +157,12 @@ void setWebApplicationType(WebApplicationType webApplicationType) {
this.webApplicationType = webApplicationType;
}

static class ApplicationPropertiesRuntimeHints extends BindableRuntimeHintsRegistrar {

ApplicationPropertiesRuntimeHints() {
super(ApplicationProperties.class);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.BindableRuntimeHintsRegistrar;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.convert.ApplicationConversionService;
Expand Down Expand Up @@ -1594,14 +1593,6 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

}

static class SpringApplicationRuntimeHints extends BindableRuntimeHintsRegistrar {

SpringApplicationRuntimeHints() {
super(SpringApplication.class);
}

}

/**
* Exception that can be thrown to silently exit a running {@link SpringApplication}
* without handling run failures.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.boot.SpringApplication.SpringApplicationRuntimeHints,\
org.springframework.boot.ApplicationProperties.ApplicationPropertiesRuntimeHints,\
org.springframework.boot.SpringApplicationBannerPrinter.SpringApplicationBannerPrinterRuntimeHints,\
org.springframework.boot.WebApplicationType.WebApplicationTypeRuntimeHints,\
org.springframework.boot.context.config.ConfigDataLocationRuntimeHints,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.junit.jupiter.api.Test;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.boot.ApplicationProperties.ApplicationPropertiesRuntimeHints;
import org.springframework.boot.Banner.Mode;
import org.springframework.mock.env.MockEnvironment;

Expand All @@ -44,4 +47,19 @@ void bannerModeShouldBeOffIfStructuredLoggingIsEnabled() {
assertThat(properties.getBannerMode(environment)).isEqualTo(Mode.OFF);
}

@Test
void shouldRegisterHints() {
RuntimeHints hints = new RuntimeHints();
new ApplicationPropertiesRuntimeHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection().onType(ApplicationProperties.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(ApplicationProperties.class, "setBannerMode"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(ApplicationProperties.class, "getSources"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(ApplicationProperties.class, "setSources"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(ApplicationProperties.class, "getBannerMode"))
.rejects(hints);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import reactor.core.publisher.Mono;

import org.springframework.aot.AotDetector;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.ObjectProvider;
Expand All @@ -58,7 +56,6 @@
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.SpringApplication.SpringApplicationRuntimeHints;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.AvailabilityState;
import org.springframework.boot.availability.LivenessState;
Expand Down Expand Up @@ -1412,18 +1409,6 @@ public void contextLoaded(ConfigurableApplicationContext context) {
then(listener).should(never()).onApplicationEvent(any(ApplicationFailedEvent.class));
}

@Test
void shouldRegisterHints() {
RuntimeHints hints = new RuntimeHints();
new SpringApplicationRuntimeHints().registerHints(hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection().onType(SpringApplication.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SpringApplication.class, "setBannerMode"))
.accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SpringApplication.class, "getSources")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SpringApplication.class, "setSources")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SpringApplication.class, "load")).rejects(hints);
}

@Test // gh-32555
void shouldUseAotInitializer() {
SpringApplication application = new SpringApplication(ExampleAotProcessedMainClass.class);
Expand Down

0 comments on commit 45b83d5

Please sign in to comment.