001package org.slf4j.log4j12;
002
003import java.lang.reflect.Method;
004
005import org.slf4j.helpers.Util;
006
007public class VersionUtil {
008    // code was compiled under Java 8 or later
009    static final int MINIMAL_VERSION = 8;
010
011    static public int getJavaMajorVersion() {
012        String javaVersionString = Util.safeGetSystemProperty("java.version");
013        return getJavaMajorVersion(javaVersionString);
014    }
015
016    static public int getJavaMajorVersion(String versionString) {
017        if (versionString == null)
018            return MINIMAL_VERSION;
019        if (versionString.startsWith("1.")) {
020            return versionString.charAt(2) - '0';
021        } else {
022            // we running under Java 9 or later
023            try {
024                Method versionMethod = Runtime.class.getMethod("version");
025                Object versionObj = versionMethod.invoke(null);
026                Method majorMethod = versionObj.getClass().getMethod("major");
027                Integer resultInteger = (Integer) majorMethod.invoke(versionObj);
028                return resultInteger.intValue();
029            } catch (Exception e) {
030                return MINIMAL_VERSION;
031            }
032        }
033    }
034}