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

Unchecked cast of arguments for MessageFormat

XMLWordPrintable

    • x86_64
    • os_x

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

      ADDITIONAL OS VERSION INFORMATION :
      Darwin mac.local 17.3.0 Darwin Kernel Version 17.3.0: Thu Nov 9 18:09:22 PST 2017; root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      In method

      java.text.MessageFormat#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)

      is unchecked cast from Object to Object[]. in description is described object array, but for example JavaFX is using it wrong and putting here single value as a String in string Bidirectional binding of property.

      Simple solution is check given object is array and if not wrap it to array.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
          public static void main(String[] args) {

              String res = "";
              StringBuffer stringBuffer = new StringBuffer(res);
              StringBuffer format = new MessageFormat("DAY {0}").format("1", stringBuffer, new FieldPosition(0));

              System.out.println(format);
          }

      ACTUAL -
      Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
      at java.text.MessageFormat.format(MessageFormat.java:865)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
      at java.text.MessageFormat.format(MessageFormat.java:865)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
          public static void main(String[] args) {

              String res = "";
              StringBuffer stringBuffer = new StringBuffer(res);
              StringBuffer format = new MessageFormat("DAY {0}").format("1", stringBuffer, new FieldPosition(0));

              System.out.println(format);
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use different method, but this method is used by JavaFX :(

            rgoel Rachna Goel (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: