Skip to content

Commit f866e20

Browse files
committed
Polish "Add RuntimeHints for StackTracePrinter"
See gh-44242
1 parent 5d781ff commit f866e20

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ record StackTrace(String printer, Root root, Integer maxLength, Integer maxThrow
8787
Boolean includeCommonFrames, Boolean includeHashes) {
8888

8989
StackTracePrinter createPrinter() {
90-
String name = getPrinter();
90+
String name = sanitizePrinter();
9191
if ("loggingsystem".equals(name) || (name.isEmpty() && !hasAnyOtherProperty())) {
9292
return null;
9393
}
@@ -101,14 +101,14 @@ StackTracePrinter createPrinter() {
101101
}
102102

103103
boolean hasCustomPrinter() {
104-
String name = getPrinter();
104+
String name = sanitizePrinter();
105105
if (name.isEmpty()) {
106106
return false;
107107
}
108108
return !("loggingsystem".equals(name) || "standard".equals(name));
109109
}
110110

111-
private String getPrinter() {
111+
private String sanitizePrinter() {
112112
return Objects.toString(printer(), "").toLowerCase(Locale.ROOT).replace("-", "");
113113
}
114114

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor.java

+22-21
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* {@link BeanFactoryInitializationAotProcessor} that registers {@link RuntimeHints} for
35-
* {@link StructuredLoggingJsonPropertiesJsonMembersCustomizer}.
35+
* {@link StructuredLoggingJsonProperties}.
3636
*
3737
* @author Dmytro Nosan
3838
* @author Yanming Zhou
@@ -47,17 +47,34 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor
4747
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
4848
Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class);
4949
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
50-
return (properties != null) ? AotContribution.get(properties) : null;
50+
if (properties != null) {
51+
Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers = properties.customizer();
52+
String stackTracePrinter = getCustomStackTracePrinter(properties);
53+
if (stackTracePrinter != null || !customizers.isEmpty()) {
54+
return new AotContribution(beanFactory.getBeanClassLoader(), customizers, stackTracePrinter);
55+
}
56+
}
57+
return null;
58+
}
59+
60+
private static String getCustomStackTracePrinter(StructuredLoggingJsonProperties properties) {
61+
return Optional.ofNullable(properties.stackTrace())
62+
.filter(StackTrace::hasCustomPrinter)
63+
.map(StackTrace::printer)
64+
.orElse(null);
5165
}
5266

5367
private static final class AotContribution implements BeanFactoryInitializationAotContribution {
5468

69+
private final ClassLoader classLoader;
70+
5571
private final Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers;
5672

5773
private final String stackTracePrinter;
5874

59-
private AotContribution(Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers,
60-
String stackTracePrinter) {
75+
private AotContribution(ClassLoader classLoader,
76+
Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers, String stackTracePrinter) {
77+
this.classLoader = classLoader;
6178
this.customizers = customizers;
6279
this.stackTracePrinter = stackTracePrinter;
6380
}
@@ -69,27 +86,11 @@ public void applyTo(GenerationContext generationContext,
6986
this.customizers.forEach((customizer) -> reflection.registerType(customizer,
7087
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
7188
if (this.stackTracePrinter != null) {
72-
reflection.registerTypeIfPresent(getClass().getClassLoader(), this.stackTracePrinter,
89+
reflection.registerTypeIfPresent(this.classLoader, this.stackTracePrinter,
7390
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
7491
}
7592
}
7693

77-
static AotContribution get(StructuredLoggingJsonProperties properties) {
78-
Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers = properties.customizer();
79-
String stackTracePrinter = getStackTracePrinter(properties);
80-
if (stackTracePrinter == null && customizers.isEmpty()) {
81-
return null;
82-
}
83-
return new AotContribution(customizers, stackTracePrinter);
84-
}
85-
86-
private static String getStackTracePrinter(StructuredLoggingJsonProperties properties) {
87-
return Optional.ofNullable(properties.stackTrace())
88-
.filter(StackTrace::hasCustomPrinter)
89-
.map(StackTrace::printer)
90-
.orElse(null);
91-
}
92-
9394
}
9495

9596
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void shouldRegisterRuntimeHintsWhenCustomizerIsPresent() {
6767
}
6868

6969
@Test
70-
void shouldRegisterRuntimeHintsWhenStackTracePrinterIsPresent() {
70+
void shouldRegisterRuntimeHintsWhenCustomStackTracePrinterIsPresent() {
7171
MockEnvironment environment = new MockEnvironment();
7272
environment.setProperty("logging.structured.json.stacktrace.printer", TestStackTracePrinter.class.getName());
7373

0 commit comments

Comments
 (0)