Name: yyT116575 Date: 01/23/2001
java version "1.3.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_01)
Java HotSpot(TM) Client VM (build 1.3.0_01, mixed mode)
I have a sample program below to illustrate the problem but first let me
describe the issue.
The signature for "MessageFormat.format(Object obj, StringBuffer sb,
FieldPosition fp)" indicates the first parameter is an Object. The javadocs,
however, indicate this parameter must be restricted to an Object[]. The
method attempts to cast the first parameter to an Object[]. Of course, if the
parameter is not an Object[], a ClassCastException results.
Why is this such a dumb problem? Because the MessageFormat class ALREADY HAS a
method whose signature is "MessageFormat.format(Object[] obj, StringBuffer sb,
FieldPosition fp)". In other words, MessageFormat has two methods with these
signatures:
#1: MessageFormat.format(Object obj, StringBuffer sb, FieldPosition fp)
#2: MessageFormat.format(Object[] obj, StringBuffer sb, FieldPosition fp)
When I call #1, the obj parameter must be an array, right? If not, I get a
ClassCastException. Well, if the obj parameter is an array, the compiler would
*automatically* call #2! In other words, it is absolutely impossible to call #1
without getting a ClassCastException.
-- Sample Program Below to Illustrate Problem --
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!!
}
}
(Review ID: 115634)
======================================================================
- duplicates
-
JDK-4228682 MessageFormat format(Object, StringBuffer, FieldPosition) doesn't work
-
- Closed
-