annotate src/java.base/share/classes/sun/misc/Version.java.template @ 13182:474db9ef5ee7

8085822: JEP 223: New Version-String Scheme (initial integration) Reviewed-by: erikj, dcubed, dholmes, alanb Contributed-by: Magnus Ihse Bursie <magnus.ihse.bursie@oracle.com>, Alejandro E Murillo <alejandro.murillo@oracle.com>, Kumar Srinivasan <kumar.x.srinivasan@oracle.com>
author ihse
date Thu, 11 Jun 2015 00:23:01 +0200
parents e336cbd8b15e
children 60387e5d6822
rev   line source
duke@0 1 /*
ihse@13182 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 package sun.misc;
duke@0 27 import java.io.PrintStream;
duke@0 28
duke@0 29 public class Version {
duke@0 30
duke@0 31
duke@0 32 private static final String launcher_name =
ihse@13182 33 "@@LAUNCHER_NAME@@";
duke@0 34
duke@0 35 private static final String java_version =
ihse@13182 36 "@@VERSION_SHORT@@";
duke@0 37
duke@0 38 private static final String java_runtime_name =
ihse@13182 39 "@@RUNTIME_NAME@@";
alanb@6573 40
duke@0 41 private static final String java_runtime_version =
ihse@13182 42 "@@VERSION_STRING@@";
duke@0 43
duke@0 44 static {
duke@0 45 init();
duke@0 46 }
duke@0 47
duke@0 48 public static void init() {
duke@0 49 System.setProperty("java.version", java_version);
duke@0 50 System.setProperty("java.runtime.version", java_runtime_version);
duke@0 51 System.setProperty("java.runtime.name", java_runtime_name);
duke@0 52 }
duke@0 53
duke@0 54 private static boolean versionsInitialized = false;
duke@0 55 private static int jvm_major_version = 0;
duke@0 56 private static int jvm_minor_version = 0;
ihse@13182 57 private static int jvm_security_version = 0;
duke@0 58 private static int jvm_update_version = 0;
duke@0 59 private static int jvm_build_number = 0;
duke@0 60 private static String jvm_special_version = null;
duke@0 61 private static int jdk_major_version = 0;
duke@0 62 private static int jdk_minor_version = 0;
ihse@13182 63 private static int jdk_security_version = 0;
duke@0 64 private static int jdk_update_version = 0;
duke@0 65 private static int jdk_build_number = 0;
duke@0 66 private static String jdk_special_version = null;
duke@0 67
duke@0 68 /**
duke@0 69 * In case you were wondering this method is called by java -version.
duke@0 70 * Sad that it prints to stderr; would be nicer if default printed on
duke@0 71 * stdout.
duke@0 72 */
duke@0 73 public static void print() {
duke@0 74 print(System.err);
duke@0 75 }
duke@0 76
duke@0 77 /**
duke@0 78 * This is the same as print except that it adds an extra line-feed
duke@0 79 * at the end, typically used by the -showversion in the launcher
duke@0 80 */
duke@0 81 public static void println() {
duke@0 82 print(System.err);
duke@0 83 System.err.println();
duke@0 84 }
duke@0 85
duke@0 86 /**
duke@0 87 * Give a stream, it will print version info on it.
duke@0 88 */
duke@0 89 public static void print(PrintStream ps) {
dholmes@3867 90 boolean isHeadless = false;
dholmes@3867 91
dholmes@3867 92 /* Report that we're running headless if the property is true */
alanb@6573 93 String headless = System.getProperty("java.awt.headless");
alanb@6573 94 if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
dholmes@3867 95 isHeadless = true;
alanb@6573 96 }
dholmes@3867 97
duke@0 98 /* First line: platform version. */
duke@0 99 ps.println(launcher_name + " version \"" + java_version + "\"");
duke@0 100
duke@0 101 /* Second line: runtime version (ie, libraries). */
dholmes@3867 102
alanb@6573 103 ps.print(java_runtime_name + " (build " + java_runtime_version);
dholmes@3867 104
alanb@6573 105 if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) {
alanb@6573 106 // embedded builds report headless state
alanb@6573 107 ps.print(", headless");
alanb@6573 108 }
alanb@6573 109 ps.println(')');
duke@0 110
duke@0 111 /* Third line: JVM information. */
duke@0 112 String java_vm_name = System.getProperty("java.vm.name");
duke@0 113 String java_vm_version = System.getProperty("java.vm.version");
duke@0 114 String java_vm_info = System.getProperty("java.vm.info");
duke@0 115 ps.println(java_vm_name + " (build " + java_vm_version + ", " +
duke@0 116 java_vm_info + ")");
duke@0 117 }
duke@0 118
duke@0 119
duke@0 120 /**
duke@0 121 * Returns the major version of the running JVM if it's 1.6 or newer
duke@0 122 * or any RE VM build. It will return 0 if it's an internal 1.5 or
duke@0 123 * 1.4.x build.
duke@0 124 *
duke@0 125 * @since 1.6
duke@0 126 */
duke@0 127 public static synchronized int jvmMajorVersion() {
duke@0 128 if (!versionsInitialized) {
duke@0 129 initVersions();
duke@0 130 }
duke@0 131 return jvm_major_version;
duke@0 132 }
duke@0 133
duke@0 134 /**
duke@0 135 * Returns the minor version of the running JVM if it's 1.6 or newer
duke@0 136 * or any RE VM build. It will return 0 if it's an internal 1.5 or
duke@0 137 * 1.4.x build.
duke@0 138 * @since 1.6
duke@0 139 */
duke@0 140 public static synchronized int jvmMinorVersion() {
duke@0 141 if (!versionsInitialized) {
duke@0 142 initVersions();
duke@0 143 }
duke@0 144 return jvm_minor_version;
duke@0 145 }
duke@0 146
duke@0 147
duke@0 148 /**
ihse@13182 149 * Returns the security version of the running JVM if it's 1.6 or newer
duke@0 150 * or any RE VM build. It will return 0 if it's an internal 1.5 or
duke@0 151 * 1.4.x build.
duke@0 152 * @since 1.6
duke@0 153 */
ihse@13182 154 public static synchronized int jvmSecurityVersion() {
duke@0 155 if (!versionsInitialized) {
duke@0 156 initVersions();
duke@0 157 }
ihse@13182 158 return jvm_security_version;
duke@0 159 }
duke@0 160
duke@0 161 /**
duke@0 162 * Returns the update release version of the running JVM if it's
duke@0 163 * a RE build. It will return 0 if it's an internal build.
duke@0 164 * @since 1.6
duke@0 165 */
duke@0 166 public static synchronized int jvmUpdateVersion() {
duke@0 167 if (!versionsInitialized) {
duke@0 168 initVersions();
duke@0 169 }
duke@0 170 return jvm_update_version;
duke@0 171 }
duke@0 172
duke@0 173 public static synchronized String jvmSpecialVersion() {
duke@0 174 if (!versionsInitialized) {
duke@0 175 initVersions();
duke@0 176 }
duke@0 177 if (jvm_special_version == null) {
duke@0 178 jvm_special_version = getJvmSpecialVersion();
duke@0 179 }
duke@0 180 return jvm_special_version;
duke@0 181 }
duke@0 182 public static native String getJvmSpecialVersion();
duke@0 183
duke@0 184 /**
duke@0 185 * Returns the build number of the running JVM if it's a RE build
duke@0 186 * It will return 0 if it's an internal build.
duke@0 187 * @since 1.6
duke@0 188 */
duke@0 189 public static synchronized int jvmBuildNumber() {
duke@0 190 if (!versionsInitialized) {
duke@0 191 initVersions();
duke@0 192 }
duke@0 193 return jvm_build_number;
duke@0 194 }
duke@0 195
duke@0 196 /**
duke@0 197 * Returns the major version of the running JDK.
duke@0 198 *
duke@0 199 * @since 1.6
duke@0 200 */
duke@0 201 public static synchronized int jdkMajorVersion() {
duke@0 202 if (!versionsInitialized) {
duke@0 203 initVersions();
duke@0 204 }
duke@0 205 return jdk_major_version;
duke@0 206 }
duke@0 207
duke@0 208 /**
duke@0 209 * Returns the minor version of the running JDK.
duke@0 210 * @since 1.6
duke@0 211 */
duke@0 212 public static synchronized int jdkMinorVersion() {
duke@0 213 if (!versionsInitialized) {
duke@0 214 initVersions();
duke@0 215 }
duke@0 216 return jdk_minor_version;
duke@0 217 }
duke@0 218
duke@0 219 /**
ihse@13182 220 * Returns the security version of the running JDK.
duke@0 221 * @since 1.6
duke@0 222 */
ihse@13182 223 public static synchronized int jdkSecurityVersion() {
duke@0 224 if (!versionsInitialized) {
duke@0 225 initVersions();
duke@0 226 }
ihse@13182 227 return jdk_security_version;
duke@0 228 }
duke@0 229
duke@0 230 /**
duke@0 231 * Returns the update release version of the running JDK if it's
duke@0 232 * a RE build. It will return 0 if it's an internal build.
duke@0 233 * @since 1.6
duke@0 234 */
duke@0 235 public static synchronized int jdkUpdateVersion() {
duke@0 236 if (!versionsInitialized) {
duke@0 237 initVersions();
duke@0 238 }
duke@0 239 return jdk_update_version;
duke@0 240 }
duke@0 241
duke@0 242 public static synchronized String jdkSpecialVersion() {
duke@0 243 if (!versionsInitialized) {
duke@0 244 initVersions();
duke@0 245 }
duke@0 246 if (jdk_special_version == null) {
duke@0 247 jdk_special_version = getJdkSpecialVersion();
duke@0 248 }
duke@0 249 return jdk_special_version;
duke@0 250 }
duke@0 251 public static native String getJdkSpecialVersion();
duke@0 252
duke@0 253 /**
duke@0 254 * Returns the build number of the running JDK if it's a RE build
duke@0 255 * It will return 0 if it's an internal build.
duke@0 256 * @since 1.6
duke@0 257 */
duke@0 258 public static synchronized int jdkBuildNumber() {
duke@0 259 if (!versionsInitialized) {
duke@0 260 initVersions();
duke@0 261 }
duke@0 262 return jdk_build_number;
duke@0 263 }
duke@0 264
duke@0 265 // true if JVM exports the version info including the capabilities
duke@0 266 private static boolean jvmVersionInfoAvailable;
duke@0 267 private static synchronized void initVersions() {
duke@0 268 if (versionsInitialized) {
duke@0 269 return;
duke@0 270 }
duke@0 271 jvmVersionInfoAvailable = getJvmVersionInfo();
duke@0 272 if (!jvmVersionInfoAvailable) {
duke@0 273 // parse java.vm.version for older JVM before the
duke@0 274 // new JVM_GetVersionInfo is added.
duke@0 275 // valid format of the version string is:
duke@0 276 // n.n.n[_uu[c]][-<identifer>]-bxx
duke@0 277 CharSequence cs = System.getProperty("java.vm.version");
duke@0 278 if (cs.length() >= 5 &&
duke@0 279 Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' &&
duke@0 280 Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' &&
duke@0 281 Character.isDigit(cs.charAt(4))) {
duke@0 282 jvm_major_version = Character.digit(cs.charAt(0), 10);
duke@0 283 jvm_minor_version = Character.digit(cs.charAt(2), 10);
ihse@13182 284 jvm_security_version = Character.digit(cs.charAt(4), 10);
duke@0 285 cs = cs.subSequence(5, cs.length());
duke@0 286 if (cs.charAt(0) == '_' && cs.length() >= 3 &&
duke@0 287 Character.isDigit(cs.charAt(1)) &&
duke@0 288 Character.isDigit(cs.charAt(2))) {
duke@0 289 int nextChar = 3;
duke@0 290 try {
duke@0 291 String uu = cs.subSequence(1, 3).toString();
duke@0 292 jvm_update_version = Integer.valueOf(uu).intValue();
duke@0 293 if (cs.length() >= 4) {
duke@0 294 char c = cs.charAt(3);
duke@0 295 if (c >= 'a' && c <= 'z') {
duke@0 296 jvm_special_version = Character.toString(c);
duke@0 297 nextChar++;
duke@0 298 }
duke@0 299 }
duke@0 300 } catch (NumberFormatException e) {
duke@0 301 // not conforming to the naming convention
duke@0 302 return;
duke@0 303 }
duke@0 304 cs = cs.subSequence(nextChar, cs.length());
duke@0 305 }
duke@0 306 if (cs.charAt(0) == '-') {
duke@0 307 // skip the first character
duke@0 308 // valid format: <identifier>-bxx or bxx
duke@0 309 // non-product VM will have -debug|-release appended
duke@0 310 cs = cs.subSequence(1, cs.length());
duke@0 311 String[] res = cs.toString().split("-");
duke@0 312 for (String s : res) {
duke@0 313 if (s.charAt(0) == 'b' && s.length() == 3 &&
duke@0 314 Character.isDigit(s.charAt(1)) &&
duke@0 315 Character.isDigit(s.charAt(2))) {
duke@0 316 jvm_build_number =
duke@0 317 Integer.valueOf(s.substring(1, 3)).intValue();
duke@0 318 break;
duke@0 319 }
duke@0 320 }
duke@0 321 }
duke@0 322 }
duke@0 323 }
duke@0 324 getJdkVersionInfo();
duke@0 325 versionsInitialized = true;
duke@0 326 }
duke@0 327
duke@0 328 // Gets the JVM version info if available and sets the jvm_*_version fields
duke@0 329 // and its capabilities.
duke@0 330 //
duke@0 331 // Return false if not available which implies an old VM (Tiger or before).
duke@0 332 private static native boolean getJvmVersionInfo();
duke@0 333 private static native void getJdkVersionInfo();
duke@0 334 }
duke@0 335
duke@0 336 // Help Emacs a little because this file doesn't end in .java.
duke@0 337 //
duke@0 338 // Local Variables: ***
duke@0 339 // mode: java ***
duke@0 340 // End: ***