jdk/src/java.base/share/classes/sun/misc/Version.java.template
author amurillo
Thu, 17 Sep 2015 14:41:39 -0700
changeset 34003 3c4ba277fdb6
parent 33986 5cbe9cd17789
child 34011 b1ce08dd7f17
permissions -rw-r--r--
8087203: Adapt Version.java.template to the JEP-223 new version string format Reviewed-by: darcy

/*
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.misc;
import java.io.PrintStream;

public class Version {


    private static final String launcher_name =
        "@@LAUNCHER_NAME@@";

    private static final String java_version =
        "@@VERSION_SHORT@@";

    private static final String java_runtime_name =
        "@@RUNTIME_NAME@@";

    private static final String java_runtime_version =
        "@@VERSION_STRING@@";

    static {
        init();
    }

    public static void init() {
        System.setProperty("java.version", java_version);
        System.setProperty("java.runtime.version", java_runtime_version);
        System.setProperty("java.runtime.name", java_runtime_name);
    }

    private static boolean versionsInitialized = false;
    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_patch_version = 0;
    private static int jvm_build_number = 0;
    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_patch_version = 0;
    private static int jdk_build_number = 0;

    /**
     * In case you were wondering this method is called by java -version.
     * Sad that it prints to stderr; would be nicer if default printed on
     * stdout.
     */
    public static void print() {
        print(System.err);
    }

    /**
     * This is the same as print except that it adds an extra line-feed
     * at the end, typically used by the -showversion in the launcher
     */
    public static void println() {
        print(System.err);
        System.err.println();
    }

    /**
     * Give a stream, it will print version info on it.
     */
    public static void print(PrintStream ps) {
        boolean isHeadless = false;

        /* Report that we're running headless if the property is true */
        String headless = System.getProperty("java.awt.headless");
        if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
            isHeadless = true;
        }

        /* First line: platform version. */
        ps.println(launcher_name + " version \"" + java_version + "\"");

        /* Second line: runtime version (ie, libraries). */

        ps.print(java_runtime_name + " (build " + java_runtime_version);

        if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) {
            // embedded builds report headless state
            ps.print(", headless");
        }
        ps.println(')');

        /* Third line: JVM information. */
        String java_vm_name    = System.getProperty("java.vm.name");
        String java_vm_version = System.getProperty("java.vm.version");
        String java_vm_info    = System.getProperty("java.vm.info");
        ps.println(java_vm_name + " (build " + java_vm_version + ", " +
                   java_vm_info + ")");
    }


    /**
     * Returns the major version of the running JVM.
     * @return the major version of the running JVM
     * @since 1.6
     */
    public static synchronized int jvmMajorVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jvm_major_version;
    }

    /**
     * Returns the minor version of the running JVM.
     * @return the minor version of the running JVM
     * @since 1.6
     */
    public static synchronized int jvmMinorVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jvm_minor_version;
    }


    /**
     * Returns the security version of the running JVM.
     * @return the security version of the running JVM
     * @since 9
     */
    public static synchronized int jvmSecurityVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jvm_security_version;
    }

    /**
     * Returns the patch release version of the running JVM.
     * @return the patch release version of the running JVM
     * @since 9
     */
    public static synchronized int jvmPatchVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jvm_patch_version;
    }

    /**
     * Returns the build number of the running JVM.
     * @return the build number of the running JVM
     * @since 1.6
     */
    public static synchronized int jvmBuildNumber() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jvm_build_number;
    }

    /**
     * Returns the major version of the running JDK.
     * @return the major version of the running JDK
     * @since 1.6
     */
    public static synchronized int jdkMajorVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jdk_major_version;
    }

    /**
     * Returns the minor version of the running JDK.
     * @return the minor version of the running JDK
     * @since 1.6
     */
    public static synchronized int jdkMinorVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jdk_minor_version;
    }

    /**
     * Returns the security version of the running JDK.
     * @return the security version of the running JDK
     * @since 9
     */
    public static synchronized int jdkSecurityVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jdk_security_version;
    }

    /**
     * Returns the patch release version of the running JDK.
     * @return the patch release version of the running JDK
     * @since 9
     */
    public static synchronized int jdkPatchVersion() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jdk_patch_version;
    }

    /**
     * Returns the build number of the running JDK.
     * @return the build number of the running JDK
     * @since 1.6
     */
    public static synchronized int jdkBuildNumber() {
        if (!versionsInitialized) {
            initVersions();
        }
        return jdk_build_number;
    }

    private static synchronized void initVersions() {
        if (versionsInitialized) {
            return;
        }
        if (!getJvmVersionInfo()) {
            throw new InternalError("Unable to obtain JVM version info");
        }
        getJdkVersionInfo();
        versionsInitialized = true;
    }

    // Gets the JVM version info if available and sets the jvm_*_version fields
    // and its capabilities.
    private static native boolean getJvmVersionInfo();
    private static native void getJdkVersionInfo();
}

// Help Emacs a little because this file doesn't end in .java.
//
// Local Variables: ***
// mode: java ***
// End: ***