Skip to content

Commit 5ca6771

Browse files
committed
Add runtime hints for JavaVersion detection
Closes gh-47619
1 parent 9af4aef commit 5ca6771

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/JavaVersion.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.Console;
2020
import java.io.Reader;
21+
import java.lang.reflect.Method;
2122
import java.text.NumberFormat;
2223
import java.time.Duration;
2324
import java.util.Arrays;
@@ -26,6 +27,9 @@
2627
import java.util.SortedSet;
2728
import java.util.concurrent.Future;
2829

30+
import org.springframework.aot.hint.ExecutableMode;
31+
import org.springframework.aot.hint.RuntimeHints;
32+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2933
import org.springframework.util.ClassUtils;
3034

3135
/**
@@ -90,8 +94,17 @@ public enum JavaVersion {
9094

9195
private final boolean available;
9296

97+
private final Class<?> versionSpecificClass;
98+
99+
private final String versionSpecificMethod;
100+
101+
private final Class<?>[] paramTypes;
102+
93103
JavaVersion(String name, Class<?> versionSpecificClass, String versionSpecificMethod, Class<?>... paramTypes) {
94104
this.name = name;
105+
this.versionSpecificClass = versionSpecificClass;
106+
this.versionSpecificMethod = versionSpecificMethod;
107+
this.paramTypes = paramTypes;
95108
this.available = ClassUtils.hasMethod(versionSpecificClass, versionSpecificMethod, paramTypes);
96109
}
97110

@@ -133,4 +146,28 @@ public boolean isOlderThan(JavaVersion version) {
133146
return compareTo(version) < 0;
134147
}
135148

149+
static class Hints implements RuntimeHintsRegistrar {
150+
151+
@Override
152+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
153+
for (JavaVersion javaVersion : JavaVersion.values()) {
154+
Method method = findMethod(javaVersion);
155+
if (method != null) {
156+
hints.reflection().registerMethod(method, ExecutableMode.INTROSPECT);
157+
}
158+
}
159+
}
160+
161+
private Method findMethod(JavaVersion javaVersion) {
162+
try {
163+
return ClassUtils.getMethod(javaVersion.versionSpecificClass, javaVersion.versionSpecificMethod,
164+
javaVersion.paramTypes);
165+
}
166+
catch (IllegalStateException ex) {
167+
return null;
168+
}
169+
}
170+
171+
}
172+
136173
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ org.springframework.boot.logging.logback.LogbackRuntimeHints,\
1313
org.springframework.boot.logging.structured.ElasticCommonSchemaProperties$ElasticCommonSchemaPropertiesRuntimeHints,\
1414
org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties$GraylogExtendedLogFormatPropertiesRuntimeHints,\
1515
org.springframework.boot.logging.structured.StructuredLoggingJsonProperties$StructuredLoggingJsonPropertiesRuntimeHints,\
16+
org.springframework.boot.system.JavaVersion$Hints,\
1617
org.springframework.boot.web.embedded.undertow.UndertowWebServer$UndertowWebServerRuntimeHints,\
1718
org.springframework.boot.web.server.MimeMappings$MimeMappingsRuntimeHints
1819

0 commit comments

Comments
 (0)