Skip to content

Commit babea65

Browse files
committed
Add convenience ClassUtil methods for checking JDK major version
1 parent 957ee39 commit babea65

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,57 @@ public static boolean isJDKClass(Class<?> rawType) {
11631163
return clsName.startsWith("java.") || clsName.startsWith("javax.");
11641164
}
11651165

1166+
/**
1167+
* Convenience method for:
1168+
*<pre>
1169+
* return getJDKMajorVersion() >= 17
1170+
*</pre>
1171+
* that also catches any possible exceptions so it is safe to call
1172+
* from static contexts.
1173+
*
1174+
* @return {@code True} if we can determine that the code is running on
1175+
* JDK 17 or above; {@code false} otherwise.
1176+
*
1177+
* @since 2.15
1178+
*/
1179+
public static boolean isJDK17OrAbove() {
1180+
try {
1181+
return getJDKMajorVersion() >= 17;
1182+
} catch (Throwable t) {
1183+
System.err.println("Failed to determine JDK major version, assuming pre-JDK-17; problem: "+t);
1184+
return false;
1185+
}
1186+
}
1187+
1188+
/**
1189+
* @return Major version of JDK we are running on
1190+
*
1191+
* @throws IllegalStateException If JDK version information cannot be determined
1192+
*
1193+
* @since 2.15
1194+
*/
1195+
public static int getJDKMajorVersion() {
1196+
String version;
1197+
1198+
try {
1199+
version = System.getProperty("java.version");
1200+
} catch (SecurityException e) {
1201+
throw new IllegalStateException("Could not access 'java.version': cannot determine JDK major version");
1202+
}
1203+
if (version.startsWith("1.")) {
1204+
// 25-Nov-2022, tatu: We'll consider JDK 8 to be the baseline since
1205+
// Jackson 2.15+ only runs on 8 and above
1206+
return 8;
1207+
}
1208+
int dotIndex = version.indexOf(".");
1209+
String cleaned = (dotIndex < 0) ? version : version.substring(0, dotIndex);
1210+
try {
1211+
return Integer.parseInt(cleaned);
1212+
} catch (NumberFormatException e) {
1213+
throw new IllegalStateException("Invalid JDK version String '"+version+"' cannot determine JDK major version");
1214+
}
1215+
}
1216+
11661217
/*
11671218
/**********************************************************
11681219
/* Access to various Class definition aspects; possibly

src/test/java/com/fasterxml/jackson/databind/util/ClassUtilTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ static void throwsException() {
328328
throw new IllegalArgumentException("A custom message");
329329
}
330330

331+
public void testJDKChecks() {
332+
int version = ClassUtil.getJDKMajorVersion();
333+
assertTrue(version > 0);
334+
335+
assertEquals((version >= 17), ClassUtil.isJDK17OrAbove());
336+
}
337+
331338
/*
332339
/**********************************************************
333340
/* Test methods, deprecated

0 commit comments

Comments
 (0)