< prev index next >
jdk/test/sun/misc/Version/Version.java
Print this page
*** 29,169 ****
* @compile -XDignore.symbol.file Version.java
* @run main Version
*/
import static sun.misc.Version.*;
public class Version {
public static void main(String[] args) throws Exception {
VersionInfo jdk = newVersionInfo(System.getProperty("java.runtime.version"));
VersionInfo v1 = new VersionInfo(jdkMajorVersion(),
jdkMinorVersion(),
jdkSecurityVersion(),
! jdkUpdateVersion(),
! jdkSpecialVersion(),
jdkBuildNumber());
System.out.println("JDK version = " + jdk + " " + v1);
if (!jdk.equals(v1)) {
throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1);
}
VersionInfo jvm = newVersionInfo(System.getProperty("java.vm.version"));
VersionInfo v2 = new VersionInfo(jvmMajorVersion(),
jvmMinorVersion(),
jvmSecurityVersion(),
! jvmUpdateVersion(),
! jvmSpecialVersion(),
jvmBuildNumber());
System.out.println("JVM version = " + jvm + " " + v2);
if (!jvm.equals(v2)) {
throw new RuntimeException("Unmatched version: " + jvm + " vs " + v2);
}
}
static class VersionInfo {
final int major;
final int minor;
! final int micro;
! final int update;
! final String special;
final int build;
! VersionInfo(int major, int minor, int micro,
! int update, String special, int build) {
this.major = major;
this.minor = minor;
! this.micro = micro;
! this.update = update;
! this.special = special;
this.build = build;
}
public boolean equals(VersionInfo v) {
return (this.major == v.major && this.minor == v.minor &&
! this.micro == v.micro && this.update == v.update &&
! this.special.equals(v.special) && this.build == v.build);
}
public String toString() {
StringBuilder sb = new StringBuilder();
! sb.append(major + "." + minor + "." + micro);
! if (update > 0) {
! sb.append("_" + update);
}
! if (!special.isEmpty()) {
! sb.append(special);
! }
! sb.append("-b" + build);
return sb.toString();
}
}
private static VersionInfo newVersionInfo(String version) throws Exception {
! // valid format of the version string is:
! // n.n.n[_uu[c]][-<identifer>]-bxx
! int major = 0;
! int minor = 0;
! int micro = 0;
! int update = 0;
! String special = "";
! int build = 0;
! CharSequence cs = version;
! if (cs.length() >= 5) {
! if (Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' &&
! Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' &&
! Character.isDigit(cs.charAt(4))) {
! major = Character.digit(cs.charAt(0), 10);
! minor = Character.digit(cs.charAt(2), 10);
! micro = Character.digit(cs.charAt(4), 10);
! cs = cs.subSequence(5, cs.length());
! } else if (Character.isDigit(cs.charAt(0)) &&
! Character.isDigit(cs.charAt(1)) && cs.charAt(2) == '.' &&
! Character.isDigit(cs.charAt(3))) {
! // HSX has nn.n[n] (major.minor) version
! major = Integer.valueOf(version.substring(0, 2)).intValue();
! if (Character.isDigit(cs.charAt(4))) {
! minor = Integer.valueOf(version.substring(3, 5)).intValue();
! cs = cs.subSequence(5, cs.length());
! }
! else {
! minor = Character.digit(cs.charAt(3), 10);
! cs = cs.subSequence(4, cs.length());
! }
! }
! if (cs.charAt(0) == '_' && cs.length() >= 3 &&
! Character.isDigit(cs.charAt(1)) &&
! Character.isDigit(cs.charAt(2))) {
! int nextChar = 3;
! String uu = cs.subSequence(1, 3).toString();
! update = Integer.valueOf(uu).intValue();
! if (cs.length() >= 4) {
! char c = cs.charAt(3);
! if (c >= 'a' && c <= 'z') {
! special = Character.toString(c);
! nextChar++;
! }
! }
! cs = cs.subSequence(nextChar, cs.length());
! }
! if (cs.charAt(0) == '-') {
! // skip the first character
! // valid format: <identifier>-bxx or bxx
! // non-product VM will have -debug|-release appended
! cs = cs.subSequence(1, cs.length());
! String[] res = cs.toString().split("-");
! for (int i = res.length - 1; i >= 0; i--) {
! String s = res[i];
! if (s.charAt(0) == 'b') {
! try {
! build = Integer.parseInt(s.substring(1, s.length()));
! break;
! } catch (NumberFormatException nfe) {
! // ignore
}
}
! }
! }
! }
! VersionInfo vi = new VersionInfo(major, minor, micro, update, special, build);
System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
return vi;
}
}
--- 29,129 ----
* @compile -XDignore.symbol.file Version.java
* @run main Version
*/
import static sun.misc.Version.*;
+ import java.util.regex.Matcher;
+ import java.util.regex.Pattern;
+
public class Version {
public static void main(String[] args) throws Exception {
VersionInfo jdk = newVersionInfo(System.getProperty("java.runtime.version"));
VersionInfo v1 = new VersionInfo(jdkMajorVersion(),
jdkMinorVersion(),
jdkSecurityVersion(),
! jdkPatchVersion(),
jdkBuildNumber());
System.out.println("JDK version = " + jdk + " " + v1);
if (!jdk.equals(v1)) {
throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1);
}
VersionInfo jvm = newVersionInfo(System.getProperty("java.vm.version"));
VersionInfo v2 = new VersionInfo(jvmMajorVersion(),
jvmMinorVersion(),
jvmSecurityVersion(),
! jvmPatchVersion(),
jvmBuildNumber());
System.out.println("JVM version = " + jvm + " " + v2);
if (!jvm.equals(v2)) {
throw new RuntimeException("Unmatched version: " + jvm + " vs " + v2);
}
}
static class VersionInfo {
final int major;
final int minor;
! final int security;
! final int patch;
final int build;
! VersionInfo(int major, int minor, int security,
! int patch, int build) {
this.major = major;
this.minor = minor;
! this.security = security;
! this.patch = patch;
this.build = build;
}
+ VersionInfo(int[] fields) {
+ this.major = fields[0];
+ this.minor = fields[1];
+ this.security = fields[2];
+ this.patch = fields[3];
+ this.build = fields[4];
+ }
+
public boolean equals(VersionInfo v) {
return (this.major == v.major && this.minor == v.minor &&
! this.security == v.security && this.patch == v.patch &&
! this.build == v.build);
}
public String toString() {
StringBuilder sb = new StringBuilder();
! sb.append(major + "." + minor + "." + security);
! if (patch > 0) {
! sb.append("." + patch);
}
! sb.append("+" + build);
return sb.toString();
}
}
private static VersionInfo newVersionInfo(String version) throws Exception {
! // Version string fromat as defined by JEP-223
! String jep223Pattern =
! "^([0-9]+)(\\.([0-9]+))?(\\.([0-9]+))?(\\.([0-9]+))?" + // $VNUM
! "(-([a-zA-Z]+))?(\\.([a-zA-Z]+))?" + // $PRE
! "(\\+([0-9]+))?" + // Build Number
! "(([-a-zA-Z0-9.]+))?$"; // $OPT
!
! // Pattern group index for: Major, Minor, Security, Patch, Build
! int[] groups = {1, 3, 5, 7, 13};
! // Default values for Major, Minor, Security, Patch, Build
! int[] versionFields = {0, 0, 0, 0, 0};
!
! Pattern pattern = Pattern.compile(jep223Pattern);
! Matcher matcher = pattern.matcher(version);
! if (matcher.matches()) {
! for (int i = 0; i < versionFields.length; i++) {
! String field = matcher.group(groups[i]);
! versionFields[i] = (field != null) ? Integer.parseInt(field) : 0;
}
}
!
! VersionInfo vi = new VersionInfo(versionFields);
System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
return vi;
}
}
< prev index next >