Skip to content

Commit 8269657

Browse files
Justin Lupull[bot]
Justin Lu
authored andcommitted
8341445: DecimalFormatSymbols setters should throw NPE
Reviewed-by: naoto
1 parent 681b78b commit 8269657

File tree

2 files changed

+137
-30
lines changed

2 files changed

+137
-30
lines changed

src/java.base/share/classes/java/text/DecimalFormatSymbols.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,11 @@ public String getInfinity() {
349349
* unchanged.
350350
*
351351
* @param infinity the string representing infinity
352+
* @throws NullPointerException if {@code infinity} is {@code null}
352353
*/
353354
public void setInfinity(String infinity) {
355+
this.infinity = Objects.requireNonNull(infinity);
354356
hashCode = 0;
355-
this.infinity = infinity;
356357
}
357358

358359
/**
@@ -370,10 +371,11 @@ public String getNaN() {
370371
* unchanged.
371372
*
372373
* @param NaN the string representing "not a number"
374+
* @throws NullPointerException if {@code NaN} is {@code null}
373375
*/
374376
public void setNaN(String NaN) {
377+
this.NaN = Objects.requireNonNull(NaN);
375378
hashCode = 0;
376-
this.NaN = NaN;
377379
}
378380

379381
/**
@@ -414,14 +416,18 @@ public String getCurrencySymbol()
414416
}
415417

416418
/**
417-
* Sets the currency symbol for the currency of these
418-
* DecimalFormatSymbols in their locale.
419+
* Sets the currency symbol for the currency of this
420+
* {@code DecimalFormatSymbols} in their locale. Unlike {@link
421+
* #setInternationalCurrencySymbol(String)}, this method does not update
422+
* the currency attribute nor the international currency symbol attribute.
419423
*
420424
* @param currency the currency symbol
425+
* @throws NullPointerException if {@code currency} is {@code null}
421426
* @since 1.2
422427
*/
423428
public void setCurrencySymbol(String currency)
424429
{
430+
Objects.requireNonNull(currency);
425431
initializeCurrency(locale);
426432
hashCode = 0;
427433
currencySymbol = currency;
@@ -448,35 +454,29 @@ public String getInternationalCurrencySymbol()
448454
* this also sets the currency attribute to the corresponding Currency
449455
* instance and the currency symbol attribute to the currency's symbol
450456
* in the DecimalFormatSymbols' locale. If the currency code is not valid,
451-
* then the currency attribute is set to null and the currency symbol
452-
* attribute is not modified.
457+
* then the currency attribute and the currency symbol attribute are not modified.
453458
*
454459
* @param currencyCode the currency code
460+
* @throws NullPointerException if {@code currencyCode} is {@code null}
455461
* @see #setCurrency
456462
* @see #setCurrencySymbol
457463
* @since 1.2
458464
*/
459-
public void setInternationalCurrencySymbol(String currencyCode)
460-
{
465+
public void setInternationalCurrencySymbol(String currencyCode) {
466+
Objects.requireNonNull(currencyCode);
467+
// init over setting currencyInit flag as true so that currency has
468+
// fallback if code is not valid
461469
initializeCurrency(locale);
462470
hashCode = 0;
463471
intlCurrencySymbol = currencyCode;
464-
currency = null;
465-
if (currencyCode != null) {
466-
try {
467-
currency = Currency.getInstance(currencyCode);
468-
currencySymbol = currency.getSymbol();
469-
} catch (IllegalArgumentException e) {
470-
}
471-
}
472+
try {
473+
currency = Currency.getInstance(currencyCode);
474+
currencySymbol = currency.getSymbol(locale);
475+
} catch (IllegalArgumentException _) {} // Simply ignore if not valid
472476
}
473477

474478
/**
475-
* Gets the currency of these DecimalFormatSymbols. May be null if the
476-
* currency symbol attribute was previously set to a value that's not
477-
* a valid ISO 4217 currency code.
478-
*
479-
* @return the currency used, or null
479+
* {@return the {@code Currency} of this {@code DecimalFormatSymbols}}
480480
* @since 1.4
481481
*/
482482
public Currency getCurrency() {
@@ -485,7 +485,7 @@ public Currency getCurrency() {
485485
}
486486

487487
/**
488-
* Sets the currency of these DecimalFormatSymbols.
488+
* Sets the currency of this {@code DecimalFormatSymbols}.
489489
* This also sets the currency symbol attribute to the currency's symbol
490490
* in the DecimalFormatSymbols' locale, and the international currency
491491
* symbol attribute to the currency's ISO 4217 currency code.
@@ -497,9 +497,7 @@ public Currency getCurrency() {
497497
* @see #setInternationalCurrencySymbol
498498
*/
499499
public void setCurrency(Currency currency) {
500-
if (currency == null) {
501-
throw new NullPointerException();
502-
}
500+
Objects.requireNonNull(currency);
503501
initializeCurrency(locale);
504502
hashCode = 0;
505503
this.currency = currency;
@@ -555,9 +553,7 @@ public String getExponentSeparator()
555553
*/
556554
public void setExponentSeparator(String exp)
557555
{
558-
if (exp == null) {
559-
throw new NullPointerException();
560-
}
556+
Objects.requireNonNull(exp);
561557
hashCode = 0;
562558
exponentialSeparator = exp;
563559
}
@@ -767,7 +763,8 @@ public boolean equals(Object obj) {
767763
patternSeparator == other.patternSeparator &&
768764
infinity.equals(other.infinity) &&
769765
NaN.equals(other.NaN) &&
770-
getCurrencySymbol().equals(other.getCurrencySymbol()) && // possible currency init occurs here
766+
// Currency fields are lazy. Init via get call to ensure non-null
767+
getCurrencySymbol().equals(other.getCurrencySymbol()) &&
771768
intlCurrencySymbol.equals(other.intlCurrencySymbol) &&
772769
currency == other.currency &&
773770
monetarySeparator == other.monetarySeparator &&
@@ -800,7 +797,8 @@ public int hashCode() {
800797
patternSeparator,
801798
infinity,
802799
NaN,
803-
getCurrencySymbol(), // possible currency init occurs here
800+
// Currency fields are lazy. Init via get call to ensure non-null
801+
getCurrencySymbol(),
804802
intlCurrencySymbol,
805803
currency,
806804
monetarySeparator,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8341445
27+
* @summary DFS setters should throw NPE. This ensures that NPE is not thrown
28+
* by equals().
29+
* @run junit SettersShouldThrowNPETest
30+
*/
31+
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.MethodSource;
35+
36+
import java.lang.reflect.InvocationTargetException;
37+
import java.lang.reflect.Method;
38+
import java.lang.reflect.Modifier;
39+
import java.text.DecimalFormatSymbols;
40+
import java.util.Arrays;
41+
import java.util.Currency;
42+
import java.util.List;
43+
import java.util.Locale;
44+
import java.util.stream.Stream;
45+
46+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
48+
import static org.junit.jupiter.api.Assertions.assertNotNull;
49+
import static org.junit.jupiter.api.Assertions.assertThrows;
50+
51+
public class SettersShouldThrowNPETest {
52+
53+
// The public setter methods that should throw NPE
54+
private static final List<Method> NPE_SETTERS =
55+
Arrays.stream(DecimalFormatSymbols.class.getDeclaredMethods())
56+
.filter(m -> Modifier.isPublic(m.getModifiers())
57+
&& m.getName().startsWith("set")
58+
&& Stream.of(m.getParameterTypes()).noneMatch(Class::isPrimitive))
59+
.toList();
60+
61+
// Non-primitive setters should throw NPE
62+
@ParameterizedTest
63+
@MethodSource("setters")
64+
public void settersThrowNPETest(Method m) {
65+
var dfs = new DecimalFormatSymbols();
66+
InvocationTargetException e =
67+
assertThrows(InvocationTargetException.class, () -> m.invoke(dfs, (Object) null));
68+
if (!(e.getCause() instanceof NullPointerException)) {
69+
throw new RuntimeException(e.getCause() + " was thrown instead of NPE by : " + m);
70+
}
71+
}
72+
73+
// Currency fields are lazy and can be null
74+
// Ensure when exposed to users, they are never null
75+
@ParameterizedTest
76+
@MethodSource("locales")
77+
public void lazyCurrencyFieldsTest(Locale locale) {
78+
var dfs = new DecimalFormatSymbols(locale);
79+
assertDoesNotThrow(() -> dfs.equals(new DecimalFormatSymbols()));
80+
assertNotNull(dfs.getCurrency());
81+
assertNotNull(dfs.getInternationalCurrencySymbol());
82+
assertNotNull(dfs.getCurrencySymbol());
83+
}
84+
85+
// Prior to 8341445, if the international currency symbol was invalid,
86+
// the currency attribute was set to null. However, we should not have null
87+
// currency fields post initializeCurrency() call. Ensure invalid code
88+
// does not update the other fields.
89+
@Test
90+
public void setInternationalCurrencySymbolFallbackTest() {
91+
var code = "fooBarBazQux";
92+
// initialize() should provide null for all currency related fields
93+
var dfs = new DecimalFormatSymbols(Locale.ROOT);
94+
// Load the fallbacks via initCurrency() since the loc is Locale.ROOT
95+
dfs.setInternationalCurrencySymbol(code); // set invalid code
96+
// Ensure our values are the expected fallbacks, minus the updated intl code
97+
assertEquals(Currency.getInstance("XXX"), dfs.getCurrency());
98+
assertEquals("\u00A4", dfs.getCurrencySymbol());
99+
assertEquals("fooBarBazQux", dfs.getInternationalCurrencySymbol());
100+
}
101+
102+
private static List<Method> setters() {
103+
return NPE_SETTERS;
104+
}
105+
106+
private static List<Locale> locales() {
107+
return List.of(Locale.ROOT, Locale.US, Locale.forLanguageTag("XXX"));
108+
}
109+
}

0 commit comments

Comments
 (0)