|
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; |
@@ -773,51 +774,48 @@ private static boolean isRoot0() { |
773 | 774 | return false; |
774 | 775 | } |
775 | 776 |
|
776 | | - @SuppressWarnings("LoopStatementThatDoesntLoop") |
777 | 777 | private static int javaVersion0() { |
778 | | - int javaVersion; |
| 778 | + final int majorVersion; |
779 | 779 |
|
780 | | - // Not really a loop |
781 | | - for (;;) { |
782 | | - // Android |
783 | | - if (isAndroid()) { |
784 | | - javaVersion = 6; |
785 | | - break; |
786 | | - } |
| 780 | + if (isAndroid()) { |
| 781 | + majorVersion = 6; |
| 782 | + } else { |
| 783 | + majorVersion = majorVersionFromJavaSpecificationVersion(); |
| 784 | + } |
787 | 785 |
|
788 | | - try { |
789 | | - Method getVersion = java.lang.Runtime.class.getMethod("version"); |
790 | | - Object version = getVersion.invoke(null); |
791 | | - javaVersion = (Integer) version.getClass().getMethod("major").invoke(version); |
792 | | - break; |
793 | | - } catch (Throwable ignored) { |
794 | | - // Ignore |
795 | | - } |
| 786 | + logger.debug("Java version: {}", majorVersion); |
796 | 787 |
|
797 | | - try { |
798 | | - Class.forName("java.time.Clock", false, getClassLoader(Object.class)); |
799 | | - javaVersion = 8; |
800 | | - break; |
801 | | - } catch (Throwable ignored) { |
802 | | - // Ignore |
803 | | - } |
| 788 | + return majorVersion; |
| 789 | + } |
804 | 790 |
|
805 | | - try { |
806 | | - Class.forName("java.util.concurrent.LinkedTransferQueue", false, getClassLoader(BlockingQueue.class)); |
807 | | - javaVersion = 7; |
808 | | - break; |
809 | | - } catch (Throwable ignored) { |
810 | | - // Ignore |
811 | | - } |
| 791 | + static int majorVersionFromJavaSpecificationVersion() { |
| 792 | + try { |
| 793 | + final String javaSpecVersion = AccessController.doPrivileged(new PrivilegedAction<String>() { |
| 794 | + @Override |
| 795 | + public String run() { |
| 796 | + return System.getProperty("java.specification.version"); |
| 797 | + } |
| 798 | + }); |
| 799 | + return majorVersion(javaSpecVersion); |
| 800 | + } catch (SecurityException e) { |
| 801 | + logger.debug("security exception while reading java.specification.version", e); |
| 802 | + return 6; |
| 803 | + } |
| 804 | + } |
812 | 805 |
|
813 | | - javaVersion = 6; |
814 | | - break; |
| 806 | + static int majorVersion(final String javaSpecVersion) { |
| 807 | + final String[] components = javaSpecVersion.split("\\."); |
| 808 | + final int[] version = new int[components.length]; |
| 809 | + for (int i = 0; i < components.length; i++) { |
| 810 | + version[i] = Integer.parseInt(components[i]); |
815 | 811 | } |
816 | 812 |
|
817 | | - if (logger.isDebugEnabled()) { |
818 | | - logger.debug("Java version: {}", javaVersion); |
| 813 | + if (version[0] == 1) { |
| 814 | + assert version[1] >= 6; |
| 815 | + return version[1]; |
| 816 | + } else { |
| 817 | + return version[0]; |
819 | 818 | } |
820 | | - return javaVersion; |
821 | 819 | } |
822 | 820 |
|
823 | 821 | private static boolean hasUnsafe0() { |
|
0 commit comments