8341445: DecimalFormatSymbols setters should throw NPE

Reviewed-by: naoto
This commit is contained in:
Justin Lu 2024-10-22 17:21:38 +00:00
parent aafc8d0dcb
commit c61d2c5a34
2 changed files with 137 additions and 30 deletions

View file

@ -349,10 +349,11 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
* unchanged.
*
* @param infinity the string representing infinity
* @throws NullPointerException if {@code infinity} is {@code null}
*/
public void setInfinity(String infinity) {
this.infinity = Objects.requireNonNull(infinity);
hashCode = 0;
this.infinity = infinity;
}
/**
@ -370,10 +371,11 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
* unchanged.
*
* @param NaN the string representing "not a number"
* @throws NullPointerException if {@code NaN} is {@code null}
*/
public void setNaN(String NaN) {
this.NaN = Objects.requireNonNull(NaN);
hashCode = 0;
this.NaN = NaN;
}
/**
@ -414,14 +416,18 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
}
/**
* Sets the currency symbol for the currency of these
* DecimalFormatSymbols in their locale.
* Sets the currency symbol for the currency of this
* {@code DecimalFormatSymbols} in their locale. Unlike {@link
* #setInternationalCurrencySymbol(String)}, this method does not update
* the currency attribute nor the international currency symbol attribute.
*
* @param currency the currency symbol
* @throws NullPointerException if {@code currency} is {@code null}
* @since 1.2
*/
public void setCurrencySymbol(String currency)
{
Objects.requireNonNull(currency);
initializeCurrency(locale);
hashCode = 0;
currencySymbol = currency;
@ -448,35 +454,29 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
* this also sets the currency attribute to the corresponding Currency
* instance and the currency symbol attribute to the currency's symbol
* in the DecimalFormatSymbols' locale. If the currency code is not valid,
* then the currency attribute is set to null and the currency symbol
* attribute is not modified.
* then the currency attribute and the currency symbol attribute are not modified.
*
* @param currencyCode the currency code
* @throws NullPointerException if {@code currencyCode} is {@code null}
* @see #setCurrency
* @see #setCurrencySymbol
* @since 1.2
*/
public void setInternationalCurrencySymbol(String currencyCode)
{
public void setInternationalCurrencySymbol(String currencyCode) {
Objects.requireNonNull(currencyCode);
// init over setting currencyInit flag as true so that currency has
// fallback if code is not valid
initializeCurrency(locale);
hashCode = 0;
intlCurrencySymbol = currencyCode;
currency = null;
if (currencyCode != null) {
try {
currency = Currency.getInstance(currencyCode);
currencySymbol = currency.getSymbol();
} catch (IllegalArgumentException e) {
}
}
try {
currency = Currency.getInstance(currencyCode);
currencySymbol = currency.getSymbol(locale);
} catch (IllegalArgumentException _) {} // Simply ignore if not valid
}
/**
* Gets the currency of these DecimalFormatSymbols. May be null if the
* currency symbol attribute was previously set to a value that's not
* a valid ISO 4217 currency code.
*
* @return the currency used, or null
* {@return the {@code Currency} of this {@code DecimalFormatSymbols}}
* @since 1.4
*/
public Currency getCurrency() {
@ -485,7 +485,7 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
}
/**
* Sets the currency of these DecimalFormatSymbols.
* Sets the currency of this {@code DecimalFormatSymbols}.
* This also sets the currency symbol attribute to the currency's symbol
* in the DecimalFormatSymbols' locale, and the international currency
* symbol attribute to the currency's ISO 4217 currency code.
@ -497,9 +497,7 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
* @see #setInternationalCurrencySymbol
*/
public void setCurrency(Currency currency) {
if (currency == null) {
throw new NullPointerException();
}
Objects.requireNonNull(currency);
initializeCurrency(locale);
hashCode = 0;
this.currency = currency;
@ -555,9 +553,7 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
*/
public void setExponentSeparator(String exp)
{
if (exp == null) {
throw new NullPointerException();
}
Objects.requireNonNull(exp);
hashCode = 0;
exponentialSeparator = exp;
}
@ -767,7 +763,8 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
patternSeparator == other.patternSeparator &&
infinity.equals(other.infinity) &&
NaN.equals(other.NaN) &&
getCurrencySymbol().equals(other.getCurrencySymbol()) && // possible currency init occurs here
// Currency fields are lazy. Init via get call to ensure non-null
getCurrencySymbol().equals(other.getCurrencySymbol()) &&
intlCurrencySymbol.equals(other.intlCurrencySymbol) &&
currency == other.currency &&
monetarySeparator == other.monetarySeparator &&
@ -800,7 +797,8 @@ public class DecimalFormatSymbols implements Cloneable, Serializable {
patternSeparator,
infinity,
NaN,
getCurrencySymbol(), // possible currency init occurs here
// Currency fields are lazy. Init via get call to ensure non-null
getCurrencySymbol(),
intlCurrencySymbol,
currency,
monetarySeparator,