JDK-6195828 : Component.setFocusTraversalKeys(..) 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-21
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 :  
Relates :  
Description
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

Comments
SUGGESTED FIX *** /export1/vb157120/mustang1//webrev/src/share/classes/java/awt/Component.java- Mon Feb 7 17:35:35 2005 --- /export1/vb157120/mustang1//webrev/src/share/classes/java/awt/Component.java Mon Feb 7 17:35:34 2005 *************** *** 6256,6261 **** --- 6256,6265 ---- throw new IllegalArgumentException("cannot set null focus traversal key"); } + // Fix for 6195828: Component.setFocusTraversalKeys(..) throws ClassCastException instead of IAE + if (!(obj instanceof AWTKeyStroke)) { + throw new IllegalArgumentException("object is expected to be AWTKeyStroke"); + } // Generates a ClassCastException if the element is not an // AWTKeyStroke. This is desirable. AWTKeyStroke keystroke = (AWTKeyStroke)obj; ###@###.### 2005-2-09 17:23:28 GMT
09-02-2005

EVALUATION Not really sure about line with "desirable ClassCastException" in Component.setFocusTraversalKeys_NoIDCheck(int id, Set keystrokes) since there are no other usages of this method besides Component and Container. So, suppose that the simplest way of the fix is to throw IAE instead of ClassCastException. ###@###.### 2004-11-16 13:22:53 GMT
16-11-2004