Generate missing hints for custom PropertySource factories

Closes gh-30175
This commit is contained in:
Stephane Nicoll
2023-04-25 16:15:58 +02:00
parent 230840a41c
commit f6875b11ff
2 changed files with 35 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
@@ -40,6 +41,7 @@ import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
import org.springframework.aot.generate.GeneratedMethod;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ResourceHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
@@ -650,6 +652,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
@Override
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) {
registerRuntimeHints(generationContext.getRuntimeHints());
GeneratedMethod generatedMethod = beanFactoryInitializationCode
.getMethods()
.add("processPropertySources", this::generateAddPropertySourceProcessorMethod);
@@ -657,6 +660,13 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
.addInitializer(generatedMethod.toMethodReference());
}
private void registerRuntimeHints(RuntimeHints hints) {
this.descriptors.stream().map(PropertySourceDescriptor::propertySourceFactory)
.filter(Objects::nonNull).distinct()
.forEach(factory -> hints.reflection()
.registerType(factory, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
}
private void generateAddPropertySourceProcessorMethod(MethodSpec.Builder method) {
method.addJavadoc("Apply known @PropertySources to the environment.");
method.addModifiers(Modifier.PRIVATE);