Support <import> in logback.xml during AOT processing

See gh-32756
pull/32822/head
Andy Wilkinson 2 years ago
parent 685fa900f8
commit 482eb49391

@ -36,15 +36,14 @@ import java.util.stream.Stream;
import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.Context; import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants; import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementSelector; import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.RuleStore; import ch.qos.logback.core.joran.spi.RuleStore;
import ch.qos.logback.core.joran.util.beans.BeanDescription; import ch.qos.logback.core.joran.util.beans.BeanDescription;
import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
import ch.qos.logback.core.model.ComponentModel; import ch.qos.logback.core.model.ComponentModel;
import ch.qos.logback.core.model.Model; import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.ModelUtil; import ch.qos.logback.core.model.ModelUtil;
import ch.qos.logback.core.model.processor.DefaultProcessor; import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
import ch.qos.logback.core.spi.ContextAware; import ch.qos.logback.core.spi.ContextAware;
import ch.qos.logback.core.spi.ContextAwareBase; import ch.qos.logback.core.spi.ContextAwareBase;
@ -113,9 +112,7 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
super.processModel(model); super.processModel(model);
if (!NativeDetector.inNativeImage() && isAotProcessingInProgress()) { if (!NativeDetector.inNativeImage() && isAotProcessingInProgress()) {
getContext().putObject(BeanFactoryInitializationAotContribution.class.getName(), getContext().putObject(BeanFactoryInitializationAotContribution.class.getName(),
new LogbackConfigurationAotContribution(model, new LogbackConfigurationAotContribution(model, getModelInterpretationContext(), getContext()));
getModelInterpretationContext().getBeanDescriptionCache(),
getModelInterpretationContext().getDefaultNestedComponentRegistry(), getContext()));
} }
} }
@ -129,9 +126,9 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
private final PatternRules patternRules; private final PatternRules patternRules;
private LogbackConfigurationAotContribution(Model model, BeanDescriptionCache beanDescriptionCache, private LogbackConfigurationAotContribution(Model model, ModelInterpretationContext interpretationContext,
DefaultNestedComponentRegistry nestedComponentRegistry, Context context) { Context context) {
this.modelWriter = new ModelWriter(model, beanDescriptionCache, nestedComponentRegistry); this.modelWriter = new ModelWriter(model, interpretationContext);
this.patternRules = new PatternRules(context); this.patternRules = new PatternRules(context);
} }
@ -150,15 +147,11 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
private final Model model; private final Model model;
private final BeanDescriptionCache beanDescriptionCache; private final ModelInterpretationContext modelInterpretationContext;
private final DefaultNestedComponentRegistry nestedComponentRegistry; private ModelWriter(Model model, ModelInterpretationContext modelInterpretationContext) {
private ModelWriter(Model model, BeanDescriptionCache beanDescriptionCache,
DefaultNestedComponentRegistry nestedComponentRegistry) {
this.model = model; this.model = model;
this.beanDescriptionCache = beanDescriptionCache; this.modelInterpretationContext = modelInterpretationContext;
this.nestedComponentRegistry = nestedComponentRegistry;
} }
private void writeTo(GenerationContext generationContext) { private void writeTo(GenerationContext generationContext) {
@ -215,7 +208,8 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
} }
String tag = model.getTag(); String tag = model.getTag();
if (tag != null) { if (tag != null) {
String componentType = this.nestedComponentRegistry.findDefaultComponentTypeByTag(tag); String componentType = this.modelInterpretationContext.getDefaultNestedComponentRegistry()
.findDefaultComponentTypeByTag(tag);
processComponent(componentType, reflectionTypes); processComponent(componentType, reflectionTypes);
} }
for (Model submodel : model.getSubModels()) { for (Model submodel : model.getSubModels()) {
@ -226,7 +220,8 @@ class SpringBootJoranConfigurator extends JoranConfigurator {
private void processComponent(String componentTypeName, Set<String> reflectionTypes) { private void processComponent(String componentTypeName, Set<String> reflectionTypes) {
if (componentTypeName != null) { if (componentTypeName != null) {
BeanDescription beanDescription = this.beanDescriptionCache componentTypeName = this.modelInterpretationContext.getImport(componentTypeName);
BeanDescription beanDescription = this.modelInterpretationContext.getBeanDescriptionCache()
.getBeanDescription(loadComponentType(componentTypeName)); .getBeanDescription(loadComponentType(componentTypeName));
reflectionTypes.addAll(parameterTypesNames(beanDescription.getPropertyNameToAdder().values())); reflectionTypes.addAll(parameterTypesNames(beanDescription.getPropertyNameToAdder().values()));
reflectionTypes.addAll(parameterTypesNames(beanDescription.getPropertyNameToSetter().values())); reflectionTypes.addAll(parameterTypesNames(beanDescription.getPropertyNameToSetter().values()));

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -32,6 +33,7 @@ import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.Layout; import ch.qos.logback.core.Layout;
import ch.qos.logback.core.model.ComponentModel; import ch.qos.logback.core.model.ComponentModel;
import ch.qos.logback.core.model.ImplicitModel; import ch.qos.logback.core.model.ImplicitModel;
import ch.qos.logback.core.model.ImportModel;
import ch.qos.logback.core.model.Model; import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy; import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicy; import ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicy;
@ -134,6 +136,19 @@ class LogbackConfigurationAotContributionTests {
.accepts(generationContext.getRuntimeHints()); .accepts(generationContext.getRuntimeHints());
} }
@Test
void componentModelReferencingImportedClassNameIsRegisteredForReflection() {
ImportModel importModel = new ImportModel();
importModel.setClassName(SizeAndTimeBasedRollingPolicy.class.getName());
ComponentModel component = new ComponentModel();
component.setClassName(SizeAndTimeBasedRollingPolicy.class.getSimpleName());
Model model = new Model();
model.getSubModels().addAll(List.of(importModel, component));
TestGenerationContext generationContext = applyContribution(model);
assertThat(invokePublicConstructorsAndInspectAndInvokePublicMethodsOf(SizeAndTimeBasedRollingPolicy.class))
.accepts(generationContext.getRuntimeHints());
}
private Predicate<RuntimeHints> invokePublicConstructorsOf(String name) { private Predicate<RuntimeHints> invokePublicConstructorsOf(String name) {
return RuntimeHintsPredicates.reflection().onType(TypeReference.of(name)) return RuntimeHintsPredicates.reflection().onType(TypeReference.of(name))
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS); .withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);

Loading…
Cancel
Save