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

JCK: Font(Map)/getFont(Map): unexpected exception for attributes of no proper type

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • solaris
    • client-libs
    • None
    • 2d
    • b33
    • generic
    • generic
    • Verified

      The spec of TextAttribute states: "If the value is null or not of the proper type then it has the default effect."
      However, trying to instantiate Font with Map containing attribute with value of unproper type throws exception. See code below for problem illustration.
      ===================== Test65.java ==========================
      import java.awt.*;
      import java.util.*;
      import java.awt.font.*;

      public class Test65 {
          public static void main(String[] args) {
              Object o1 = TextAttribute.FAMILY;
              Object o2 = Color.black;
       
              System.out.println("Trying to instantiate font with Map " +
                          "containing incorrect attribute: "
                          + o1 + " = " + o2);

              Map hash = new HashMap(1);
              hash.put(o1, o2);

              Font font = new Font(hash);

              System.out.println("Trying to call getFont(Map) with Map " +
                          "containing incorrect attribute: "
                          + o1 + " = " + o2);

              font = Font.getFont(hash);
              System.out.println("Done");

          }
      }
      ================= output =======================
      > java -version
      java version "1.6.0-ea"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b11)
      Java HotSpot(TM) Server VM (build 1.6.0-ea-b11, mixed mode)

      > java -cp ~/work/test/ Test65
      Trying to instantiate font with Map containing incorrect attribute: java.awt.font.TextAttribute(family) = java.awt.Color[r=0,g=0,b=0]
      Exception in thread "main" java.lang.ClassCastException: java.awt.Color cannot be cast to java.lang.String
              at java.awt.Font.initFromMap(Font.java:546)
              at java.awt.Font.<init>(Font.java:531)
              at Test65.main(Test65.java:17)

      ###@###.### 2004-11-18 10:56:27 GMT

            dougfelt Doug Felt (Inactive)
            mchernyssunw Michael Chernyshov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: