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

Component.setFocusTraversalKeys(..) throws ClassCastException instead of IAE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 5.0u1
    • client-libs
    • None
    • b27
    • x86
    • windows_2000
    • Verified

      According to the specs J2SE 1.4.2 and 5.0:
      Component.setFocusTraversalKeys(int id, Set keystrokes)
      "Throws: IllegalArgumentException - .. if any Object in
      keystrokes is not an AWTKeyStroke"

      However, the J2SE implementations 1.4.2 and 1.5.0 throw ClassCastException
      instead of expected IllegalArgumentException.

      The code sample below demonstrates the problem:
      -------------- TestFT13.java -----------------
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;

      public class TestFT13 {
          Button button = new Button();

          public static void main(String[] args) {
              new TestFT13().test();
          }

          private void test() {
              AWTKeyStroke key = AWTKeyStroke.getAWTKeyStrokeForEvent(
                  new KeyEvent(button, KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_UNDEFINED, 'a'));
              HashSet set = new HashSet();
              set.add(key);
              set.add(new Object());

              try {
                  button.setFocusTraversalKeys(
                         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);
              } catch(Exception e) {
                  if (e instanceof IllegalArgumentException) {
                      System.out.println("Component: OKAY");
                  } else {
                      e.printStackTrace();
                      System.out.println("Component: unexpected " + e + " thrown instead of IAE");
                  }
              }
          }
      }
      ---------------------------------------------------
      -------------- Output for 1.4.2 -------------------
      e:\c:\jdk1.4.2\bin\java -classpath . TestFT13
      java.lang.ClassCastException
              at java.awt.Component.setFocusTraversalKeys_NoIDCheck(Component.java:5802)
              at java.awt.Component.setFocusTraversalKeys(Component.java:5742)
              at TestFT13.test(TestFT13.java:20)
              at TestFT13.main(TestFT13.java:9)
      Component: unexpected java.lang.ClassCastException thrown instead of IAE
      ---------------------------------------------------

      ###@###.### 2004-11-16 11:00:40 GMT

            vbaranovsunw Vyacheslav Baranov (Inactive)
            visaenko Vasily Isaenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: