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

MessageFormat.format() method fails to replace placeholders if the pattern used to create the MessageFormat is not escaped

XMLWordPrintable

    • generic
    • generic

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

      ADDITIONAL OS VERSION INFORMATION :
      Linux den00blt 3.8.13-44.1.1.el6uek.x86_64 #2 SMP Wed Sep 10 06:10:25 PDT 2014 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      When creating a MessageFormat using the constructor MessageFormat(String pattern, Locale locale), if the pattern has a single non escaped characters like ' (apostrophe) prior to a placeholder {0} in the pattern, the placeholders are not replaced by calling the messageFormat.format(Object[], StringBuffer, null). But if the non escaped character is after the placeholder, the placeholder would be replaced.

      Example:

      Couldn't play game : {0} --> {0} here would not be replaced with actual content
      whereas
      Playing {0} wasn't possible -> {0} here would be properly replaced with actual content.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      MessageFormat mf = new MessageFormat(pattern, locale);
      mf.format(args, new StringBuffer(1000), null)

      use pattern = Couldn't play game : {0}
      locale = en
      args[0] = "Mario"



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The placeholder {0} would not be replaced by string "mario"
      Couldn't play game : mario
      ACTUAL -
      Couldn't play game : {0}

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public static void main(String args[]) {
              MessageFormat mf = new MessageFormat("Couldn't play game : {0}", Locale.ENGLISH);
              String[] ar = new String[1];
              ar[0] = "mario";
              System.out.print("Final string " + mf.format(ar, new StringBuffer(1000), null).toString());
          }
      ---------- END SOURCE ----------

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: