8087202: Add support for PATCH field and remove unused fields of new version string
authoramurillo
Fri, 19 Jun 2015 10:58:00 -0700
changeset 33986 5cbe9cd17789
parent 33985 6a01dc9458f7
child 33987 24ecd3c83f33
8087202: Add support for PATCH field and remove unused fields of new version string Reviewed-by: dholmes, alanb
jdk/make/mapfiles/libjava/mapfile-vers
jdk/src/java.base/share/classes/sun/misc/Version.java.template
jdk/src/java.base/share/native/include/jvm.h
jdk/src/java.base/share/native/libjava/Version.c
jdk/src/java.base/share/native/libjava/jdk_util.c
jdk/test/sun/misc/Version/Version.java
--- a/jdk/make/mapfiles/libjava/mapfile-vers	Thu Jun 11 00:51:11 2015 +0200
+++ b/jdk/make/mapfiles/libjava/mapfile-vers	Fri Jun 19 10:58:00 2015 -0700
@@ -269,9 +269,7 @@
 		Java_sun_reflect_Reflection_getCallerClass__I;
 		Java_sun_reflect_Reflection_getClassAccessFlags;
                 Java_sun_misc_Version_getJdkVersionInfo;
-                Java_sun_misc_Version_getJdkSpecialVersion;
                 Java_sun_misc_Version_getJvmVersionInfo;
-                Java_sun_misc_Version_getJvmSpecialVersion;
 		Java_sun_misc_VM_latestUserDefinedLoader;
                 Java_sun_misc_VM_getuid;
                 Java_sun_misc_VM_geteuid;
--- a/jdk/src/java.base/share/classes/sun/misc/Version.java.template	Thu Jun 11 00:51:11 2015 +0200
+++ b/jdk/src/java.base/share/classes/sun/misc/Version.java.template	Fri Jun 19 10:58:00 2015 -0700
@@ -55,15 +55,13 @@
     private static int jvm_major_version = 0;
     private static int jvm_minor_version = 0;
     private static int jvm_security_version = 0;
-    private static int jvm_update_version = 0;
+    private static int jvm_patch_version = 0;
     private static int jvm_build_number = 0;
-    private static String jvm_special_version = null;
     private static int jdk_major_version = 0;
     private static int jdk_minor_version = 0;
     private static int jdk_security_version = 0;
-    private static int jdk_update_version = 0;
+    private static int jdk_patch_version = 0;
     private static int jdk_build_number = 0;
-    private static String jdk_special_version = null;
 
     /**
      * In case you were wondering this method is called by java -version.
@@ -146,9 +144,7 @@
 
 
     /**
-     * Returns the security version of the running JVM if it's 1.6 or newer
-     * or any RE VM build. It will return 0 if it's an internal 1.5 or
-     * 1.4.x build.
+     * Returns the security version of the running JVM 
      * @since 1.6
      */
     public static synchronized int jvmSecurityVersion() {
@@ -159,31 +155,18 @@
     }
 
     /**
-     * Returns the update release version of the running JVM if it's
-     * a RE build. It will return 0 if it's an internal build.
-     * @since 1.6
+     * Returns the patch release version of the running JVM
+     * @since JDK9
      */
