Update to protect against NPEs
Tidy up Java version code Add Java version int to supplement float git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137336 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
34cca2038a
commit
d68d21856d
|
@ -66,7 +66,7 @@ package org.apache.commons.lang;
|
|||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @author Michael Becke
|
||||
* @since 1.0
|
||||
* @version $Id: SystemUtils.java,v 1.10 2003/05/22 21:51:26 scolebourne Exp $
|
||||
* @version $Id: SystemUtils.java,v 1.11 2003/05/24 12:06:37 scolebourne Exp $
|
||||
*/
|
||||
public class SystemUtils {
|
||||
|
||||
|
@ -81,6 +81,7 @@ public class SystemUtils {
|
|||
public SystemUtils() {
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* The <code>file.separator</code> System Property.
|
||||
* File separator ("/" on UNIX).
|
||||
|
@ -91,7 +92,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.class.path</code> System Property.
|
||||
* Java class path.
|
||||
|
@ -102,7 +102,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.class.version</code> System Property.
|
||||
* Java class format version number.
|
||||
|
@ -113,7 +112,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.compiler</code> System Property.
|
||||
* Name of JIT compiler to use.
|
||||
|
@ -124,7 +122,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_COMPILER = getSystemProperty("java.compiler");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.ext.dirs</code> System Property.
|
||||
* Path of extension directory or directories.
|
||||
|
@ -135,7 +132,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.home</code> System Property.
|
||||
* Java installation directory.
|
||||
|
@ -146,7 +142,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_HOME = getSystemProperty("java.home");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.io.tmpdir</code> System Property.
|
||||
* Default temp file path.
|
||||
|
@ -157,7 +152,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_IO_TMPDIR = getSystemProperty("java.io.tmpdir");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.library.path</code> System Property.
|
||||
* List of paths to search when loading libraries.
|
||||
|
@ -168,7 +162,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.specification.name</code> System Property.
|
||||
* Java Runtime Environment specification name.
|
||||
|
@ -179,7 +172,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.specification.vendor</code> System Property.
|
||||
* Java Runtime Environment specification vendor.
|
||||
|
@ -190,7 +182,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.specification.version</code> System Property.
|
||||
* Java Runtime Environment specification version.
|
||||
|
@ -201,7 +192,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vendor</code> System Property.
|
||||
* Java vendor-specific string.
|
||||
|
@ -212,7 +202,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VENDOR = getSystemProperty("java.vendor");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vendor.url</code> System Property.
|
||||
* Java vendor URL.
|
||||
|
@ -223,7 +212,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.version</code> System Property.
|
||||
* Java version number.
|
||||
|
@ -234,7 +222,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VERSION = getSystemProperty("java.version");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vm.name</code> System Property.
|
||||
* Java Virtual Machine implementation name.
|
||||
|
@ -245,7 +232,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vm.specification.name</code> System Property.
|
||||
* Java Virtual Machine specification name.
|
||||
|
@ -256,7 +242,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vm.specification.vendor</code> System Property.
|
||||
* Java Virtual Machine specification vendor.
|
||||
|
@ -267,7 +252,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vm.specification.version</code> System Property.
|
||||
* Java Virtual Machine specification version.
|
||||
|
@ -278,7 +262,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vm.vendor</code> System Property.
|
||||
* Java Virtual Machine implementation vendor.
|
||||
|
@ -289,7 +272,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>java.vm.version</code> System Property.
|
||||
* Java Virtual Machine implementation version.
|
||||
|
@ -300,7 +282,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>line.separator</code> System Property.
|
||||
* Line separator ("\n" on UNIX).
|
||||
|
@ -311,7 +292,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>os.arch</code> System Property.
|
||||
* Operating system architecture.
|
||||
|
@ -322,7 +302,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String OS_ARCH = getSystemProperty("os.arch");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>os.name</code> System Property.
|
||||
* Operating system name.
|
||||
|
@ -333,7 +312,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String OS_NAME = getSystemProperty("os.name");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>os.version</code> System Property.
|
||||
* Operating system version.
|
||||
|
@ -344,7 +322,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String OS_VERSION = getSystemProperty("os.version");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>path.separator</code> System Property.
|
||||
* Path separator (":" on UNIX).
|
||||
|
@ -355,7 +332,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String PATH_SEPARATOR = getSystemProperty("path.separator");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>user.dir</code> System Property.
|
||||
* User's current working directory.
|
||||
|
@ -366,7 +342,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String USER_DIR = getSystemProperty("user.dir");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>user.home</code> System Property.
|
||||
* User's home directory.
|
||||
|
@ -377,7 +352,6 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String USER_HOME = getSystemProperty("user.home");
|
||||
|
||||
|
||||
/**
|
||||
* The <code>user.name</code> System Property.
|
||||
* User's account name.
|
||||
|
@ -388,33 +362,68 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String USER_NAME = getSystemProperty("user.name");
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).
|
||||
* <p>
|
||||
* The field will return false if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final boolean IS_JAVA_1_1 = JAVA_VERSION.startsWith("1.1.");
|
||||
public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
|
||||
|
||||
/**
|
||||
* Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).
|
||||
* <p>
|
||||
* The field will return false if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final boolean IS_JAVA_1_2 = JAVA_VERSION.startsWith("1.2.");
|
||||
public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
|
||||
|
||||
/**
|
||||
* Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).
|
||||
* <p>
|
||||
* The field will return false if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final boolean IS_JAVA_1_3 = JAVA_VERSION.startsWith("1.3.");
|
||||
public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
|
||||
|
||||
/**
|
||||
* Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).
|
||||
* <p>
|
||||
* The field will return false if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final boolean IS_JAVA_1_4 = JAVA_VERSION.startsWith("1.4.");
|
||||
public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
|
||||
|
||||
/**
|
||||
* Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).
|
||||
* <p>
|
||||
* The field will return false if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final boolean IS_JAVA_1_5 = JAVA_VERSION.startsWith("1.5.");
|
||||
public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Gets the Java version as a <code>float</code>.
|
||||
* Example output:
|
||||
* <ul>
|
||||
* <li><code>1.2f</code> for JDK 1.2
|
||||
* <li><code>1.31f</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
* <p>
|
||||
* The field will return zero if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
|
||||
|
||||
/**
|
||||
* Gets the Java version as an <code>int</code>.
|
||||
* Example output:
|
||||
* <ul>
|
||||
* <li><code>120</code> for JDK 1.2
|
||||
* <li><code>131</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
* <p>
|
||||
* The field will return zero if <code>JAVA_VERSION</code> is <code>null</code>.
|
||||
*/
|
||||
public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Gets a System property, defaulting to the given value if the property
|
||||
* cannot be read.
|
||||
|
@ -428,10 +437,81 @@ public class SystemUtils {
|
|||
|
||||
} catch (SecurityException ex) {
|
||||
// we are not allowed to look at this property
|
||||
System.err.println(
|
||||
"SecurityException thrown when reading system property '" + property
|
||||
+ "' - property value will default to null"
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides if the java version matches.
|
||||
*
|
||||
* @param versionPrefix the prefix for the java version
|
||||
* @return true if matches, or false if not or can't determine
|
||||
*/
|
||||
private static boolean getJavaVersionMatches(String versionPrefix) {
|
||||
if (JAVA_VERSION == null) {
|
||||
return false;
|
||||
}
|
||||
return JAVA_VERSION.startsWith(versionPrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Get the Java version number as a <code>float</code>.</p>
|
||||
*
|
||||
* <p>Example output:</p>
|
||||
* <ul>
|
||||
* <li><code>1.2f</code> for JDK 1.2
|
||||
* <li><code>1.31f</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
*
|
||||
* <p>Patch releases are not reported.
|
||||
* Zero is returned if JAVA_VERSION is <code>null</code>.</p>
|
||||
*
|
||||
* @return the version, for example 1.31f for JDK 1.3.1
|
||||
*/
|
||||
private static float getJavaVersionAsFloat() {
|
||||
if (JAVA_VERSION == null) {
|
||||
return 0f;
|
||||
}
|
||||
String str = JAVA_VERSION.substring(0, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
}
|
||||
return Float.parseFloat(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Get the Java version number as an <code>int</code>.</p>
|
||||
*
|
||||
* <p>Example output:</p>
|
||||
* <ul>
|
||||
* <li><code>120</code> for JDK 1.2
|
||||
* <li><code>131</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
*
|
||||
* <p>Patch releases are not reported.
|
||||
* Zero is returned if JAVA_VERSION is <code>null</code>.</p>
|
||||
*
|
||||
* @return the version, for example 131 for JDK 1.3.1
|
||||
*/
|
||||
private static int getJavaVersionAsInt() {
|
||||
if (JAVA_VERSION == null) {
|
||||
return 0;
|
||||
}
|
||||
String str = JAVA_VERSION.substring(0, 1);
|
||||
str = str + JAVA_VERSION.substring(2, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
} else {
|
||||
str = str + "0";
|
||||
}
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// Parsing operating system may stay here, or it may be moved somewhere else entirely
|
||||
// /** True iff this is running on Windows */
|
||||
// public static final boolean IS_WINDOWS;
|
||||
|
@ -531,6 +611,7 @@ public class SystemUtils {
|
|||
// }
|
||||
// }
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Get the Java version number as a <code>float</code>.</p>
|
||||
*
|
||||
|
@ -540,23 +621,21 @@ public class SystemUtils {
|
|||
* <li><code>1.31f</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
*
|
||||
* @deprecated Use JAVA_VERSION_FLOAT instead.
|
||||
* Will be removed in Commons Lang 3.0.
|
||||
* @return the version, for example 1.31f for JDK 1.3.1
|
||||
*/
|
||||
public static float getJavaVersion() {
|
||||
String str = JAVA_VERSION.substring(0, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
}
|
||||
return Float.parseFloat(str);
|
||||
return JAVA_VERSION_FLOAT;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Is the Java version at the the requested version.</p>
|
||||
* <p>Is the Java version at least the requested version.</p>
|
||||
*
|
||||
* <p>Example input:</p>
|
||||
* <ul>
|
||||
* <li><code>1.2f</code> for JDK 1.2
|
||||
* <li><code>1.31f</code> for JDK 1.3.1
|
||||
* <li><code>1.2f</code> to test for JDK 1.2
|
||||
* <li><code>1.31f</code> to test for JDK 1.3.1
|
||||
* </ul>
|
||||
*
|
||||
* @param requiredVersion the required version, for example 1.31f
|
||||
|
@ -564,7 +643,24 @@ public class SystemUtils {
|
|||
* than the required version
|
||||
*/
|
||||
public static boolean isJavaVersionAtLeast(float requiredVersion) {
|
||||
return (getJavaVersion() >= requiredVersion);
|
||||
return (JAVA_VERSION_FLOAT >= requiredVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Is the Java version at least the requested version.</p>
|
||||
*
|
||||
* <p>Example input:</p>
|
||||
* <ul>
|
||||
* <li><code>120</code> to test for JDK 1.2 or greater
|
||||
* <li><code>131</code> to test for JDK 1.3.1 or greater
|
||||
* </ul>
|
||||
*
|
||||
* @param requiredVersion the required version, for example 131
|
||||
* @return <code>true</code> if the actual version is equal or greater
|
||||
* than the required version
|
||||
*/
|
||||
public static boolean isJavaVersionAtLeast(int requiredVersion) {
|
||||
return (JAVA_VERSION_INT >= requiredVersion);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue