src/java.base/share/classes/java/lang/Runtime.java
author mchung
Mon, 06 Nov 2017 17:48:00 -0800
changeset 47707 67aa34b019e1
parent 47216 71c04702a3d5
child 48214 c3bf1ae9c51d
permissions -rw-r--r--
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library Reviewed-by: alanb, bchristi, kbarrett, dholmes, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
     2
 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    29
import java.math.BigInteger;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    30
import java.util.ArrayList;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    31
import java.util.regex.Matcher;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    32
import java.util.regex.Pattern;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    33
import java.util.stream.Collectors;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    34
import java.util.Collections;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    35
import java.util.List;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    36
import java.util.Optional;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34350
diff changeset
    38
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34350
diff changeset
    39
import jdk.internal.reflect.Reflection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Every Java application has a single instance of class
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
    43
 * {@code Runtime} that allows the application to interface with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the environment in which the application is running. The current
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
    45
 * runtime can be obtained from the {@code getRuntime} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * An application cannot create its own instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see     java.lang.Runtime#getRuntime()
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
    51
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class Runtime {
34350
6beb09485bdd 8144210: Runtime.currentRuntime should be final
alanb
parents: 30042
diff changeset
    55
    private static final Runtime currentRuntime = new Runtime();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    57
    private static Version version;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Returns the runtime object associated with the current Java application.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
    61
     * Most of the methods of class {@code Runtime} are instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * methods and must be invoked with respect to the current runtime object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
    64
     * @return  the {@code Runtime} object associated with the current
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *          Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static Runtime getRuntime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return currentRuntime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Don't let anyone else instantiate this class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Runtime() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Terminates the currently running Java virtual machine by initiating its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * shutdown sequence.  This method never returns normally.  The argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * serves as a status code; by convention, a nonzero status code indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * abnormal termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p> The virtual machine's shutdown sequence consists of two phases.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * the first phase all registered {@link #addShutdownHook shutdown hooks},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * if any, are started in some unspecified order and allowed to run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * concurrently until they finish.  In the second phase all uninvoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
    85
     * has been enabled.  Once this is done the virtual machine {@link #halt halts}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p> If this method is invoked after the virtual machine has begun its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * shutdown sequence then if shutdown hooks are being run this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * block indefinitely.  If shutdown hooks have already been run and on-exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * finalization has been enabled then this method halts the virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * with the given status code if the status is nonzero; otherwise, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * blocks indefinitely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
    94
     * <p> The {@link System#exit(int) System.exit} method is the
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 18776
diff changeset
    95
     * conventional and convenient means of invoking this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param  status
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *         Termination status.  By convention, a nonzero status code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *         indicates abnormal termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws SecurityException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   102
     *         If a security manager is present and its
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   103
     *         {@link SecurityManager#checkExit checkExit} method does not permit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *         exiting with the specified status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see java.lang.SecurityManager#checkExit(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see #addShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see #removeShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #runFinalizersOnExit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #halt(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public void exit(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            security.checkExit(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Shutdown.exit(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Registers a new virtual-machine shutdown hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p> The Java virtual machine <i>shuts down</i> in response to two kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * of events:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *   <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 16906
diff changeset
   129
     *   <li> The program <i>exits</i> normally, when the last non-daemon
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   130
     *   thread exits or when the {@link #exit exit} (equivalently,
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 16906
diff changeset
   131
     *   {@link System#exit(int) System.exit}) method is invoked, or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 16906
diff changeset
   133
     *   <li> The virtual machine is <i>terminated</i> in response to a
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   134
     *   user interrupt, such as typing {@code ^C}, or a system-wide event,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *   such as user logoff or system shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *   </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p> A <i>shutdown hook</i> is simply an initialized but unstarted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * thread.  When the virtual machine begins its shutdown sequence it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * start all registered shutdown hooks in some unspecified order and let
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * them run concurrently.  When all the hooks have finished it will then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * run all uninvoked finalizers if finalization-on-exit has been enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Finally, the virtual machine will halt.  Note that daemon threads will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * continue to run during the shutdown sequence, as will non-daemon threads
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   146
     * if shutdown was initiated by invoking the {@link #exit exit} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p> Once the shutdown sequence has begun it can be stopped only by
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   149
     * invoking the {@link #halt halt} method, which forcibly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * terminates the virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p> Once the shutdown sequence has begun it is impossible to register a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * new shutdown hook or de-register a previously-registered hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Attempting either of these operations will cause an
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   155
     * {@link IllegalStateException} to be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p> Shutdown hooks run at a delicate time in the life cycle of a virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * machine and should therefore be coded defensively.  They should, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * particular, be written to be thread-safe and to avoid deadlocks insofar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * as possible.  They should also not rely blindly upon services that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * have registered their own shutdown hooks and therefore may themselves in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * the process of shutting down.  Attempts to use other thread-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * services such as the AWT event-dispatch thread, for example, may lead to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * deadlocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p> Shutdown hooks should also finish their work quickly.  When a
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   167
     * program invokes {@link #exit exit} the expectation is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * that the virtual machine will promptly shut down and exit.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * virtual machine is terminated due to user logoff or system shutdown the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * underlying operating system may only allow a fixed amount of time in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * which to shut down and exit.  It is therefore inadvisable to attempt any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * user interaction or to perform a long-running computation in a shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p> Uncaught exceptions are handled in shutdown hooks just as in any
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   176
     * other thread, by invoking the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   177
     * {@link ThreadGroup#uncaughtException uncaughtException} method of the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   178
     * thread's {@link ThreadGroup} object. The default implementation of this
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   179
     * method prints the exception's stack trace to {@link System#err} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * terminates the thread; it does not cause the virtual machine to exit or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * halt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p> In rare circumstances the virtual machine may <i>abort</i>, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * stop running without shutting down cleanly.  This occurs when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * virtual machine is terminated externally, for example with the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   186
     * {@code SIGKILL} signal on Unix or the {@code TerminateProcess} call on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Microsoft Windows.  The virtual machine may also abort if a native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * method goes awry by, for example, corrupting internal data structures or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * attempting to access nonexistent memory.  If the virtual machine aborts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * then no guarantee can be made about whether or not any shutdown hooks
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 18776
diff changeset
   191
     * will be run.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param   hook
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   194
     *          An initialized but unstarted {@link Thread} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          If the specified hook has already been registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *          or if it can be determined that the hook is already running or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *          has already been run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *          If the virtual machine is already in the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          of shutting down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          If a security manager is present and it denies
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   207
     *          {@link RuntimePermission}("shutdownHooks")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see #removeShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see #halt(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see #exit(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void addShutdownHook(Thread hook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        ApplicationShutdownHooks.add(hook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   223
     * De-registers a previously-registered virtual-machine shutdown hook.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param hook the hook to remove
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   226
     * @return {@code true} if the specified hook had previously been
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   227
     * registered and was successfully de-registered, {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *          If the virtual machine is already in the process of shutting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          If a security manager is present and it denies
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   236
     *          {@link RuntimePermission}("shutdownHooks")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see #addShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see #exit(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public boolean removeShutdownHook(Thread hook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return ApplicationShutdownHooks.remove(hook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Forcibly terminates the currently running Java virtual machine.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * method never returns normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p> This method should be used with extreme caution.  Unlike the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   255
     * {@link #exit exit} method, this method does not cause shutdown
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * hooks to be started and does not run uninvoked finalizers if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * finalization-on-exit has been enabled.  If the shutdown sequence has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * already been initiated then this method does not wait for any running
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 18776
diff changeset
   259
     * shutdown hooks or finalizers to finish their work.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param  status
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   262
     *         Termination status. By convention, a nonzero status code
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   263
     *         indicates abnormal termination. If the {@link Runtime#exit exit}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   264
     *         (equivalently, {@link System#exit(int) System.exit}) method
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   265
     *         has already been invoked then this status code
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   266
     *         will override the status code passed to that method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @throws SecurityException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   269
     *         If a security manager is present and its
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   270
     *         {@link SecurityManager#checkExit checkExit} method
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   271
     *         does not permit an exit with the specified status
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see #exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see #addShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see #removeShutdownHook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public void halt(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            sm.checkExit(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Shutdown.halt(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Enable or disable finalization on exit; doing so specifies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * finalizers of all objects that have finalizers that have not yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * automatically invoked are to be run before the Java runtime exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * By default, finalization on exit is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p>If there is a security manager,
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   293
     * its {@code checkExit} method is first called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * with 0 as its argument to ensure the exit is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param value true to enable finalization on exit, false to disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @deprecated  This method is inherently unsafe.  It may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *      finalizers being called on live objects while other threads are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *      concurrently manipulating those objects, resulting in erratic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *      behavior or deadlock.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   302
     *      This method is subject to removal in a future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @throws  SecurityException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   305
     *        if a security manager exists and its {@code checkExit}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *        method doesn't allow the exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see     java.lang.Runtime#exit(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see     java.lang.Runtime#gc()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @see     java.lang.SecurityManager#checkExit(int)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
   311
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   313
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public static void runFinalizersOnExit(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                security.checkExit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                throw new SecurityException("runFinalizersOnExit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Shutdown.setRunFinalizersOnExit(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Executes the specified string command in a separate process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p>This is a convenience method.  An invocation of the form
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   330
     * {@code exec(command)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * behaves in exactly the same way as the invocation
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   332
     * {@link #exec(String, String[], File) exec}{@code (command, null, null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param   command   a specified system command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return  A new {@link Process} object for managing the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *          {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *          method doesn't allow creation of the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   347
     *          If {@code command} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @throws  IllegalArgumentException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   350
     *          If {@code command} is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see     #exec(String[], String[], File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see     ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public Process exec(String command) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return exec(command, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Executes the specified string command in a separate process with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * specified environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>This is a convenience method.  An invocation of the form
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   364
     * {@code exec(command, envp)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * behaves in exactly the same way as the invocation
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   366
     * {@link #exec(String, String[], File) exec}{@code (command, envp, null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param   command   a specified system command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param   envp      array of strings, each element of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *                    has environment variable settings in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *                    <i>name</i>=<i>value</i>, or
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   373
     *                    {@code null} if the subprocess should inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *                    the environment of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return  A new {@link Process} object for managing the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *          {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *          method doesn't allow creation of the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   387
     *          If {@code command} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   388
     *          or one of the elements of {@code envp} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @throws  IllegalArgumentException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   391
     *          If {@code command} is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see     #exec(String[], String[], File)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @see     ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public Process exec(String command, String[] envp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return exec(command, envp, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Executes the specified string command in a separate process with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * specified environment and working directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p>This is a convenience method.  An invocation of the form
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   405
     * {@code exec(command, envp, dir)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * behaves in exactly the same way as the invocation
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   407
     * {@link #exec(String[], String[], File) exec}{@code (cmdarray, envp, dir)},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   408
     * where {@code cmdarray} is an array of all the tokens in
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   409
     * {@code command}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   411
     * <p>More precisely, the {@code command} string is broken
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * into tokens using a {@link StringTokenizer} created by the call
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   413
     * {@code new {@link StringTokenizer}(command)} with no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * further modification of the character categories.  The tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * produced by the tokenizer are then placed in the new string
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   416
     * array {@code cmdarray}, in the same order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param   command   a specified system command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param   envp      array of strings, each element of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *                    has environment variable settings in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *                    <i>name</i>=<i>value</i>, or
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   423
     *                    {@code null} if the subprocess should inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *                    the environment of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param   dir       the working directory of the subprocess, or
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   427
     *                    {@code null} if the subprocess should inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *                    the working directory of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return  A new {@link Process} object for managing the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *          {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *          method doesn't allow creation of the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   441
     *          If {@code command} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   442
     *          or one of the elements of {@code envp} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @throws  IllegalArgumentException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   445
     *          If {@code command} is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see     ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public Process exec(String command, String[] envp, File dir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (command.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new IllegalArgumentException("Empty command");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        StringTokenizer st = new StringTokenizer(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        String[] cmdarray = new String[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        for (int i = 0; st.hasMoreTokens(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            cmdarray[i] = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return exec(cmdarray, envp, dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Executes the specified command and arguments in a separate process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <p>This is a convenience method.  An invocation of the form
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   466
     * {@code exec(cmdarray)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * behaves in exactly the same way as the invocation
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   468
     * {@link #exec(String[], String[], File) exec}{@code (cmdarray, null, null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param   cmdarray  array containing the command to call and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *                    its arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @return  A new {@link Process} object for managing the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *          {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *          method doesn't allow creation of the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   484
     *          If {@code cmdarray} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   485
     *          or one of the elements of {@code cmdarray} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @throws  IndexOutOfBoundsException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   488
     *          If {@code cmdarray} is an empty array
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   489
     *          (has length {@code 0})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @see     ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public Process exec(String cmdarray[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return exec(cmdarray, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Executes the specified command and arguments in a separate process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * with the specified environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <p>This is a convenience method.  An invocation of the form
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   502
     * {@code exec(cmdarray, envp)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * behaves in exactly the same way as the invocation
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   504
     * {@link #exec(String[], String[], File) exec}{@code (cmdarray, envp, null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param   cmdarray  array containing the command to call and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *                    its arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param   envp      array of strings, each element of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *                    has environment variable settings in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *                    <i>name</i>=<i>value</i>, or
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   512
     *                    {@code null} if the subprocess should inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *                    the environment of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return  A new {@link Process} object for managing the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *          {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *          method doesn't allow creation of the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   526
     *          If {@code cmdarray} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   527
     *          or one of the elements of {@code cmdarray} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   528
     *          or one of the elements of {@code envp} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @throws  IndexOutOfBoundsException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   531
     *          If {@code cmdarray} is an empty array
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   532
     *          (has length {@code 0})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @see     ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public Process exec(String[] cmdarray, String[] envp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return exec(cmdarray, envp, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Executes the specified command and arguments in a separate process with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * the specified environment and working directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   545
     * <p>Given an array of strings {@code cmdarray}, representing the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   546
     * tokens of a command line, and an array of strings {@code envp},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * representing "environment" variable settings, this method creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * a new process in which to execute the specified command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   550
     * <p>This method checks that {@code cmdarray} is a valid operating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * system command.  Which commands are valid is system-dependent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * but at the very least the command must be a non-empty list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * non-null strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   555
     * <p>If {@code envp} is {@code null}, the subprocess inherits the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * environment settings of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
9500
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 5506
diff changeset
   558
     * <p>A minimal set of system dependent environment variables may
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 5506
diff changeset
   559
     * be required to start a process on some operating systems.
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 5506
diff changeset
   560
     * As a result, the subprocess may inherit additional environment variable
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 5506
diff changeset
   561
     * settings beyond those in the specified environment.
268f823d9e1c 7034570: java.lang.Runtime.exec(String[] cmd, String[] env) can not work properly if SystemRoot not inherited
michaelm
parents: 5506
diff changeset
   562
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <p>{@link ProcessBuilder#start()} is now the preferred way to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * start a process with a modified environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   566
     * <p>The working directory of the new subprocess is specified by {@code dir}.
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   567
     * If {@code dir} is {@code null}, the subprocess inherits the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * current working directory of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p>If a security manager exists, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * method is invoked with the first component of the array
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   573
     * {@code cmdarray} as its argument. This may result in a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * {@link SecurityException} being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <p>Starting an operating system process is highly system-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Among the many things that can go wrong are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <li>The operating system program file was not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <li>Access to the program file was denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <li>The working directory does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p>In such cases an exception will be thrown.  The exact nature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * of the exception is system-dependent, but it will always be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * subclass of {@link IOException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
28872
82a09f5a7ed6 8072034: (process) ProcessBuilder.start and Runtime.exec UnsupportedOperationException editorial cleanup
rriggs
parents: 28750
diff changeset
   588
     * <p>If the operating system does not support the creation of
82a09f5a7ed6 8072034: (process) ProcessBuilder.start and Runtime.exec UnsupportedOperationException editorial cleanup
rriggs
parents: 28750
diff changeset
   589
     * processes, an {@link UnsupportedOperationException} will be thrown.
82a09f5a7ed6 8072034: (process) ProcessBuilder.start and Runtime.exec UnsupportedOperationException editorial cleanup
rriggs
parents: 28750
diff changeset
   590
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param   cmdarray  array containing the command to call and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *                    its arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param   envp      array of strings, each element of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *                    has environment variable settings in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *                    <i>name</i>=<i>value</i>, or
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   598
     *                    {@code null} if the subprocess should inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *                    the environment of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @param   dir       the working directory of the subprocess, or
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   602
     *                    {@code null} if the subprocess should inherit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *                    the working directory of the current process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return  A new {@link Process} object for managing the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *          {@link SecurityManager#checkExec checkExec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *          method doesn't allow creation of the subprocess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
28750
a1dae439cdab 8055330: (process spec) ProcessBuilder.start and Runtime.exec should throw UnsupportedOperationException on platforms that don't support
rriggs
parents: 27184
diff changeset
   612
     * @throws  UnsupportedOperationException
a1dae439cdab 8055330: (process spec) ProcessBuilder.start and Runtime.exec should throw UnsupportedOperationException on platforms that don't support
rriggs
parents: 27184
diff changeset
   613
     *          If the operating system does not support the creation of processes.
a1dae439cdab 8055330: (process spec) ProcessBuilder.start and Runtime.exec should throw UnsupportedOperationException on platforms that don't support
rriggs
parents: 27184
diff changeset
   614
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @throws  NullPointerException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   619
     *          If {@code cmdarray} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   620
     *          or one of the elements of {@code cmdarray} is {@code null},
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   621
     *          or one of the elements of {@code envp} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @throws  IndexOutOfBoundsException
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   624
     *          If {@code cmdarray} is an empty array
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   625
     *          (has length {@code 0})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @see     ProcessBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public Process exec(String[] cmdarray, String[] envp, File dir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return new ProcessBuilder(cmdarray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            .environment(envp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            .directory(dir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            .start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Returns the number of processors available to the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <p> This value may change during a particular invocation of the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * machine.  Applications that are sensitive to the number of available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * processors should therefore occasionally poll this property and adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * their resource usage appropriately. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @return  the maximum number of processors available to the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *          machine; never smaller than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public native int availableProcessors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Returns the amount of free memory in the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Calling the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   655
     * {@code gc} method may result in increasing the value returned
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   656
     * by {@code freeMemory.}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @return  an approximation to the total amount of memory currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *          available for future allocated objects, measured in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public native long freeMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Returns the total amount of memory in the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * The value returned by this method may vary over time, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * the host environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Note that the amount of memory required to hold an object of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * given type may be implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @return  the total amount of memory currently available for current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *          and future objects, measured in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public native long totalMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   677
     * Returns the maximum amount of memory that the Java virtual machine
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   678
     * will attempt to use.  If there is no inherent limit then the value
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   679
     * {@link java.lang.Long#MAX_VALUE} will be returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @return  the maximum amount of memory that the virtual machine will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *          attempt to use, measured in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public native long maxMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Runs the garbage collector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Calling this method suggests that the Java virtual machine expend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * effort toward recycling unused objects in order to make the memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * they currently occupy available for quick reuse. When control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * returns from the method call, the virtual machine has made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * its best effort to recycle all discarded objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p>
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   695
     * The name {@code gc} stands for "garbage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * collector". The virtual machine performs this recycling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * process automatically as needed, in a separate thread, even if the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   698
     * {@code gc} method is not invoked explicitly.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * The method {@link System#gc()} is the conventional and convenient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * means of invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    public native void gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    private static native void runFinalization0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Runs the finalization methods of any objects pending finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Calling this method suggests that the Java virtual machine expend
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   711
     * effort toward running the {@code finalize} methods of objects
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   712
     * that have been found to be discarded but whose {@code finalize}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * methods have not yet been run. When control returns from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * method call, the virtual machine has made a best effort to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * complete all outstanding finalizations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * The virtual machine performs the finalization process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * automatically as needed, in a separate thread, if the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   719
     * {@code runFinalization} method is not invoked explicitly.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * The method {@link System#runFinalization()} is the conventional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * and convenient means of invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @see     java.lang.Object#finalize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public void runFinalization() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        runFinalization0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   731
     * Not implemented, does nothing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   733
     * @deprecated
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   734
     * This method was intended to control instruction tracing.
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   735
     * It has been superseded by JVM-specific tracing mechanisms.
40793
fb8850791384 8165636: add removal text to Runtime.traceInstructions/MethodCalls deprecation text
smarks
parents: 39889
diff changeset
   736
     * This method is subject to removal in a future version of Java SE.
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   737
     *
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   738
     * @param on ignored
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   740
    @Deprecated(since="9", forRemoval=true)
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   741
    public void traceInstructions(boolean on) { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /**
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   744
     * Not implemented, does nothing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   746
     * @deprecated
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   747
     * This method was intended to control method call tracing.
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   748
     * It has been superseded by JVM-specific tracing mechanisms.
40793
fb8850791384 8165636: add removal text to Runtime.traceInstructions/MethodCalls deprecation text
smarks
parents: 39889
diff changeset
   749
     * This method is subject to removal in a future version of Java SE.
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   750
     *
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   751
     * @param on ignored
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
37606
e74fdd1e637f 8153330: deprecate Runtime.traceInstructions() and traceMethodCalls()
smarks
parents: 37521
diff changeset
   753
    @Deprecated(since="9", forRemoval=true)
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   754
    public void traceMethodCalls(boolean on) { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /**
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   757
     * Loads the native library specified by the filename argument.  The filename
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   758
     * argument must be an absolute path name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * (for example
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   760
     * {@code Runtime.getRuntime().load("/home/avh/lib/libX11.so");}).
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   761
     *
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   762
     * If the filename argument, when stripped of any platform-specific library
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   763
     * prefix, path, and file extension, indicates a library whose name is,
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   764
     * for example, L, and a native library called L is statically linked
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   765
     * with the VM, then the JNI_OnLoad_L function exported by the library
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   766
     * is invoked rather than attempting to load a dynamic library.
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   767
     * A filename matching the argument does not have to exist in the file
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47216
diff changeset
   768
     * system.
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47216
diff changeset
   769
     * See the <a href="{@docRoot}/../specs/jni/index.html"> JNI Specification</a>
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47216
diff changeset
   770
     * for more details.
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   771
     *
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   772
     * Otherwise, the filename argument is mapped to a native library image in
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   773
     * an implementation-dependent manner.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <p>
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   775
     * First, if there is a security manager, its {@code checkLink}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   776
     * method is called with the {@code filename} as its argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * This may result in a security exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * This is similar to the method {@link #loadLibrary(String)}, but it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * accepts a general file name as an argument rather than just a library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * name, allowing any file of native code to be loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * The method {@link System#load(String)} is the conventional and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * convenient means of invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @param      filename   the file to load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @exception  SecurityException  if a security manager exists and its
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   788
     *             {@code checkLink} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *             loading of the specified dynamic library
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   790
     * @exception  UnsatisfiedLinkError  if either the filename is not an
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   791
     *             absolute path name, the native library is not statically
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   792
     *             linked with the VM, or the library cannot be mapped to
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   793
     *             a native library image by the host system.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   794
     * @exception  NullPointerException if {@code filename} is
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   795
     *             {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @see        java.lang.Runtime#getRuntime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @see        java.lang.SecurityManager#checkLink(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
   800
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    public void load(String filename) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
   802
        load0(Reflection.getCallerClass(), filename);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9500
diff changeset
   805
    synchronized void load0(Class<?> fromClass, String filename) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            security.checkLink(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if (!(new File(filename).isAbsolute())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            throw new UnsatisfiedLinkError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                "Expecting an absolute path of the library: " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        ClassLoader.loadLibrary(fromClass, filename, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   818
     * Loads the native library specified by the {@code libname}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   819
     * argument.  The {@code libname} argument must not contain any platform
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   820
     * specific prefix, file extension or path. If a native library
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   821
     * called {@code libname} is statically linked with the VM, then the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   822
     * JNI_OnLoad_{@code libname} function exported by the library is invoked.
47707
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47216
diff changeset
   823
     * See the <a href="{@docRoot}/../specs/jni/index.html"> JNI Specification</a>
67aa34b019e1 8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents: 47216
diff changeset
   824
     * for more details.
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   825
     *
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   826
     * Otherwise, the libname argument is loaded from a system library
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   827
     * location and mapped to a native library image in an implementation-
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   828
     * dependent manner.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * <p>
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   830
     * First, if there is a security manager, its {@code checkLink}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   831
     * method is called with the {@code libname} as its argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * This may result in a security exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * The method {@link System#loadLibrary(String)} is the conventional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * and convenient means of invoking this method. If native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * methods are to be used in the implementation of a class, a standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * strategy is to put the native code in a library file (call it
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   838
     * {@code LibFile}) and then to put a static initializer:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * static { System.loadLibrary("LibFile"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * within the class declaration. When the class is loaded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * initialized, the necessary native code implementation for the native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * methods will then be loaded as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * If this method is called more than once with the same library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * name, the second and subsequent calls are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @param      libname   the name of the library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @exception  SecurityException  if a security manager exists and its
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   851
     *             {@code checkLink} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *             loading of the specified dynamic library
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   853
     * @exception  UnsatisfiedLinkError if either the libname argument
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   854
     *             contains a file path, the native library is not statically
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   855
     *             linked with the VM,  or the library cannot be mapped to a
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 14342
diff changeset
   856
     *             native library image by the host system.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   857
     * @exception  NullPointerException if {@code libname} is
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   858
     *             {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @see        java.lang.SecurityManager#checkLink(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
   862
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public void loadLibrary(String libname) {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16479
diff changeset
   864
        loadLibrary0(Reflection.getCallerClass(), libname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9500
diff changeset
   867
    synchronized void loadLibrary0(Class<?> fromClass, String libname) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            security.checkLink(libname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (libname.indexOf((int)File.separatorChar) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            throw new UnsatisfiedLinkError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    "Directory separator should not appear in library name: " + libname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        ClassLoader.loadLibrary(fromClass, libname, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Creates a localized version of an input stream. This method takes
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   881
     * an {@code InputStream} and returns an {@code InputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * equivalent to the argument in all respects except that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * localized: as characters in the local character set are read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * the stream, they are automatically converted from the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * character set to Unicode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * If the argument is already a localized stream, it may be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * as the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @param      in InputStream to localize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @return     a localized input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @see        java.io.InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @see        java.io.BufferedReader#BufferedReader(java.io.Reader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @see        java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @deprecated As of JDK&nbsp;1.1, the preferred way to translate a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * stream in the local encoding into a character stream in Unicode is via
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   897
     * the {@code InputStreamReader} and {@code BufferedReader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * classes.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   899
     * This method is subject to removal in a future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   901
    @Deprecated(since="1.1", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    public InputStream getLocalizedInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Creates a localized version of an output stream. This method
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   908
     * takes an {@code OutputStream} and returns an
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   909
     * {@code OutputStream} equivalent to the argument in all respects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * except that it is localized: as Unicode characters are written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * the stream, they are automatically converted to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * If the argument is already a localized stream, it may be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * as the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @deprecated As of JDK&nbsp;1.1, the preferred way to translate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Unicode character stream into a byte stream in the local encoding is via
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   919
     * the {@code OutputStreamWriter}, {@code BufferedWriter}, and
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 28872
diff changeset
   920
     * {@code PrintWriter} classes.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   921
     * This method is subject to removal in a future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param      out OutputStream to localize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @return     a localized output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @see        java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @see        java.io.BufferedWriter#BufferedWriter(java.io.Writer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @see        java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @see        java.io.PrintWriter#PrintWriter(java.io.OutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
   930
    @Deprecated(since="1.1", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public OutputStream getLocalizedOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   935
    /**
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
   936
     * Returns the version of the Java Runtime Environment as a {@link Version}.
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   937
     *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
   938
     * @return  the {@link Version} of the Java Runtime Environment
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   939
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   940
     * @since  9
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   941
     */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   942
    public static Version version() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   943
        if (version == null) {
39302
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
   944
            version = new Version(VersionProps.versionNumbers(),
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
   945
                    VersionProps.pre(), VersionProps.build(),
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
   946
                    VersionProps.optional());
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   947
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   948
        return version;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   949
    }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   950
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   951
    /**
39766
745f165bedee 8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents: 39302
diff changeset
   952
     * A representation of a version string for an implementation of the
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
   953
     * Java&nbsp;SE Platform.  A version string consists of a version number
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   954
     * optionally followed by pre-release and build information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   955
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
   956
     * <h2><a id="verNum">Version numbers</a></h2>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   957
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   958
     * <p> A <em>version number</em>, {@code $VNUM}, is a non-empty sequence
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   959
     * of elements separated by period characters (U+002E).  An element is
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
   960
     * either zero, or an unsigned integer numeral without leading zeros.  The
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   961
     * final element in a version number must not be zero.  The format is:
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   962
     * </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   963
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   964
     * <blockquote><pre>
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
   965
     *     [1-9][0-9]*((\.0)*\.[1-9][0-9]*)*
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   966
     * </pre></blockquote>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   967
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   968
     * <p> The sequence may be of arbitrary length but the first three
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   969
     * elements are assigned specific meanings, as follows:</p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   970
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   971
     * <blockquote><pre>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   972
     *     $MAJOR.$MINOR.$SECURITY
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   973
     * </pre></blockquote>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   974
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   975
     * <ul>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   976
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
   977
     * <li><p> <a id="major">{@code $MAJOR}</a> --- The major version
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   978
     * number, incremented for a major release that contains significant new
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   979
     * features as specified in a new edition of the Java&#160;SE Platform
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   980
     * Specification, <em>e.g.</em>, <a
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   981
     * href="https://jcp.org/en/jsr/detail?id=337">JSR 337</a> for
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   982
     * Java&#160;SE&#160;8.  Features may be removed in a major release, given
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   983
     * advance notice at least one major release ahead of time, and
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   984
     * incompatible changes may be made when justified. The {@code $MAJOR}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   985
     * version number of JDK&#160;8 is {@code 8}; the {@code $MAJOR} version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   986
     * number of JDK&#160;9 is {@code 9}.  When {@code $MAJOR} is incremented,
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   987
     * all subsequent elements are removed. </p></li>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   988
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
   989
     * <li><p> <a id="minor">{@code $MINOR}</a> --- The minor version
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   990
     * number, incremented for a minor update release that may contain
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   991
     * compatible bug fixes, revisions to standard APIs mandated by a
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   992
     * <a href="https://jcp.org/en/procedures/jcp2#5.3">Maintenance Release</a>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   993
     * of the relevant Platform Specification, and implementation features
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   994
     * outside the scope of that Specification such as new JDK-specific APIs,
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   995
     * additional service providers, new garbage collectors, and ports to new
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   996
     * hardware architectures. </p></li>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   997
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
   998
     * <li><p> <a id="security">{@code $SECURITY}</a> --- The security
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
   999
     * level, incremented for a security update release that contains critical
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1000
     * fixes including those necessary to improve security.  {@code $SECURITY}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1001
     * is <strong>not</strong> reset when {@code $MINOR} is incremented.  A
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1002
     * higher value of {@code $SECURITY} for a given {@code $MAJOR} value,
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1003
     * therefore, always indicates a more secure release, regardless of the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1004
     * value of {@code $MINOR}. </p></li>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1005
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1006
     * </ul>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1007
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1008
     * <p> The fourth and later elements of a version number are free for use
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1009
     * by downstream consumers of this code base.  Such a consumer may,
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1010
     * <em>e.g.</em>, use the fourth element to identify patch releases which
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1011
     * contain a small number of critical non-security fixes in addition to
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1012
     * the security fixes in the corresponding security release. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1013
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1014
     * <p> The version number does not include trailing zero elements;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1015
     * <em>i.e.</em>, {@code $SECURITY} is omitted if it has the value zero,
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1016
     * and {@code $MINOR} is omitted if both {@code $MINOR} and {@code
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1017
     * $SECURITY} have the value zero. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1018
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1019
     * <p> The sequence of numerals in a version number is compared to another
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1020
     * such sequence in numerical, pointwise fashion; <em>e.g.</em>, {@code
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1021
     * 9.9.1} is less than {@code 9.10.3}. If one sequence is shorter than
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1022
     * another then the missing elements of the shorter sequence are
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1023
     * considered to be less than the corresponding elements of the longer
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1024
     * sequence; <em>e.g.</em>, {@code 9.1.2} is less than {@code 9.1.2.1}.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1025
     * </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1026
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
  1027
     * <h2><a id="verStr">Version strings</a></h2>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1028
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1029
     * <p> A <em>version string</em>, {@code $VSTR}, consists of a version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1030
     * number {@code $VNUM}, as described above, optionally followed by
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1031
     * pre-release and build information, in one of the following formats:
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1032
     * </p>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1033
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1034
     * <blockquote><pre>
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1035
     *     $VNUM(-$PRE)?\+$BUILD(-$OPT)?
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1036
     *     $VNUM-$PRE(-$OPT)?
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1037
     *     $VNUM(+-$OPT)?
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1038
     * </pre></blockquote>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1039
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1040
     * <p> where: </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1041
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1042
     * <ul>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1043
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
  1044
     * <li><p> <a id="pre">{@code $PRE}</a>, matching {@code ([a-zA-Z0-9]+)}
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1045
     * --- A pre-release identifier.  Typically {@code ea}, for a
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1046
     * potentially unstable early-access release under active development,
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1047
     * or {@code internal}, for an internal developer build. </p></li>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1048
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
  1049
     * <li><p> <a id="build">{@code $BUILD}</a>, matching {@code
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1050
     * (0|[1-9][0-9]*)} --- The build number, incremented for each promoted
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1051
     * build.  {@code $BUILD} is reset to {@code 1} when any portion of {@code
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1052
     * $VNUM} is incremented. </p></li>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1053
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44785
diff changeset
  1054
     * <li><p> <a id="opt">{@code $OPT}</a>, matching {@code
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1055
     * ([-a-zA-Z0-9.]+)} --- Additional build information, if desired.  In
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1056
     * the case of an {@code internal} build this will often contain the date
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1057
     * and time of the build. </p></li>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1058
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1059
     * </ul>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1060
     *
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
  1061
     * <p> A version string {@code 10-ea} matches {@code $VNUM = "10"} and
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
  1062
     * {@code $PRE = "ea"}.  The version string {@code 10+-ea} matches
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1063
     * {@code $VNUM = "10"} and {@code $OPT = "ea"}. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1064
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1065
     * <p> When comparing two version strings, the value of {@code $OPT}, if
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1066
     * present, may or may not be significant depending on the chosen
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1067
     * comparison method.  The comparison methods {@link #compareTo(Version)
39766
745f165bedee 8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents: 39302
diff changeset
  1068
     * compareTo()} and {@link #compareToIgnoreOptional(Version)
745f165bedee 8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents: 39302
diff changeset
  1069
     * compareToIgnoreOptional()} should be used consistently with the
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1070
     * corresponding methods {@link #equals(Object) equals()} and {@link
39766
745f165bedee 8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents: 39302
diff changeset
  1071
     * #equalsIgnoreOptional(Object) equalsIgnoreOptional()}.  </p>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1072
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1073
     * <p> A <em>short version string</em>, {@code $SVSTR}, often useful in
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1074
     * less formal contexts, is a version number optionally followed by a
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1075
     * pre-release identifier:</p>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1076
     *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1077
     * <blockquote><pre>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1078
     *     $VNUM(-$PRE)?
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1079
     * </pre></blockquote>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1080
     *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1081
     * <p>This is a <a href="./doc-files/ValueBased.html">value-based</a>
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1082
     * class; use of identity-sensitive operations (including reference equality
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1083
     * ({@code ==}), identity hash code, or synchronization) on instances of
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1084
     * {@code Version} may have unpredictable results and should be avoided.
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1085
     * </p>
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1086
     *
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1087
     * @since  9
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1088
     */
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1089
    public static final class Version
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1090
        implements Comparable<Version>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1091
    {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1092
        private final List<Integer>     version;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1093
        private final Optional<String>  pre;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1094
        private final Optional<Integer> build;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1095
        private final Optional<String>  optional;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1096
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1097
        /*
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1098
         * List of version number components passed to this constructor MUST
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1099
         * be at least unmodifiable (ideally immutable). In the case on an
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1100
         * unmodifiable list, the caller MUST hand the list over to this
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1101
         * constructor and never change the underlying list.
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1102
         */
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1103
        private Version(List<Integer> unmodifiableListOfVersions,
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1104
                        Optional<String> pre,
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1105
                        Optional<Integer> build,
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1106
                        Optional<String> optional)
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1107
        {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1108
            this.version = unmodifiableListOfVersions;
39302
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1109
            this.pre = pre;
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1110
            this.build = build;
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1111
            this.optional = optional;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1112
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1113
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1114
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1115
         * Parses the given string as a valid
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1116
         * <a href="#verStr">version string</a> containing a
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1117
         * <a href="#verNum">version number</a> followed by pre-release and
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1118
         * build information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1119
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1120
         * @param  s
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1121
         *         A string to interpret as a version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1122
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1123
         * @throws  IllegalArgumentException
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1124
         *          If the given string cannot be interpreted as a valid
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1125
         *          version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1126
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1127
         * @throws  NullPointerException
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1128
         *          If the given string is {@code null}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1129
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1130
         * @throws  NumberFormatException
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1131
         *          If an element of the version number or the build number
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1132
         *          cannot be represented as an {@link Integer}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1133
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1134
         * @return  The Version of the given string
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1135
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1136
        public static Version parse(String s) {
39889
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1137
            if (s == null)
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1138
                throw new NullPointerException();
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1139
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1140
            // Shortcut to avoid initializing VersionPattern when creating
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1141
            // major version constants during startup
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1142
            if (isSimpleNumber(s)) {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1143
                return new Version(List.of(Integer.parseInt(s)),
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1144
                        Optional.empty(), Optional.empty(), Optional.empty());
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1145
            }
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1146
            Matcher m = VersionPattern.VSTR_PATTERN.matcher(s);
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1147
            if (!m.matches())
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1148
                throw new IllegalArgumentException("Invalid version string: '"
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1149
                                                   + s + "'");
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1150
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1151
            // $VNUM is a dot-separated list of integers of arbitrary length
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1152
            String[] split = m.group(VersionPattern.VNUM_GROUP).split("\\.");
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1153
            Integer[] version = new Integer[split.length];
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1154
            for (int i = 0; i < split.length; i++) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1155
                version[i] = Integer.parseInt(split[i]);
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1156
            }
39889
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1157
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1158
            Optional<String> pre = Optional.ofNullable(
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1159
                    m.group(VersionPattern.PRE_GROUP));
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1160
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1161
            String b = m.group(VersionPattern.BUILD_GROUP);
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1162
            // $BUILD is an integer
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1163
            Optional<Integer> build = (b == null)
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1164
                ? Optional.empty()
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1165
                : Optional.of(Integer.parseInt(b));
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1166
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1167
            Optional<String> optional = Optional.ofNullable(
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1168
                    m.group(VersionPattern.OPT_GROUP));
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1169
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1170
            // empty '+'
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1171
            if ((m.group(VersionPattern.PLUS_GROUP) != null)
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1172
                    && !build.isPresent()) {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1173
                if (optional.isPresent()) {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1174
                    if (pre.isPresent())
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1175
                        throw new IllegalArgumentException("'+' found with"
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1176
                            + " pre-release and optional components:'" + s
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1177
                            + "'");
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1178
                } else {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1179
                    throw new IllegalArgumentException("'+' found with neither"
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1180
                        + " build or optional components: '" + s + "'");
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1181
                }
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1182
            }
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1183
            return new Version(List.of(version), pre, build, optional);
39889
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1184
        }
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1185
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1186
        private static boolean isSimpleNumber(String s) {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1187
            for (int i = 0; i < s.length(); i++) {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1188
                char c = s.charAt(i);
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1189
                char lowerBound = (i > 0) ? '0' : '1';
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1190
                if (c < lowerBound || c > '9') {
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1191
                    return false;
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1192
                }
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1193
            }
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1194
            return true;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1195
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1196
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1197
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1198
         * Returns the <a href="#major">major</a> version number.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1199
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1200
         * @return  The major version number
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1201
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1202
        public int major() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1203
            return version.get(0);
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1204
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1205
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1206
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1207
         * Returns the <a href="#minor">minor</a> version number or zero if it
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1208
         * was not set.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1209
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1210
         * @return  The minor version number or zero if it was not set
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1211
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1212
        public int minor() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1213
            return (version.size() > 1 ? version.get(1) : 0);
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1214
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1215
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1216
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1217
         * Returns the <a href="#security">security</a> version number or zero
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1218
         * if it was not set.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1219
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1220
         * @return  The security version number or zero if it was not set
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1221
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1222
        public int security() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1223
            return (version.size() > 2 ? version.get(2) : 0);
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1224
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1225
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1226
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1227
         * Returns an unmodifiable {@link java.util.List List} of the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1228
         * integer numerals contained in the <a href="#verNum">version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1229
         * number</a>.  The {@code List} always contains at least one
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1230
         * element corresponding to the <a href="#major">major version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1231
         * number</a>.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1232
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1233
         * @return  An unmodifiable list of the integer numerals
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1234
         *          contained in the version number
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1235
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1236
        public List<Integer> version() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1237
            return version;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1238
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1239
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1240
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1241
         * Returns the optional <a href="#pre">pre-release</a> information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1242
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1243
         * @return  The optional pre-release information as a String
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1244
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1245
        public Optional<String> pre() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1246
            return pre;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1247
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1248
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1249
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1250
         * Returns the <a href="#build">build number</a>.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1251
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1252
         * @return  The optional build number.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1253
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1254
        public Optional<Integer> build() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1255
            return build;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1256
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1257
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1258
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1259
         * Returns <a href="#opt">optional</a> additional identifying build
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1260
         * information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1261
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1262
         * @return  Additional build information as a String
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1263
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1264
        public Optional<String> optional() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1265
            return optional;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1266
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1267
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1268
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1269
         * Compares this version to another.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1270
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1271
         * <p> Each of the components in the <a href="#verStr">version</a> is
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
  1272
         * compared in the following order of precedence: version numbers,
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1273
         * pre-release identifiers, build numbers, optional build information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1274
         * </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1275
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1276
         * <p> Comparison begins by examining the sequence of version numbers.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1277
         * If one sequence is shorter than another, then the missing elements
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1278
         * of the shorter sequence are considered to be less than the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1279
         * corresponding elements of the longer sequence. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1280
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1281
         * <p> A version with a pre-release identifier is always considered to
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1282
         * be less than a version without one.  Pre-release identifiers are
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1283
         * compared numerically when they consist only of digits, and
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1284
         * lexicographically otherwise.  Numeric identifiers are considered to
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1285
         * be less than non-numeric identifiers.  </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1286
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1287
         * <p> A version without a build number is always less than one with a
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1288
         * build number; otherwise build numbers are compared numerically. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1289
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1290
         * <p> The optional build information is compared lexicographically.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1291
         * During this comparison, a version with optional build information is
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1292
         * considered to be greater than a version without one. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1293
         *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1294
         * @param  obj
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1295
         *         The object to be compared
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1296
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1297
         * @return  A negative integer, zero, or a positive integer if this
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1298
         *          {@code Version} is less than, equal to, or greater than the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1299
         *          given {@code Version}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1300
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1301
         * @throws  NullPointerException
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1302
         *          If the given object is {@code null}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1303
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1304
        @Override
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1305
        public int compareTo(Version obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1306
            return compare(obj, false);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1307
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1308
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1309
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1310
         * Compares this version to another disregarding optional build
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1311
         * information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1312
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1313
         * <p> Two versions are compared by examining the version string as
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1314
         * described in {@link #compareTo(Version)} with the exception that the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1315
         * optional build information is always ignored. </p>
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1316
         *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1317
         * <p> This method provides ordering which is consistent with
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1318
         * {@code equalsIgnoreOptional()}. </p>
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1319
         *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1320
         * @param  obj
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1321
         *         The object to be compared
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1322
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1323
         * @return  A negative integer, zero, or a positive integer if this
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1324
         *          {@code Version} is less than, equal to, or greater than the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1325
         *          given {@code Version}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1326
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1327
         * @throws  NullPointerException
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1328
         *          If the given object is {@code null}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1329
         */
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1330
        public int compareToIgnoreOptional(Version obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1331
            return compare(obj, true);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1332
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1333
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1334
        private int compare(Version obj, boolean ignoreOpt) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1335
            if (obj == null)
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1336
                throw new NullPointerException();
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1337
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1338
            int ret = compareVersion(obj);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1339
            if (ret != 0)
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1340
                return ret;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1341
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1342
            ret = comparePre(obj);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1343
            if (ret != 0)
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1344
                return ret;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1345
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1346
            ret = compareBuild(obj);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1347
            if (ret != 0)
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1348
                return ret;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1349
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1350
            if (!ignoreOpt)
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1351
                return compareOptional(obj);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1352
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1353
            return 0;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1354
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1355
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1356
        private int compareVersion(Version obj) {
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1357
            int size = version.size();
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1358
            int oSize = obj.version().size();
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1359
            int min = Math.min(size, oSize);
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1360
            for (int i = 0; i < min; i++) {
44272
4d16899cb312 8176882: Incorrect integer comparison in version numbers
prappo
parents: 44265
diff changeset
  1361
                int val = version.get(i);
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1362
                int oVal = obj.version().get(i);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1363
                if (val != oVal)
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1364
                    return val - oVal;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1365
            }
44272
4d16899cb312 8176882: Incorrect integer comparison in version numbers
prappo
parents: 44265
diff changeset
  1366
            return size - oSize;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1367
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1368
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1369
        private int comparePre(Version obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1370
            Optional<String> oPre = obj.pre();
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1371
            if (!pre.isPresent()) {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1372
                if (oPre.isPresent())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1373
                    return 1;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1374
            } else {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1375
                if (!oPre.isPresent())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1376
                    return -1;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1377
                String val = pre.get();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1378
                String oVal = oPre.get();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1379
                if (val.matches("\\d+")) {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1380
                    return (oVal.matches("\\d+")
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1381
                        ? (new BigInteger(val)).compareTo(new BigInteger(oVal))
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1382
                        : -1);
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1383
                } else {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1384
                    return (oVal.matches("\\d+")
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1385
                        ? 1
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1386
                        : val.compareTo(oVal));
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1387
                }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1388
            }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1389
            return 0;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1390
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1391
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1392
        private int compareBuild(Version obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1393
            Optional<Integer> oBuild = obj.build();
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1394
            if (oBuild.isPresent()) {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1395
                return (build.isPresent()
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1396
                        ? build.get().compareTo(oBuild.get())
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
  1397
                        : -1);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1398
            } else if (build.isPresent()) {
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
  1399
                return 1;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1400
            }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1401
            return 0;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1402
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1403
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1404
        private int compareOptional(Version obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1405
            Optional<String> oOpt = obj.optional();
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1406
            if (!optional.isPresent()) {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1407
                if (oOpt.isPresent())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1408
                    return -1;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1409
            } else {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1410
                if (!oOpt.isPresent())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1411
                    return 1;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1412
                return optional.get().compareTo(oOpt.get());
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1413
            }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1414
            return 0;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1415
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1416
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1417
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1418
         * Returns a string representation of this version.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1419
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1420
         * @return  The version string
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1421
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1422
        @Override
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1423
        public String toString() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1424
            StringBuilder sb
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1425
                = new StringBuilder(version.stream()
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1426
                    .map(Object::toString)
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1427
                    .collect(Collectors.joining(".")));
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1428
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1429
            pre.ifPresent(v -> sb.append("-").append(v));
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1430
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1431
            if (build.isPresent()) {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1432
                sb.append("+").append(build.get());
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1433
                if (optional.isPresent())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1434
                    sb.append("-").append(optional.get());
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1435
            } else {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1436
                if (optional.isPresent()) {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1437
                    sb.append(pre.isPresent() ? "-" : "+-");
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1438
                    sb.append(optional.get());
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1439
                }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1440
            }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1441
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1442
            return sb.toString();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1443
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1444
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1445
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1446
         * Determines whether this {@code Version} is equal to another object.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1447
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1448
         * <p> Two {@code Version}s are equal if and only if they represent the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1449
         * same version string.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1450
         *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1451
         * @param  obj
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1452
         *         The object to which this {@code Version} is to be compared
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1453
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1454
         * @return  {@code true} if, and only if, the given object is a {@code
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1455
         *          Version} that is identical to this {@code Version}
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1456
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1457
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1458
        @Override
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1459
        public boolean equals(Object obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1460
            boolean ret = equalsIgnoreOptional(obj);
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1461
            if (!ret)
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1462
                return false;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1463
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1464
            Version that = (Version)obj;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1465
            return (this.optional().equals(that.optional()));
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1466
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1467
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1468
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1469
         * Determines whether this {@code Version} is equal to another
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1470
         * disregarding optional build information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1471
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1472
         * <p> Two {@code Version}s are equal if and only if they represent the
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1473
         * same version string disregarding the optional build information.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1474
         *
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1475
         * @param  obj
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1476
         *         The object to which this {@code Version} is to be compared
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1477
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1478
         * @return  {@code true} if, and only if, the given object is a {@code
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1479
         *          Version} that is identical to this {@code Version}
44265
ac63ae089927 8160956: Runtime.Version.compareTo/compareToIgnoreOpt problem
prappo
parents: 40793
diff changeset
  1480
         *          ignoring the optional build information
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1481
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1482
         */
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1483
        public boolean equalsIgnoreOptional(Object obj) {
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1484
            if (this == obj)
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1485
                return true;
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1486
            if (!(obj instanceof Version))
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1487
                return false;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1488
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1489
            Version that = (Version)obj;
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1490
            return (this.version().equals(that.version())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1491
                && this.pre().equals(that.pre())
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1492
                && this.build().equals(that.build()));
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1493
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1494
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1495
        /**
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1496
         * Returns the hash code of this version.
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1497
         *
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1498
         * @return  The hashcode of this version
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1499
         */
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1500
        @Override
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1501
        public int hashCode() {
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1502
            int h = 1;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1503
            int p = 17;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1504
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1505
            h = p * h + version.hashCode();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1506
            h = p * h + pre.hashCode();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1507
            h = p * h + build.hashCode();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1508
            h = p * h + optional.hashCode();
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1509
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1510
            return h;
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1511
        }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1512
    }
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37606
diff changeset
  1513
39889
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1514
    private static class VersionPattern {
39302
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1515
        // $VNUM(-$PRE)?(\+($BUILD)?(\-$OPT)?)?
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1516
        // RE limits the format of version strings
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1517
        // ([1-9][0-9]*(?:(?:\.0)*\.[1-9][0-9]*)*)(?:-([a-zA-Z0-9]+))?(?:(\+)(0|[1-9][0-9]*)?)?(?:-([-a-zA-Z0-9.]+))?
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1518
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1519
        private static final String VNUM
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1520
            = "(?<VNUM>[1-9][0-9]*(?:(?:\\.0)*\\.[1-9][0-9]*)*)";
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1521
        private static final String PRE      = "(?:-(?<PRE>[a-zA-Z0-9]+))?";
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1522
        private static final String BUILD
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1523
            = "(?:(?<PLUS>\\+)(?<BUILD>0|[1-9][0-9]*)?)?";
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1524
        private static final String OPT      = "(?:-(?<OPT>[-a-zA-Z0-9.]+))?";
44785
62a18e20f5c1 8177738: Runtime.Version must be a value-based class
prappo
parents: 44272
diff changeset
  1525
        private static final String VSTR_FORMAT = VNUM + PRE + BUILD + OPT;
39302
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1526
39889
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1527
        static final Pattern VSTR_PATTERN = Pattern.compile(VSTR_FORMAT);
39302
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1528
39889
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1529
        static final String VNUM_GROUP  = "VNUM";
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1530
        static final String PRE_GROUP   = "PRE";
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1531
        static final String PLUS_GROUP  = "PLUS";
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1532
        static final String BUILD_GROUP = "BUILD";
2e82e1c36e90 8162439: Runtime.Version.parse needs fast-path for major versions
redestad
parents: 39766
diff changeset
  1533
        static final String OPT_GROUP   = "OPT";
39302
aa8d0bc2a6d2 8160000: Runtime.version() cause startup regressions in 9+119
redestad
parents: 38432
diff changeset
  1534
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
}