Name: nt126004 Date: 09/09/2002
FULL PRODUCT VERSION :
java version "1.4.1-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)
FULL OPERATING SYSTEM VERSION : Windows 2000, SP3
A DESCRIPTION OF THE PROBLEM :
In the java.text.MessageFormat class there is a version of
format() with this signature:
format(Object arguments, StringBuffer result, FieldPosition
pos) // form 1
There is also a second format() method with a very similar
signature:
format(Object[] arguments, StringBuffer result,
FieldPosition pos) // form 2
If you look at the source code for "Form 1" you will see that it is *logically impossible* to call this method successfully. Whoever wrote the MessageFormat class made a mistake in writing this method.
The first format() method has a single statement in its
body (again, look at the source code if you like):
return subformat((Object[]) arguments, result, pos, null);
The return statement will *NEVER, EVER, EVER* be executed --
it is 100% impossible. It tries to cast the 'arguments' parameter to an Object[]. Well that's fine except that form 2 of the format() method (shown above) *already uses* a Object[] as its first parameter. If a client of this method passes an Object[] as the first argument it is the second form of the format() method that gets called -- the one that takes an Object[] parameter -- not the first form.
The only time the first form gets called is when a non- array Object is passed. But then the method tries to cast that Object to an Object[] and boom! ClassCastException!
I originally submitted this bug 1.5 years ago. It was given a review ID of 4407453. The reviewer said it was a duplicate of 4228682, which I agree with. The evaluator's assessment of 4228682 is sloppy, I think. I don't think the
reviewer understood that the first form of format() will never execute correctly.
The first form of format() is essentially non-functioning at this point -- it's 100% brain dead.
A simple fix is all that's need though -- change the method's implementation to:
return subformat(new Object[] {arguments}, result, pos, null);
Please look at this carefully. It's important to understand that the existing implementation of the first format() method does absolutely nothing -- except throw ClassCastExceptions. Currently, it does not call subformat().
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.text.*;
public class Tester {
public static void main(String[] args) {
MessageFormat mf = new MessageFormat("Hi {0}, nice to see you.");
String result1 = mf.format("Skippy"); // ClassCastException!!
String result2 = mf.format(new String[] {"Skippy"}); // A-OK!!
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
When you call format(), make sure the Object parameter is
really an Object[].
(Review ID: 163722)
======================================================================
- duplicates
-
JDK-4228682 MessageFormat format(Object, StringBuffer, FieldPosition) doesn't work
-
- Closed
-