JDK-7081794 : java.util.Formatter fails to format numbers with legal pattern '%1$#8s'
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2011-08-22
  • Updated: 2012-03-20
  • Resolved: 2011-08-26
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
JDK 1.7.0 class "java.util.Formatter" fails to format a number according to the pattern '%1$#8s'.

The test case below illustrates the issue.
The test succeeds on JDK 1.6.20.

REGRESSION.  Last worked in version 6u26

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A formatted string, see test.
ACTUAL -
A FormatFlagsConversionException


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = #
	at java.util.Formatter$FormatSpecifier.failMismatch(Formatter.java:4041)
	at java.util.Formatter$FormatSpecifier.printString(Formatter.java:2834)
	at java.util.Formatter$FormatSpecifier.print(Formatter.java:2718)
	at java.util.Formatter.format(Formatter.java:2494)
	at java.util.Formatter.format(Formatter.java:2423)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import org.junit.Assert;
import org.junit.Test;

import java.util.Formatter;

import static org.hamcrest.CoreMatchers.*;

public class FormatTest {

    //This test is green in JDK 1.6.20, red in JDK 1.7.0
    @Test
    public void formatterDoesNotFormatNumbers() throws Exception {
        String result = new Formatter().format("%1$#8s", 15).toString();
        Assert.assertThat(result, is("      15"));
    }
}
---------- END SOURCE ----------
The related spec in j.u.Formatter API states

--------------------------------------------------------------------------
's' 	'\u0073' 	Produces a string.

If the argument is null, then the result is "null". If the argument implements Formattable, then its formatTo method is invoked. Otherwise, the result is obtained by invoking the argument's toString() method.

If the '#' flag is given and the argument is not a Formattable , then a FormatFlagsConversionMismatchException will be thrown. 
---------------------------------------------------------------------------

However this specification was not inforced by our  j.u.Formatter implementation until the #5063507 was fixed in JDK7. #5063507 is an impatible change, and we had a CCC for it.

Closed as "not a defect".