|
10 | 10 | import java.lang.reflect.Type; |
11 | 11 | import java.util.Arrays; |
12 | 12 |
|
13 | | -import com.cedarsoftware.util.ReflectionUtils; |
14 | | - |
15 | 13 | import com.cedarsoftware.io.JsonIoException; |
16 | | -import com.cedarsoftware.util.ArrayUtilities; |
17 | | -import com.cedarsoftware.util.CollectionUtilities; |
18 | 14 | import com.cedarsoftware.util.Converter; |
| 15 | +import com.cedarsoftware.util.ReflectionUtils; |
19 | 16 | import com.cedarsoftware.util.StringUtilities; |
20 | 17 |
|
21 | 18 | /** |
@@ -66,7 +63,7 @@ public class Injector { |
66 | 63 | private static final Class<?> VAR_HANDLE_CLASS; // although appears unused, it is intentional for caching |
67 | 64 |
|
68 | 65 | static { |
69 | | - int javaVersion = getJavaVersion(); |
| 66 | + int javaVersion = determineJdkMajorVersion(); |
70 | 67 | IS_JDK17_OR_HIGHER = javaVersion >= 17; |
71 | 68 |
|
72 | 69 | Object lookup = null; |
@@ -408,21 +405,32 @@ public String getUniqueFieldName() { |
408 | 405 | return uniqueFieldName; |
409 | 406 | } |
410 | 407 |
|
411 | | - /** |
412 | | - * Determines the Java major version by parsing the {@code java.version} system property. |
413 | | - * This method handles both legacy (1.8) and modern (9+) version numbering schemes. |
414 | | - * |
415 | | - * @return the major Java version (e.g., 8 for Java 1.8, 17 for Java 17, etc.) |
416 | | - */ |
417 | | - private static int getJavaVersion() { |
418 | | - String version = System.getProperty("java.version"); |
419 | | - if (version.startsWith("1.")) { |
420 | | - return Integer.parseInt(version.substring(2, 3)); |
421 | | - } |
422 | | - int dot = version.indexOf('.'); |
423 | | - if (dot != -1) { |
424 | | - return Integer.parseInt(version.substring(0, dot)); |
| 408 | + //TODO: remove and wire to java-util API for this (3.8.0+) |
| 409 | + private static int determineJdkMajorVersion() { |
| 410 | + try { |
| 411 | + Method versionMethod = ReflectionUtils.getMethod(Runtime.class, "version"); |
| 412 | + Object v = versionMethod.invoke(Runtime.getRuntime()); |
| 413 | + Method major = ReflectionUtils.getMethod(v.getClass(), "major"); |
| 414 | + return (Integer) major.invoke(v); |
| 415 | + } catch (Exception ignore) { |
| 416 | + try { |
| 417 | + String version = System.getProperty("java.version"); |
| 418 | + if (version.startsWith("1.")) { |
| 419 | + return Integer.parseInt(version.substring(2, 3)); |
| 420 | + } |
| 421 | + int dot = version.indexOf('.'); |
| 422 | + if (dot != -1) { |
| 423 | + return Integer.parseInt(version.substring(0, dot)); |
| 424 | + } |
| 425 | + return Integer.parseInt(version); |
| 426 | + } catch (Exception ignored) { |
| 427 | + try { |
| 428 | + String spec = System.getProperty("java.specification.version"); |
| 429 | + return spec.startsWith("1.") ? Integer.parseInt(spec.substring(2)) : Integer.parseInt(spec); |
| 430 | + } catch (NumberFormatException e) { |
| 431 | + return -1; |
| 432 | + } |
| 433 | + } |
425 | 434 | } |
426 | | - return Integer.parseInt(version); |
427 | 435 | } |
428 | 436 | } |
0 commit comments