From 482eb49391c128ad4451a508614245c279a4c16c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 20 Oct 2022 14:56:25 +0100 Subject: [PATCH] Support in logback.xml during AOT processing See gh-32756 --- .../logback/SpringBootJoranConfigurator.java | 29 ++++++++----------- ...backConfigurationAotContributionTests.java | 15 ++++++++++ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java index 06ee187b3e..bcf1abd86e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java @@ -36,15 +36,14 @@ import java.util.stream.Stream; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.Context; 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.RuleStore; 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.Model; import ch.qos.logback.core.model.ModelUtil; 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.ContextAwareBase; @@ -113,9 +112,7 @@ class SpringBootJoranConfigurator extends JoranConfigurator { super.processModel(model); if (!NativeDetector.inNativeImage() && isAotProcessingInProgress()) { getContext().putObject(BeanFactoryInitializationAotContribution.class.getName(), - new LogbackConfigurationAotContribution(model, - getModelInterpretationContext().getBeanDescriptionCache(), - getModelInterpretationContext().getDefaultNestedComponentRegistry(), getContext())); + new LogbackConfigurationAotContribution(model, getModelInterpretationContext(), getContext())); } } @@ -129,9 +126,9 @@ class SpringBootJoranConfigurator extends JoranConfigurator { private final PatternRules patternRules; - private LogbackConfigurationAotContribution(Model model, BeanDescriptionCache beanDescriptionCache, - DefaultNestedComponentRegistry nestedComponentRegistry, Context context) { - this.modelWriter = new ModelWriter(model, beanDescriptionCache, nestedComponentRegistry); + private LogbackConfigurationAotContribution(Model model, ModelInterpretationContext interpretationContext, + Context context) { + this.modelWriter = new ModelWriter(model, interpretationContext); this.patternRules = new PatternRules(context); } @@ -150,15 +147,11 @@ class SpringBootJoranConfigurator extends JoranConfigurator { private final Model model; - private final BeanDescriptionCache beanDescriptionCache; + private final ModelInterpretationContext modelInterpretationContext; - private final DefaultNestedComponentRegistry nestedComponentRegistry; - - private ModelWriter(Model model, BeanDescriptionCache beanDescriptionCache, - DefaultNestedComponentRegistry nestedComponentRegistry) { + private ModelWriter(Model model, ModelInterpretationContext modelInterpretationContext) { this.model = model; - this.beanDescriptionCache = beanDescriptionCache; - this.nestedComponentRegistry = nestedComponentRegistry; + this.modelInterpretationContext = modelInterpretationContext; } private void writeTo(GenerationContext generationContext) { @@ -215,7 +208,8 @@ class SpringBootJoranConfigurator extends JoranConfigurator { } String tag = model.getTag(); if (tag != null) { - String componentType = this.nestedComponentRegistry.findDefaultComponentTypeByTag(tag); + String componentType = this.modelInterpretationContext.getDefaultNestedComponentRegistry() + .findDefaultComponentTypeByTag(tag); processComponent(componentType, reflectionTypes); } for (Model submodel : model.getSubModels()) { @@ -226,7 +220,8 @@ class SpringBootJoranConfigurator extends JoranConfigurator { private void processComponent(String componentTypeName, Set reflectionTypes) { if (componentTypeName != null) { - BeanDescription beanDescription = this.beanDescriptionCache + componentTypeName = this.modelInterpretationContext.getImport(componentTypeName); + BeanDescription beanDescription = this.modelInterpretationContext.getBeanDescriptionCache() .getBeanDescription(loadComponentType(componentTypeName)); reflectionTypes.addAll(parameterTypesNames(beanDescription.getPropertyNameToAdder().values())); reflectionTypes.addAll(parameterTypesNames(beanDescription.getPropertyNameToSetter().values())); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java index 3d44a884d0..0ee5ba2a73 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackConfigurationAotContributionTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Properties; 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.model.ComponentModel; 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.rolling.SizeAndTimeBasedRollingPolicy; import ch.qos.logback.core.rolling.TimeBasedFileNamingAndTriggeringPolicy; @@ -134,6 +136,19 @@ class LogbackConfigurationAotContributionTests { .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 invokePublicConstructorsOf(String name) { return RuntimeHintsPredicates.reflection().onType(TypeReference.of(name)) .withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);