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