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