8176425: Add radix indication in NumberFormatException message for Integer.decode
authordarcy
Wed, 15 Aug 2018 10:16:15 -0700
changeset 51412 8f7e3f9ddbc0
parent 51411 4699147a4f91
child 51413 43e41800d579
8176425: Add radix indication in NumberFormatException message for Integer.decode Reviewed-by: lancea
src/java.base/share/classes/java/lang/Integer.java
src/java.base/share/classes/java/lang/Long.java
src/java.base/share/classes/java/lang/NumberFormatException.java
src/java.base/share/classes/java/lang/Package.java
--- a/src/java.base/share/classes/java/lang/Integer.java	Wed Aug 15 10:00:16 2018 -0700
+++ b/src/java.base/share/classes/java/lang/Integer.java	Wed Aug 15 10:16:15 2018 -0700
@@ -635,11 +635,11 @@
                     negative = true;
                     limit = Integer.MIN_VALUE;
                 } else if (firstChar != '+') {
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
 
                 if (len == 1) { // Cannot have lone "+" or "-"
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
                 i++;
             }
@@ -649,17 +649,17 @@
                 // Accumulating negatively avoids surprises near MAX_VALUE
                 int digit = Character.digit(s.charAt(i++), radix);
                 if (digit < 0 || result < multmin) {
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
                 result *= radix;
                 if (result < limit + digit) {
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
                 result -= digit;
             }
             return negative ? result : -result;
         } else {
-            throw NumberFormatException.forInputString(s);
+            throw NumberFormatException.forInputString(s, radix);
         }
     }
 
@@ -745,7 +745,7 @@
             }
             return negative ? result : -result;
         } else {
-            throw NumberFormatException.forInputString("");
+            throw NumberFormatException.forInputString("", radix);
         }
     }
 
@@ -842,7 +842,7 @@
                 }
             }
         } else {
-            throw NumberFormatException.forInputString(s);
+            throw NumberFormatException.forInputString(s, radix);
         }
     }
 
--- a/src/java.base/share/classes/java/lang/Long.java	Wed Aug 15 10:00:16 2018 -0700
+++ b/src/java.base/share/classes/java/lang/Long.java	Wed Aug 15 10:16:15 2018 -0700
@@ -675,11 +675,11 @@
                     negative = true;
                     limit = Long.MIN_VALUE;
                 } else if (firstChar != '+') {
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
 
                 if (len == 1) { // Cannot have lone "+" or "-"
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
                 i++;
             }
@@ -689,17 +689,17 @@
                 // Accumulating negatively avoids surprises near MAX_VALUE
                 int digit = Character.digit(s.charAt(i++),radix);
                 if (digit < 0 || result < multmin) {
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
                 result *= radix;
                 if (result < limit + digit) {
-                    throw NumberFormatException.forInputString(s);
+                    throw NumberFormatException.forInputString(s, radix);
                 }
                 result -= digit;
             }
             return negative ? result : -result;
         } else {
-            throw NumberFormatException.forInputString(s);
+            throw NumberFormatException.forInputString(s, radix);
         }
     }
 
@@ -945,7 +945,7 @@
                 return result;
             }
         } else {
-            throw NumberFormatException.forInputString(s);
+            throw NumberFormatException.forInputString(s, radix);
         }
     }
 
@@ -1063,7 +1063,7 @@
                 return result;
             }
         } else {
-            throw NumberFormatException.forInputString("");
+            throw NumberFormatException.forInputString("", radix);
         }
     }
 
--- a/src/java.base/share/classes/java/lang/NumberFormatException.java	Wed Aug 15 10:00:16 2018 -0700
+++ b/src/java.base/share/classes/java/lang/NumberFormatException.java	Wed Aug 15 10:16:15 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -61,8 +61,11 @@
      *
      * @param   s   the input causing the error
      */
-    static NumberFormatException forInputString(String s) {
-        return new NumberFormatException("For input string: \"" + s + "\"");
+    static NumberFormatException forInputString(String s, int radix) {
+        return new NumberFormatException("For input string: \"" + s + "\"" +
+                                         (radix == 10 ?
+                                          "" :
+                                          " under radix " + radix));
     }
 
     /**
--- a/src/java.base/share/classes/java/lang/Package.java	Wed Aug 15 10:00:16 2018 -0700
+++ b/src/java.base/share/classes/java/lang/Package.java	Wed Aug 15 10:16:15 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -286,7 +286,7 @@
         for (int i = 0; i < sa.length; i++) {
             si[i] = Integer.parseInt(sa[i]);
             if (si[i] < 0)
-                throw NumberFormatException.forInputString("" + si[i]);
+                throw NumberFormatException.forInputString("" + si[i], 10);
         }
 
         String [] da = desired.split("\\.", -1);
@@ -294,7 +294,7 @@
         for (int i = 0; i < da.length; i++) {
             di[i] = Integer.parseInt(da[i]);
             if (di[i] < 0)
-                throw NumberFormatException.forInputString("" + di[i]);
+                throw NumberFormatException.forInputString("" + di[i], 10);
         }
 
         int len = Math.max(di.length, si.length);