8176425: Add radix indication in NumberFormatException message for Integer.decode

Reviewed-by: lancea
This commit is contained in:
Joe Darcy 2018-08-15 10:16:15 -07:00
parent b71faed5df
commit b0954bc7ab
4 changed files with 23 additions and 20 deletions

View file

@ -635,11 +635,11 @@ public final class Integer extends Number implements Comparable<Integer> {
negative = true; negative = true;
limit = Integer.MIN_VALUE; limit = Integer.MIN_VALUE;
} else if (firstChar != '+') { } else if (firstChar != '+') {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
if (len == 1) { // Cannot have lone "+" or "-" if (len == 1) { // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
i++; i++;
} }
@ -649,17 +649,17 @@ public final class Integer extends Number implements Comparable<Integer> {
// Accumulating negatively avoids surprises near MAX_VALUE // Accumulating negatively avoids surprises near MAX_VALUE
int digit = Character.digit(s.charAt(i++), radix); int digit = Character.digit(s.charAt(i++), radix);
if (digit < 0 || result < multmin) { if (digit < 0 || result < multmin) {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
result *= radix; result *= radix;
if (result < limit + digit) { if (result < limit + digit) {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
result -= digit; result -= digit;
} }
return negative ? result : -result; return negative ? result : -result;
} else { } else {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
} }
@ -745,7 +745,7 @@ public final class Integer extends Number implements Comparable<Integer> {
} }
return negative ? result : -result; return negative ? result : -result;
} else { } else {
throw NumberFormatException.forInputString(""); throw NumberFormatException.forInputString("", radix);
} }
} }
@ -842,7 +842,7 @@ public final class Integer extends Number implements Comparable<Integer> {
} }
} }
} else { } else {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
} }

View file

@ -675,11 +675,11 @@ public final class Long extends Number implements Comparable<Long> {
negative = true; negative = true;
limit = Long.MIN_VALUE; limit = Long.MIN_VALUE;
} else if (firstChar != '+') { } else if (firstChar != '+') {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
if (len == 1) { // Cannot have lone "+" or "-" if (len == 1) { // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
i++; i++;
} }
@ -689,17 +689,17 @@ public final class Long extends Number implements Comparable<Long> {
// Accumulating negatively avoids surprises near MAX_VALUE // Accumulating negatively avoids surprises near MAX_VALUE
int digit = Character.digit(s.charAt(i++),radix); int digit = Character.digit(s.charAt(i++),radix);
if (digit < 0 || result < multmin) { if (digit < 0 || result < multmin) {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
result *= radix; result *= radix;
if (result < limit + digit) { if (result < limit + digit) {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
result -= digit; result -= digit;
} }
return negative ? result : -result; return negative ? result : -result;
} else { } else {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
} }
@ -945,7 +945,7 @@ public final class Long extends Number implements Comparable<Long> {
return result; return result;
} }
} else { } else {
throw NumberFormatException.forInputString(s); throw NumberFormatException.forInputString(s, radix);
} }
} }
@ -1063,7 +1063,7 @@ public final class Long extends Number implements Comparable<Long> {
return result; return result;
} }
} else { } else {
throw NumberFormatException.forInputString(""); throw NumberFormatException.forInputString("", radix);
} }
} }

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -61,8 +61,11 @@ class NumberFormatException extends IllegalArgumentException {
* *
* @param s the input causing the error * @param s the input causing the error
*/ */
static NumberFormatException forInputString(String s) { static NumberFormatException forInputString(String s, int radix) {
return new NumberFormatException("For input string: \"" + s + "\""); return new NumberFormatException("For input string: \"" + s + "\"" +
(radix == 10 ?
"" :
" under radix " + radix));
} }
/** /**

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -286,7 +286,7 @@ public class Package extends NamedPackage implements java.lang.reflect.Annotated
for (int i = 0; i < sa.length; i++) { for (int i = 0; i < sa.length; i++) {
si[i] = Integer.parseInt(sa[i]); si[i] = Integer.parseInt(sa[i]);
if (si[i] < 0) if (si[i] < 0)
throw NumberFormatException.forInputString("" + si[i]); throw NumberFormatException.forInputString("" + si[i], 10);
} }
String [] da = desired.split("\\.", -1); String [] da = desired.split("\\.", -1);
@ -294,7 +294,7 @@ public class Package extends NamedPackage implements java.lang.reflect.Annotated
for (int i = 0; i < da.length; i++) { for (int i = 0; i < da.length; i++) {
di[i] = Integer.parseInt(da[i]); di[i] = Integer.parseInt(da[i]);
if (di[i] < 0) if (di[i] < 0)
throw NumberFormatException.forInputString("" + di[i]); throw NumberFormatException.forInputString("" + di[i], 10);
} }
int len = Math.max(di.length, si.length); int len = Math.max(di.length, si.length);