@@ -1163,6 +1163,57 @@ public static boolean isJDKClass(Class<?> rawType) {
1163
1163
return clsName .startsWith ("java." ) || clsName .startsWith ("javax." );
1164
1164
}
1165
1165
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
+
1166
1217
/*
1167
1218
/**********************************************************
1168
1219
/* Access to various Class definition aspects; possibly
0 commit comments