|
| 1 | +/** |
| 2 | + * Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland) |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | + * a copy of this software and associated documentation files (the |
| 7 | + * "Software"), to deal in the Software without restriction, including |
| 8 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | + * permit persons to whom the Software is furnished to do so, subject to |
| 11 | + * the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + * |
| 24 | + */ |
1 | 25 | package org.slf4j.impl;
|
2 | 26 |
|
3 |
| -import java.lang.reflect.Method; |
4 |
| - |
5 | 27 | import org.slf4j.helpers.Util;
|
6 | 28 |
|
7 | 29 | public class VersionUtil {
|
8 |
| - static final int MINIMAL_VERSION = 5; |
| 30 | + static final int DEFAULT_GUESS = 8; |
| 31 | + |
| 32 | + static public int getJavaMajorVersion() { |
| 33 | + String javaVersionString = Util.safeGetSystemProperty("java.version"); |
| 34 | + return getJavaMajorVersion(javaVersionString); |
| 35 | + } |
9 | 36 |
|
10 |
| - static public int getJavaMajorVersion() { |
11 |
| - String javaVersionString = Util.safeGetSystemProperty("java.version"); |
12 |
| - return getJavaMajorVersion(javaVersionString); |
13 |
| - } |
| 37 | + static public int getJavaMajorVersion(String versionString) { |
| 38 | + if (versionString == null) |
| 39 | + return DEFAULT_GUESS; |
| 40 | + if (versionString.startsWith("1.")) { |
| 41 | + return versionString.charAt(2) - '0'; |
| 42 | + } else { |
| 43 | + String firstDigits = extractFirstDigits(versionString); |
| 44 | + try { |
| 45 | + return Integer.parseInt(firstDigits); |
| 46 | + } catch(NumberFormatException e) { |
| 47 | + return DEFAULT_GUESS; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
14 | 51 |
|
15 |
| - static int getJavaMajorVersion(String versionString) { |
16 |
| - if (versionString == null) |
17 |
| - return MINIMAL_VERSION; |
18 |
| - if (versionString.startsWith("1.")) { |
19 |
| - return versionString.charAt(2) - '0'; |
20 |
| - } else { |
21 |
| - // we running under Java 9 or later |
22 |
| - try { |
23 |
| - Method versionMethod = Runtime.class.getMethod("version"); |
24 |
| - Object versionObj = versionMethod.invoke(null); |
25 |
| - Method majorMethod = versionObj.getClass().getMethod("major"); |
26 |
| - Integer resultInteger = (Integer) majorMethod.invoke(versionObj); |
27 |
| - return resultInteger.intValue(); |
28 |
| - } catch (Exception e) { |
29 |
| - return MINIMAL_VERSION; |
30 |
| - } |
31 |
| - } |
32 |
| - } |
| 52 | + private static String extractFirstDigits(String versionString) { |
| 53 | + StringBuffer buf = new StringBuffer(); |
| 54 | + for(char c : versionString.toCharArray()) { |
| 55 | + if(Character.isDigit(c)) |
| 56 | + buf.append(c); |
| 57 | + else |
| 58 | + break; |
| 59 | + } |
| 60 | + return buf.toString(); |
| 61 | + |
| 62 | + } |
33 | 63 | }
|
0 commit comments