|
38 | 38 | import java.net.ServerSocket; |
39 | 39 | import java.nio.ByteBuffer; |
40 | 40 | import java.nio.ByteOrder; |
| 41 | +import java.security.AccessController; |
| 42 | +import java.security.PrivilegedAction; |
41 | 43 | import java.util.Deque; |
42 | 44 | import java.util.List; |
43 | 45 | import java.util.Locale; |
44 | 46 | import java.util.Map; |
45 | 47 | import java.util.Queue; |
46 | | -import java.util.concurrent.BlockingQueue; |
47 | 48 | import java.util.concurrent.ConcurrentHashMap; |
48 | 49 | import java.util.concurrent.ConcurrentLinkedDeque; |
49 | 50 | import java.util.concurrent.ConcurrentMap; |
@@ -1018,51 +1019,48 @@ private static boolean isRoot0() { |
1018 | 1019 | return false; |
1019 | 1020 | } |
1020 | 1021 |
|
1021 | | - @SuppressWarnings("LoopStatementThatDoesntLoop") |
1022 | 1022 | private static int javaVersion0() { |
1023 | | - int javaVersion; |
| 1023 | + final int majorVersion; |
1024 | 1024 |
|
1025 | | - // Not really a loop |
1026 | | - for (;;) { |
1027 | | - // Android |
1028 | | - if (isAndroid()) { |
1029 | | - javaVersion = 6; |
1030 | | - break; |
1031 | | - } |
| 1025 | + if (isAndroid()) { |
| 1026 | + majorVersion = 6; |
| 1027 | + } else { |
| 1028 | + majorVersion = majorVersionFromJavaSpecificationVersion(); |
| 1029 | + } |
1032 | 1030 |
|
1033 | | - try { |
1034 | | - Method getVersion = java.lang.Runtime.class.getMethod("version"); |
1035 | | - Object version = getVersion.invoke(null); |
1036 | | - javaVersion = (Integer) version.getClass().getMethod("major").invoke(version); |
1037 | | - break; |
1038 | | - } catch (Throwable ignored) { |
1039 | | - // Ignore |
1040 | | - } |
| 1031 | + logger.debug("Java version: {}", majorVersion); |
1041 | 1032 |
|
1042 | | - try { |
1043 | | - Class.forName("java.time.Clock", false, getClassLoader(Object.class)); |
1044 | | - javaVersion = 8; |
1045 | | - break; |
1046 | | - } catch (Throwable ignored) { |
1047 | | - // Ignore |
1048 | | - } |
| 1033 | + return majorVersion; |
| 1034 | + } |
1049 | 1035 |
|
1050 | | - try { |
1051 | | - Class.forName("java.util.concurrent.LinkedTransferQueue", false, getClassLoader(BlockingQueue.class)); |
1052 | | - javaVersion = 7; |
1053 | | - break; |
1054 | | - } catch (Throwable ignored) { |
1055 | | - // Ignore |
1056 | | - } |
| 1036 | + static int majorVersionFromJavaSpecificationVersion() { |
| 1037 | + try { |
| 1038 | + final String javaSpecVersion = AccessController.doPrivileged(new PrivilegedAction<String>() { |
| 1039 | + @Override |
| 1040 | + public String run() { |
| 1041 | + return System.getProperty("java.specification.version"); |
| 1042 | + } |
| 1043 | + }); |
| 1044 | + return majorVersion(javaSpecVersion); |
| 1045 | + } catch (SecurityException e) { |
| 1046 | + logger.debug("security exception while reading java.specification.version", e); |
| 1047 | + return 6; |
| 1048 | + } |
| 1049 | + } |
1057 | 1050 |
|
1058 | | - javaVersion = 6; |
1059 | | - break; |
| 1051 | + static int majorVersion(final String javaSpecVersion) { |
| 1052 | + final String[] components = javaSpecVersion.split("\\."); |
| 1053 | + final int[] version = new int[components.length]; |
| 1054 | + for (int i = 0; i < components.length; i++) { |
| 1055 | + version[i] = Integer.parseInt(components[i]); |
1060 | 1056 | } |
1061 | 1057 |
|
1062 | | - if (logger.isDebugEnabled()) { |
1063 | | - logger.debug("Java version: {}", javaVersion); |
| 1058 | + if (version[0] == 1) { |
| 1059 | + assert version[1] >= 6; |
| 1060 | + return version[1]; |
| 1061 | + } else { |
| 1062 | + return version[0]; |
1064 | 1063 | } |
1065 | | - return javaVersion; |
1066 | 1064 | } |
1067 | 1065 |
|
1068 | 1066 | private static boolean hasUnsafe0() { |
|
0 commit comments