一、前言
对于datanucleus-core源码包中提供的org.datanucleus.util.JavaUtils工具类,对jre版本的范围判断isJRE1_5OrAbove/isJRE1_6OrAbove,获取JRE主版本号getJREMajorVersion、初始化JRE版本initialiseJREVersion、版本大小对比isGreaterEqualsThan等。
二、源码说明
package org.datanucleus.util;@b@@b@import java.util.StringTokenizer;@b@@b@public class JavaUtils@b@{@b@ private static boolean versionInitialised = false;@b@ private static int majorVersion = 1;@b@ private static int minorVersion = 0;@b@ private static int isJRE15 = -1;@b@ private static int isJRE16 = -1;@b@@b@ public static boolean isJRE1_5OrAbove()@b@ {@b@ if (isJRE15 == -1)@b@ {@b@ try@b@ {@b@ Class.forName("java.util.Queue");@b@ isJRE15 = 1;@b@ }@b@ catch (Exception e)@b@ {@b@ isJRE15 = 0;@b@ }@b@ }@b@ return (isJRE15 == 1);@b@ }@b@@b@ public static boolean isJRE1_6OrAbove()@b@ {@b@ if (isJRE16 == -1)@b@ {@b@ try@b@ {@b@ Class.forName("java.util.Deque");@b@ isJRE16 = 1;@b@ }@b@ catch (Exception e)@b@ {@b@ isJRE16 = 0;@b@ }@b@ }@b@ return (isJRE16 == 1);@b@ }@b@@b@ public static int getJREMajorVersion()@b@ {@b@ if (!(versionInitialised))@b@ {@b@ initialiseJREVersion();@b@ }@b@ return majorVersion;@b@ }@b@@b@ public static int getJREMinorVersion()@b@ {@b@ if (!(versionInitialised))@b@ {@b@ initialiseJREVersion();@b@ }@b@ return minorVersion;@b@ }@b@@b@ private static void initialiseJREVersion()@b@ {@b@ String version = System.getProperty("java.version");@b@@b@ StringTokenizer tokeniser = new StringTokenizer(version, ".");@b@ String token = tokeniser.nextToken();@b@ try@b@ {@b@ Integer ver = Integer.valueOf(token);@b@ majorVersion = ver.intValue();@b@@b@ token = tokeniser.nextToken();@b@ ver = Integer.valueOf(token);@b@ minorVersion = ver.intValue();@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ versionInitialised = true;@b@ }@b@@b@ public static boolean isGreaterEqualsThan(String version)@b@ {@b@ boolean greaterEquals = false;@b@ StringTokenizer tokeniser = new StringTokenizer(version, ".");@b@ String token = tokeniser.nextToken();@b@ try@b@ {@b@ Integer ver = Integer.valueOf(token);@b@ int majorVersion = ver.intValue();@b@@b@ token = tokeniser.nextToken();@b@ ver = Integer.valueOf(token);@b@ int minorVersion = ver.intValue();@b@ if ((getJREMajorVersion() >= majorVersion) && (getJREMinorVersion() >= minorVersion))@b@ {@b@ greaterEquals = true;@b@ }@b@@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ return greaterEquals;@b@ }@b@@b@ public static boolean isEqualsThan(String version)@b@ {@b@ boolean equals = false;@b@ StringTokenizer tokeniser = new StringTokenizer(version, ".");@b@ String token = tokeniser.nextToken();@b@ try@b@ {@b@ Integer ver = Integer.valueOf(token);@b@ int majorVersion = ver.intValue();@b@@b@ token = tokeniser.nextToken();@b@ ver = Integer.valueOf(token);@b@ int minorVersion = ver.intValue();@b@ if ((getJREMajorVersion() == majorVersion) && (getJREMinorVersion() == minorVersion))@b@ {@b@ equals = true;@b@ }@b@@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ return equals;@b@ }@b@}