Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8042551

java.text.MessageFormat selects wrong format from the array of formats

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8
    • core-libs
    • x86
    • windows_8

      FULL PRODUCT VERSION :
      java version "1.8.0_05"
      Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      java.text.MessageFormat selects wrong format from the array of formats, which can result in an exception being thrown. Please see "Source code for an executable test case" below. The code + actual/expected result show the issue clearly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please see "Source code for an executable test case" below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      There are no files on C:\.
      There is one file on C:\.
      There are 2 files on C:\.
      ACTUAL -
      Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
      at java.text.DecimalFormat.format(DecimalFormat.java:507)
      at java.text.Format.format(Format.java:157)
      at java.text.MessageFormat.subformat(MessageFormat.java:1322)
      at java.text.MessageFormat.format(MessageFormat.java:865)
      at java.text.Format.format(Format.java:157)
      at bugreports.MessageFormatBug.main(MessageFormatBug.java:26)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.*;
      import java.text.*;

      public class MessageFormatBug {

      public static void main(String[] args) {
      MessageFormat messageFmt = new MessageFormat("");
      messageFmt.setLocale(Locale.ENGLISH);
      messageFmt.applyPattern("There {0} on {2}.");

      double[] fileLimits = {0, 1, 2};
      String[] fileStrings = { "are no files", "is one file", "are {1} files" };
      ChoiceFormat choiceFmt = new ChoiceFormat(fileLimits, fileStrings);

      Format[] formats = {choiceFmt, NumberFormat.getInstance(), null};

      messageFmt.setFormats(formats);

      Object[] messageArguments = {null, null, "C:\\"};

      for (int nbFiles = 0; nbFiles < 3; nbFiles++) {
      messageArguments[0] = nbFiles;
      messageArguments[1] = nbFiles;
      System.out.println(messageFmt.format(messageArguments));
      }
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In java.text.MessageFormat ( this version: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/4bb758a77fd7/src/share/classes/java/text/MessageFormat.java#l1259 ), changing formats[i] to formats[argumentNumber] on lines 1259, 1260, 1262 solves the problem & correctly prints the expected result.

            nishjain Nishit Jain
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: