< prev index next >

src/java.base/share/classes/java/lang/Runtime.java

Print this page
rev 15134 : 8162439: Runtime.Version.parse needs fast-path for major versions
Reviewed-by: sdrach, iris

@@ -1110,10 +1110,22 @@
          *          cannot be represented as an {@link Integer}
          *
          * @return  The Version of the given string
          */
         public static Version parse(String s) {
+            // Shortcut to avoid initializing VersionBuilder when creating
+            // a major version constant, e.g., Runtime.Version.parse("8");
+            if (s.indexOf('.') == -1
+                    && s.indexOf('-') == -1
+                    && s.indexOf('+') == -1) {
+                if (s.isEmpty() || s.charAt(0) == '0') {
+                    throw new IllegalArgumentException(
+                        "Invalid version string: '" + s + "'");
+                }
+                return new Version(List.of(Integer.parseInt(s)),
+                        Optional.empty(), Optional.empty(), Optional.empty());
+            }
             return VersionBuilder.parse(s);
         }
 
         /**
          * Returns the <a href="#major">major</a> version number.
< prev index next >