United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4990596 REGRESSION: DecimalFormat does not support subclasses
4990596 : REGRESSION: DecimalFormat does not support subclasses

Details
Type:
Bug
Submit Date:
2004-02-07
Status:
Resolved
Updated Date:
2004-02-20
Project Name:
JDK
Resolved Date:
2004-02-20
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.text
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0

Related Reports

Sub Tasks

Description

Name: gm110360			Date: 02/06/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Class java.text.DecimalFormat does not support formatting subclasses of java.lang.Number that you have defined yourself.

If you create a subclass of java.lang.Number  and use it in to format a MessageFormat with a DecimalFormat field, the exception java.lang.IllegalArgumentException: Cannot format given Object as a Number will be thrown.

The problem seems to be located in the method DecimalFormat.format(Object,StringBuffer,FieldPosition).

The api doc of this method clearly states that it "Formats a number (...)"  and that "The number can be of any subclass of Number."

This bug means that quite some code we use does not work with JRE1.5.0beta1, as we apply our own Number subclass for various formatting purposes.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The program provided in the test case section defines class MutableInteger as a subclass of class java.lang.Number

An instance this class is used to format  a message containing a number field: new MessageFormat("{0,number}").   (MessageFormat apparently uses a DecimalFormat instance to format this field)

Basically, the program tests the following:
Object[] formatArgs = new Object[]{ new MutableInteger() };
new MessageFormat("{0,number}").format(formatArgs);


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The Number subclass instance correctly formatted.
ACTUAL -
The exception java.lang.IllegalArgumentException: Cannot format given Object as a Number

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.IllegalArgumentException: Cannot format given Object as a Number
	at java.text.DecimalFormat.format(DecimalFormat.java:480)
	at java.text.Format.format(Format.java:133)
	at java.text.MessageFormat.subformat(MessageFormat.java:1279)
	at java.text.MessageFormat.format(MessageFormat.java:828)
	at java.text.Format.format(Format.java:133)
	at DecimalFormatTest.main(DecimalFormatTest.java:33)
Exception in thread "main"

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.MessageFormat;

public class DecimalFormatTest {

	public static class MutableInteger extends Number {
		public int value;

		public MutableInteger() {
		}
		public MutableInteger(int value) {
			this.value = value;
		}
		public double doubleValue() {
			return this.value;
		}
		public float floatValue() {
			return this.value;
		}
		public int intValue() {
			return this.value;
		}
		public long longValue() {
			return this.value;
		}
	}
	
	public static void main(String[] args) {
		MessageFormat formatter = new MessageFormat("{0,number}");
		MutableInteger mi = new MutableInteger();
		Object[] formatArgs = new Object[1];
		formatArgs[0] = mi;
		for (mi.value = 1; mi.value <= 10; mi.value++) {
			String line = formatter.format(formatArgs);
			System.out.println(line);
		}
	}
}

---------- END SOURCE ----------

Release Regression From : 1.4.2_03
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Incident Review ID: 237530) 
======================================================================

                                    

Comments
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger-beta2

FIXED IN:
tiger-beta2

INTEGRATED IN:
tiger-b40
tiger-beta2


                                     
2004-06-14
EVALUATION



Name: nl37777			Date: 02/06/2004

Need to fix in Tiger.
======================================================================


In the current implementation, DecimalFormat.format() accepts only known
subclasses of Number(BigDecimal, BigInteger, Byte, Double, Float, Integer, 
Long, Short) as its argument. Any subclass of Number should be accepted.
Will be fixed in Tiger.
###@###.### 2004-02-12
                                     
2004-02-12



Hardware and Software, Engineered to Work Together