JDK 12 RFR of 8176425: Add radix indication in NumberFormatException message for Integer.decode (original) (raw)

joe darcy joe.darcy at oracle.com
Wed Aug 15 03:20:09 UTC 2018


Hello,

Please review the changes to address:

    8176425: Add radix indication in NumberFormatException message for Integer.decode     http://cr.openjdk.java.net/~darcy/8176425.0/

Basically the radix used for parsing an int or long is passed along to a factory method in the NumberFormatException class and the radix is included in the exception message for non-decimal radices.

Patch below.

Thanks,

-Joe

--- old/src/java.base/share/classes/java/lang/Integer.java 2018-08-14 20:15:45.642000000 -0700 +++ new/src/java.base/share/classes/java/lang/Integer.java 2018-08-14 20:15:45.498000000 -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);          }      }

--- old/src/java.base/share/classes/java/lang/Long.java 2018-08-14 20:15:46.030000000 -0700 +++ new/src/java.base/share/classes/java/lang/Long.java 2018-08-14 20:15:45.882000000 -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);          }      }

--- old/src/java.base/share/classes/java/lang/NumberFormatException.java 2018-08-14 20:15:46.418000000 -0700 +++ new/src/java.base/share/classes/java/lang/NumberFormatException.java 2018-08-14 20:15:46.274000000 -0700 @@ -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));      }

     /** --- old/src/java.base/share/classes/java/lang/Package.java 2018-08-14 20:15:46.790000000 -0700 +++ new/src/java.base/share/classes/java/lang/Package.java 2018-08-14 20:15:46.646000000 -0700 @@ -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,8 +294,8 @@          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);          for (int i = 0; i < len; i++) {



More information about the core-libs-dev mailing list