Skip to content

Commit 753bbf4

Browse files
committed
Register new Logback converter for reflection in a native image
See gh-39564
1 parent 3ba9478 commit 753bbf4

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackRuntimeHints.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ private void registerHintsForBuiltInLogbackConverters(ReflectionHints reflection
5858
}
5959

6060
private void registerHintsForSpringBootConverters(ReflectionHints reflection) {
61-
registerForPublicConstructorInvocation(reflection, ColorConverter.class,
61+
registerForPublicConstructorInvocation(reflection, ApplicationNameConverter.class, ColorConverter.class,
6262
ExtendedWhitespaceThrowableProxyConverter.class, WhitespaceThrowableProxyConverter.class,
6363
CorrelationIdConverter.class);
6464
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackRuntimeHintsTests.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,14 @@
1616

1717
package org.springframework.boot.logging.logback;
1818

19+
import java.io.IOException;
1920
import java.util.List;
2021
import java.util.function.Consumer;
22+
import java.util.stream.Stream;
2123

2224
import ch.qos.logback.classic.LoggerContext;
2325
import ch.qos.logback.classic.pattern.SyslogStartConverter;
26+
import ch.qos.logback.core.pattern.Converter;
2427
import ch.qos.logback.core.rolling.helper.DateTokenConverter;
2528
import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
2629
import org.junit.jupiter.api.Test;
@@ -30,6 +33,8 @@
3033
import org.springframework.aot.hint.ReflectionHints;
3134
import org.springframework.aot.hint.RuntimeHints;
3235
import org.springframework.aot.hint.TypeHint;
36+
import org.springframework.core.io.Resource;
37+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
3338

3439
import static org.assertj.core.api.Assertions.assertThat;
3540

@@ -54,11 +59,29 @@ void registersHintsForBuiltInLogbackConverters() {
5459
}
5560

5661
@Test
57-
void registersHintsForSpringBootConverters() throws LinkageError {
62+
void registersHintsForSpringBootConverters() throws IOException {
5863
ReflectionHints reflection = registerHints();
59-
assertThat(List.of(ColorConverter.class, ExtendedWhitespaceThrowableProxyConverter.class,
60-
WhitespaceThrowableProxyConverter.class))
61-
.allSatisfy(registeredForPublicConstructorInvocation(reflection));
64+
assertThat(converterClasses()).allSatisfy(registeredForPublicConstructorInvocation(reflection));
65+
}
66+
67+
@SuppressWarnings("unchecked")
68+
private Stream<Class<Converter<?>>> converterClasses() throws IOException {
69+
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
70+
return Stream.of(resolver.getResources("classpath:org/springframework/boot/logging/logback/*.class"))
71+
.filter(Resource::isFile)
72+
.map(this::loadClass)
73+
.filter(Converter.class::isAssignableFrom)
74+
.map((type) -> (Class<Converter<?>>) type);
75+
}
76+
77+
private Class<?> loadClass(Resource resource) {
78+
try {
79+
return getClass().getClassLoader()
80+
.loadClass("org.springframework.boot.logging.logback." + resource.getFilename().replace(".class", ""));
81+
}
82+
catch (ClassNotFoundException ex) {
83+
throw new RuntimeException(ex);
84+
}
6285
}
6386

6487
@Test

0 commit comments

Comments
 (0)