< prev index next >

src/java.base/share/classes/jdk/Version.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk;
  27 
  28 import java.math.BigInteger;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.ArrayList;
  32 import java.util.regex.Matcher;
  33 import java.util.regex.Pattern;
  34 import java.util.stream.Collectors;
  35 import java.util.Collections;
  36 import java.util.List;
  37 import java.util.Optional;

  38 
  39 /**
  40  * A representation of the JDK version-string which contains a version
  41  * number optionally followed by pre-release and build information.
  42  *
  43  * <h2><a name="verNum">Version numbers</a></h2>
  44  *
  45  * A <em>version number</em>, {@code $VNUM}, is a non-empty sequence of
  46  * non-negative integer numerals, without leading or trailing zeroes,
  47  * separated by period characters (U+002E); i.e., it matches the regular
  48  * expression {@code ^[1-9][0-9]*(((\.0)*\.[1-9][0-9]*)*)*$}. The sequence may
  49  * be of arbitrary length but the first three elements are assigned specific
  50  * meanings, as follows:
  51  *
  52  * <blockquote><pre>
  53  *     $MAJOR.$MINOR.$SECURITY
  54  * </pre></blockquote>
  55  *
  56  * <ul>
  57  *


 257      *
 258      * @return  This version
 259      */
 260     public static Version parse(String s) {
 261         return new Version(s);
 262     }
 263 
 264     /**
 265      * Returns {@code System.getProperty("java.version")} as a Version.
 266      *
 267      * @throws  SecurityException
 268      *          If a security manager exists and its {@link
 269      *          SecurityManager#checkPropertyAccess(String)
 270      *          checkPropertyAccess} method does not allow access to the
 271      *          system property "java.version"
 272      *
 273      * @return  {@code System.getProperty("java.version")} as a Version
 274      */
 275     public static Version current() {
 276         if (current == null) {
 277             current = parse(AccessController.doPrivileged(
 278                 new PrivilegedAction<>() {
 279                     public String run() {
 280                         return System.getProperty("java.version");
 281                     }
 282                 }));
 283         }
 284         return current;
 285     }
 286 
 287     /**
 288      * Returns the <a href="#major">major</a> version number.
 289      *
 290      * @return  The major version number
 291      */
 292     public int major() {
 293         return version.get(0);
 294     }
 295 
 296     /**
 297      * Returns the <a href="#minor">minor</a> version number or zero if it was
 298      * not set.
 299      *
 300      * @return  The minor version number or zero if it was not set
 301      */
 302     public int minor() {




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk;
  27 
  28 import java.math.BigInteger;


  29 import java.util.ArrayList;
  30 import java.util.regex.Matcher;
  31 import java.util.regex.Pattern;
  32 import java.util.stream.Collectors;
  33 import java.util.Collections;
  34 import java.util.List;
  35 import java.util.Optional;
  36 import sun.security.action.GetPropertyAction;
  37 
  38 /**
  39  * A representation of the JDK version-string which contains a version
  40  * number optionally followed by pre-release and build information.
  41  *
  42  * <h2><a name="verNum">Version numbers</a></h2>
  43  *
  44  * A <em>version number</em>, {@code $VNUM}, is a non-empty sequence of
  45  * non-negative integer numerals, without leading or trailing zeroes,
  46  * separated by period characters (U+002E); i.e., it matches the regular
  47  * expression {@code ^[1-9][0-9]*(((\.0)*\.[1-9][0-9]*)*)*$}. The sequence may
  48  * be of arbitrary length but the first three elements are assigned specific
  49  * meanings, as follows:
  50  *
  51  * <blockquote><pre>
  52  *     $MAJOR.$MINOR.$SECURITY
  53  * </pre></blockquote>
  54  *
  55  * <ul>
  56  *


 256      *
 257      * @return  This version
 258      */
 259     public static Version parse(String s) {
 260         return new Version(s);
 261     }
 262 
 263     /**
 264      * Returns {@code System.getProperty("java.version")} as a Version.
 265      *
 266      * @throws  SecurityException
 267      *          If a security manager exists and its {@link
 268      *          SecurityManager#checkPropertyAccess(String)
 269      *          checkPropertyAccess} method does not allow access to the
 270      *          system property "java.version"
 271      *
 272      * @return  {@code System.getProperty("java.version")} as a Version
 273      */
 274     public static Version current() {
 275         if (current == null) {
 276             current = parse(GetPropertyAction.getProperty("java.version"));





 277         }
 278         return current;
 279     }
 280 
 281     /**
 282      * Returns the <a href="#major">major</a> version number.
 283      *
 284      * @return  The major version number
 285      */
 286     public int major() {
 287         return version.get(0);
 288     }
 289 
 290     /**
 291      * Returns the <a href="#minor">minor</a> version number or zero if it was
 292      * not set.
 293      *
 294      * @return  The minor version number or zero if it was not set
 295      */
 296     public int minor() {


< prev index next >