JDK-6195831 : KeyboardFocusManager.setDefaultFocusTraversalKeys(..) throws ClassCastException instead of IAE
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0u1
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-11-16
  • Updated: 2012-10-09
  • Resolved: 2005-06-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b27Fixed
Related Reports
Relates :  
Description
According to the specs J2SE 1.4.2 and 5.0:
KeyboardFocusManager.setDefaultFocusTraversalKeys(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:
-------------- TestFT14.java -----------------
import java.awt.*;
import java.util.*;

public class TestFT14 {
    KeyboardFocusManager currentKFM = 
            KeyboardFocusManager.getCurrentKeyboardFocusManager();

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

    private void test() {       
        AWTKeyStroke key = AWTKeyStroke.getAWTKeyStroke("ENTER");
        HashSet set = new HashSet();
        set.add(key);
        set.add(new Object());

        try {
            currentKFM.setDefaultFocusTraversalKeys(
                   KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
        } catch(Exception e) {
            if (e instanceof IllegalArgumentException) {
                System.out.println("KFM: OKAY");
            } else {
                e.printStackTrace();
                System.out.println("KFM: unexpected " + e + " thrown instead of IAE");
            }
        }
    }
}
---------------------------------------------------
-------------- Output for 1.4.2 -------------------
e:\>c:\jdk1.4.2\bin\java -classpath . TestFT14
java.lang.ClassCastException
        at java.awt.KeyboardFocusManager.setDefaultFocusTraversalKeys(KeyboardFocusManager.java:952)
        at TestFT14.test(TestFT14.java:19)
        at TestFT14.main(TestFT14.java:9)
KFM: unexpected java.lang.ClassCastException thrown instead of IAE
---------------------------------------------------

###@###.### 2004-11-16 11:06:39 GMT

Comments
SUGGESTED FIX *** /export1/vb157120/mr/webrev/src/share/classes/java/awt/KeyboardFocusManager.java- Tue Feb 8 17:37:43 2005 --- /export1/vb157120/mr/webrev/src/share/classes/java/awt/KeyboardFocusManager.java Tue Feb 8 17:37:42 2005 *************** *** 1033,1040 **** throw new IllegalArgumentException("cannot set null focus traversal key"); } ! // Generates a ClassCastException if the element is not an ! // AWTKeyStroke. This is desirable. AWTKeyStroke keystroke = (AWTKeyStroke)obj; if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { --- 1033,1043 ---- throw new IllegalArgumentException("cannot set null focus traversal key"); } ! // Fix for 6195831: ! //According to javadoc this method should throw IAE instead of ClassCastException ! if (!(obj instanceof AWTKeyStroke)) { ! throw new IllegalArgumentException("object is expected to be AWTKeyStroke"); ! } AWTKeyStroke keystroke = (AWTKeyStroke)obj; if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { ###@###.### 2005-2-09 17:24:51 GMT
09-02-2005

EVALUATION The same as 6195828. ###@###.### 2004-11-16 13:27:07 GMT
16-11-2004