-    public static synchronized int jvmUpdateVersion() {
+    public static synchronized int jvmPatchVersion() {
         if (!versionsInitialized) {
             initVersions();
         }
-        return jvm_update_version;
+        return jvm_patch_version;
     }
 
-    public static synchronized String jvmSpecialVersion() {
-        if (!versionsInitialized) {
-            initVersions();
-        }
-        if (jvm_special_version == null) {
-            jvm_special_version = getJvmSpecialVersion();
-        }
-        return jvm_special_version;
-    }
-    public static native String getJvmSpecialVersion();
-
     /**
-     * Returns the build number of the running JVM if it's a RE build
-     * It will return 0 if it's an internal build.
+     * Returns the build number of the running JVM
      * @since 1.6
      */
     public static synchronized int jvmBuildNumber() {
@@ -228,31 +211,18 @@
     }
 
     /**
-     * Returns the update release version of the running JDK if it's
-     * a RE build. It will return 0 if it's an internal build.
-     * @since 1.6
+     * Returns the patch release version of the running JDK
+     * @since 9
      */
-    public static synchronized int jdkUpdateVersion() {
+    public static synchronized int jdkPatchVersion() {
         if (!versionsInitialized) {
             initVersions();
         }
-        return jdk_update_version;
+        return jdk_patch_version;
     }
 
-    public static synchronized String jdkSpecialVersion() {
-        if (!versionsInitialized) {
-            initVersions();
-        }
-        if (jdk_special_version == null) {
-            jdk_special_version = getJdkSpecialVersion();
-        }
-        return jdk_special_version;
-    }
-    public static native String getJdkSpecialVersion();
-
     /**
-     * Returns the build number of the running JDK if it's a RE build
-     * It will return 0 if it's an internal build.
+     * Returns the build number of the running JDK
      * @since 1.6
      */
     public static synchronized int jdkBuildNumber() {
@@ -262,64 +232,12 @@
         return jdk_build_number;
     }
 
-    // true if JVM exports the version info including the capabilities
-    private static boolean jvmVersionInfoAvailable;
     private static synchronized void initVersions() {
         if (versionsInitialized) {
             return;
         }
-        jvmVersionInfoAvailable = getJvmVersionInfo();
-        if (!jvmVersionInfoAvailable) {
-            // parse java.vm.version for older JVM before the
-            // new JVM_GetVersionInfo is added.
-            // valid format of the version string is:
-            // n.n.n[_uu[c]][-<identifer>]-bxx
-            CharSequence cs = System.getProperty("java.vm.version");
-            if (cs.length() >= 5 &&
-                Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' &&
-                Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' &&
-                Character.isDigit(cs.charAt(4))) {
-                jvm_major_version = Character.digit(cs.charAt(0), 10);
-                jvm_minor_version = Character.digit(cs.charAt(2), 10);
-                jvm_security_version = Character.digit(cs.charAt(4), 10);
-                cs = cs.subSequence(5, cs.length());
-                if (cs.charAt(0) == '_' && cs.length() >= 3 &&
-                    Character.isDigit(cs.charAt(1)) &&
-                    Character.isDigit(cs.charAt(2))) {
-                    int nextChar = 3;
-                    try {
-                        String uu = cs.subSequence(1, 3).toString();
-                        jvm_update_version = Integer.valueOf(uu).intValue();
-                        if (cs.length() >= 4) {
-                            char c = cs.charAt(3);
-                            if (c >= 'a' && c <= 'z') {
-                                jvm_special_version = Character.toString(c);
-                                nextChar++;
-                            }
-                        }
-                    } catch (NumberFormatException e) {
-                        // not conforming to the naming convention
-                        return;
-                    }
-                    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 (String s : res) {
-                        if (s.charAt(0) == 'b' && s.length() == 3 &&
-                            Character.isDigit(s.charAt(1)) &&
-                            Character.isDigit(s.charAt(2))) {
-                            jvm_build_number =
-                                Integer.valueOf(s.substring(1, 3)).intValue();
-                            break;
-                        }
-                    }
-                }
-            }
+        if (!getJvmVersionInfo()) {
+            throw new InternalError("Unable to obtain JVM version info");
         }
         getJdkVersionInfo();
         versionsInitialized = true;
@@ -327,8 +245,6 @@
 
     // Gets the JVM version info if available and sets the jvm_*_version fields
     // and its capabilities.
-    //
-    // Return false if not available which implies an old VM (Tiger or before).
     private static native boolean getJvmVersionInfo();
     private static native void getJdkVersionInfo();
 }
--- a/jdk/src/java.base/share/native/include/jvm.h	Thu Jun 11 00:51:11 2015 +0200
+++ b/jdk/src/java.base/share/native/include/jvm.h	Fri Jun 19 10:58:00 2015 -0700
@@ -1127,9 +1127,9 @@
  * ==========================================================================
  */
 typedef struct {
-    unsigned int jvm_version;  /* Follows JDK version string as specified by JEP-223 */
-    unsigned int update_version : 8;         /* Update release version (uu) */
-    unsigned int special_update_version : 8; /* Special update release version (c)*/
+    unsigned int jvm_version;  /* Encoded $VNUM as specified by JEP-223 */
+    unsigned int patch_version : 8; /* JEP-223 patch version */
+    unsigned int reserved3 : 8;
     unsigned int reserved1 : 16;
     unsigned int reserved2;
 
@@ -1149,19 +1149,15 @@
 #define JVM_VERSION_MAJOR(version) ((version & 0xFF000000) >> 24)
 #define JVM_VERSION_MINOR(version) ((version & 0x00FF0000) >> 16)
 #define JVM_VERSION_SECURITY(version) ((version & 0x0000FF00) >> 8)
-
-/* Build number is available only for RE builds.
- * It will be zero for internal builds.
- */
 #define JVM_VERSION_BUILD(version) ((version & 0x000000FF))
 
 JNIEXPORT void JNICALL
 JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size);
 
 typedef struct {
-    unsigned int jdk_version; /* JDK version string as specified by JEP-223 */
-    unsigned int update_version : 8;         /* Update release version (uu) */
-    unsigned int special_update_version : 8; /* Special update release version (c)*/
+    unsigned int jdk_version; /* Encoded $VNUM as specified by JEP-223 */
+    unsigned int patch_version : 8; /* JEP-223 patch version */
+    unsigned int reserved3 : 8;
     unsigned int reserved1 : 16;
     unsigned int reserved2;
 
@@ -1183,10 +1179,6 @@
 #define JDK_VERSION_MAJOR(version) ((version & 0xFF000000) >> 24)
 #define JDK_VERSION_MINOR(version) ((version & 0x00FF0000) >> 16)
 #define JDK_VERSION_SECURITY(version) ((version & 0x0000FF00) >> 8)
-
-/* Build number is available only for RE build (i.e. JDK_BUILD_NUMBER is set to NN)
- * It will be zero for internal builds.
- */
 #define JDK_VERSION_BUILD(version) ((version & 0x000000FF))
 
 /*
--- a/jdk/src/java.base/share/native/libjava/Version.c	Thu Jun 11 00:51:11 2015 +0200
+++ b/jdk/src/java.base/share/native/libjava/Version.c	Fri Jun 19 10:58:00 2015 -0700
@@ -30,8 +30,6 @@
 
 #include "sun_misc_Version.h"
 
-char jvm_special_version = '\0';
-char jdk_special_version = '\0';
 static void setStaticIntField(JNIEnv* env, jclass cls, const char* name, jint value)
 {
     jfieldID fid;
@@ -67,23 +65,12 @@
     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
     setStaticIntField(env, cls, "jvm_build_number", JVM_VERSION_BUILD(info.jvm_version));
     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
-    setStaticIntField(env, cls, "jvm_update_version", info.update_version);
+    setStaticIntField(env, cls, "jvm_patch_version", info.patch_version);
     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
-    jvm_special_version = info.special_update_version;
 
     return JNI_TRUE;
 }
 
-JNIEXPORT jstring JNICALL
-Java_sun_misc_Version_getJvmSpecialVersion(JNIEnv *env, jclass cls) {
-    char s[2];
-    jstring special;
-    s[0] = jvm_special_version;
-    s[1] = '\0';
-    special = (*env)->NewStringUTF(env, s);
-    return special;
-}
-
 JNIEXPORT void JNICALL
 Java_sun_misc_Version_getJdkVersionInfo(JNIEnv *env, jclass cls)
 {
@@ -98,17 +85,6 @@
     JNU_CHECK_EXCEPTION(env);
     setStaticIntField(env, cls, "jdk_build_number", JDK_VERSION_BUILD(info.jdk_version));
     JNU_CHECK_EXCEPTION(env);
-    setStaticIntField(env, cls, "jdk_update_version", info.update_version);
+    setStaticIntField(env, cls, "jdk_patch_version", info.patch_version);
     JNU_CHECK_EXCEPTION(env);
-    jdk_special_version = info.special_update_version;
 }
-
-JNIEXPORT jstring JNICALL
-Java_sun_misc_Version_getJdkSpecialVersion(JNIEnv *env, jclass cls) {
-    char s[2];
-    jstring special;
-    s[0] = jdk_special_version;
-    s[1] = '\0';
-    special = (*env)->NewStringUTF(env, s);
-    return special;
-}
--- a/jdk/src/java.base/share/native/libjava/jdk_util.c	Thu Jun 11 00:51:11 2015 +0200
+++ b/jdk/src/java.base/share/native/libjava/jdk_util.c	Fri Jun 19 10:58:00 2015 -0700
@@ -37,6 +37,7 @@
     const unsigned int version_major = VERSION_MAJOR;
     const unsigned int version_minor = VERSION_MINOR;
     const unsigned int version_security = VERSION_SECURITY;
+    const unsigned int version_patch = VERSION_PATCH;
     const unsigned int version_build = VERSION_BUILD;
 
     memset(info, 0, info_size);
@@ -44,9 +45,7 @@
                         ((version_minor & 0xFF) << 16) |
                         ((version_security & 0xFF) << 8)  |
                         (version_build & 0xFF);
-    // FIXME: update_version and special_update_version does not make sense anymore.
-    info->update_version = 0;
-    info->special_update_version = 0;
+    info->patch_version = version_patch;
     info->thread_park_blocker = 1;
     // Advertise presence of sun.misc.PostVMInitHook:
     // future optimization: detect if this is enabled.
--- a/jdk/test/sun/misc/Version/Version.java	Thu Jun 11 00:51:11 2015 +0200
+++ b/jdk/test/sun/misc/Version/Version.java	Fri Jun 19 10:58:00 2015 -0700
@@ -31,6 +31,9 @@
  */
 
 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 {
@@ -38,8 +41,7 @@
         VersionInfo v1 = new VersionInfo(jdkMajorVersion(),
                                          jdkMinorVersion(),
                                          jdkSecurityVersion(),
-                                         jdkUpdateVersion(),
-                                         jdkSpecialVersion(),
+                                         jdkPatchVersion(),
                                          jdkBuildNumber());
         System.out.println("JDK version = " + jdk + "  " + v1);
         if (!jdk.equals(v1)) {
@@ -49,8 +51,7 @@
         VersionInfo v2 = new VersionInfo(jvmMajorVersion(),
                                          jvmMinorVersion(),
                                          jvmSecurityVersion(),
-                                         jvmUpdateVersion(),
-                                         jvmSpecialVersion(),
+                                         jvmPatchVersion(),
                                          jvmBuildNumber());
         System.out.println("JVM version = " + jvm + " " + v2);
         if (!jvm.equals(v2)) {
@@ -61,108 +62,67 @@
     static class VersionInfo {
         final int major;
         final int minor;
-        final int micro;
-        final int update;
-        final String special;
+        final int security;
+        final int patch;
         final int build;
-        VersionInfo(int major, int minor, int micro,
-                    int update, String special, int build) {
+        VersionInfo(int major, int minor, int security,
+                    int patch, int build) {
             this.major = major;
             this.minor = minor;
-            this.micro = micro;
-            this.update = update;
-            this.special = special;
+            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.micro == v.micro && this.update == v.update &&
-                    this.special.equals(v.special) && this.build == v.build);
+                    this.security == v.security && this.patch == v.patch &&
+                    this.build == v.build);
         }
 
         public String toString() {
             StringBuilder sb = new StringBuilder();
-            sb.append(major + "." + minor + "." + micro);
-            if (update > 0) {
-                sb.append("_" + update);
+            sb.append(major + "." + minor + "." + security);
+            if (patch > 0) {
+                sb.append("." + patch);
             }
 
-            if (!special.isEmpty()) {
-                sb.append(special);
-            }
-            sb.append("-b" + build);
+            sb.append("+" + 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
-                        }
-                    }
-                }
+        // 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(major, minor, micro, update, special, build);
+
+        VersionInfo vi = new VersionInfo(versionFields);
         System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
         return vi;
     }