-
Bug
-
Resolution: Fixed
-
P4
-
8, 9
-
b36
-
x86_64
-
linux
-
Verified
FULL PRODUCT VERSION :
A DESCRIPTION OF THE PROBLEM :
When ICU4J Locale SP module is used, simple Scanner use as below throws ClassCastException.
Code snippet
=====
System.out.print("Enter an integer: ");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
System.out.println("You entered: " + number);
=====
Exception stack trace
=====
Exception in thread "main" java.lang.ClassCastException: com.ibm.icu.impl.jdkadapter.NumberFormatICU cannot be cast to java.text.DecimalFormat
at java.util.Scanner.useLocale(Scanner.java:1186)
at java.util.Scanner.<init>(Scanner.java:540)
at java.util.Scanner.<init>(Scanner.java:563)
at test.ScannerTest.main(ScannerTest.java:11)
=====
com.ibm.icu.impl.jskadapter.NumberFormatICU implements java.text.spi.NumberFormatProvider. The method, getNumberInstance(Locale) returns an instance of com.ibm.icu.impl.jdkadapter.NumberFormatICU that extends java.text.NumberFormat.
In java.util.Scanner, NumberFormat object returned by NumberFormat#getNumberInstance(Locale) is casted to java.text.DecimalFormat without any type check. Because NumberFormatICU is not a subclass of DecimalFormat, it triggers ClassCastException.
Below is the site throwing ClassCastException.
=====
public Scanner useLocale(Locale locale) {
if (locale.equals(this.locale))
return this;
this.locale = locale;
DecimalFormat df =
(DecimalFormat)NumberFormat.getNumberInstance(locale);
=====
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Download ICU4J core library and locale provider from ICU4J project site -
http://download.icu-project.org/files/icu4j/59.1/icu4j-59_1.jar
http://download.icu-project.org/files/icu4j/59.1/icu4j-localespi-59_1.jar
2) Run above java code snippet with JVM arguments
-Djava.ext.dirs=<directory containing above 2 jar files> -Djava.locale.providers=SPI,JRE
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.ClassCastException: com.ibm.icu.impl.jdkadapter.NumberFormatICU cannot be cast to java.text.DecimalFormat
at java.util.Scanner.useLocale(Scanner.java:1186)
at java.util.Scanner.<init>(Scanner.java:540)
at java.util.Scanner.<init>(Scanner.java:563)
at test.ScannerTest.main(ScannerTest.java:11)
REPRODUCIBILITY :
This bug can be reproduced always.
A DESCRIPTION OF THE PROBLEM :
When ICU4J Locale SP module is used, simple Scanner use as below throws ClassCastException.
Code snippet
=====
System.out.print("Enter an integer: ");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
System.out.println("You entered: " + number);
=====
Exception stack trace
=====
Exception in thread "main" java.lang.ClassCastException: com.ibm.icu.impl.jdkadapter.NumberFormatICU cannot be cast to java.text.DecimalFormat
at java.util.Scanner.useLocale(Scanner.java:1186)
at java.util.Scanner.<init>(Scanner.java:540)
at java.util.Scanner.<init>(Scanner.java:563)
at test.ScannerTest.main(ScannerTest.java:11)
=====
com.ibm.icu.impl.jskadapter.NumberFormatICU implements java.text.spi.NumberFormatProvider. The method, getNumberInstance(Locale) returns an instance of com.ibm.icu.impl.jdkadapter.NumberFormatICU that extends java.text.NumberFormat.
In java.util.Scanner, NumberFormat object returned by NumberFormat#getNumberInstance(Locale) is casted to java.text.DecimalFormat without any type check. Because NumberFormatICU is not a subclass of DecimalFormat, it triggers ClassCastException.
Below is the site throwing ClassCastException.
=====
public Scanner useLocale(Locale locale) {
if (locale.equals(this.locale))
return this;
this.locale = locale;
DecimalFormat df =
(DecimalFormat)NumberFormat.getNumberInstance(locale);
=====
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Download ICU4J core library and locale provider from ICU4J project site -
http://download.icu-project.org/files/icu4j/59.1/icu4j-59_1.jar
http://download.icu-project.org/files/icu4j/59.1/icu4j-localespi-59_1.jar
2) Run above java code snippet with JVM arguments
-Djava.ext.dirs=<directory containing above 2 jar files> -Djava.locale.providers=SPI,JRE
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.ClassCastException: com.ibm.icu.impl.jdkadapter.NumberFormatICU cannot be cast to java.text.DecimalFormat
at java.util.Scanner.useLocale(Scanner.java:1186)
at java.util.Scanner.<init>(Scanner.java:540)
at java.util.Scanner.<init>(Scanner.java:563)
at test.ScannerTest.main(ScannerTest.java:11)
REPRODUCIBILITY :
This bug can be reproduced always.