Behavior change in exceptions when setting AWT focus traversal keys

Java 8 introduces a behavior change for the runtime exceptions that are thrown by the java.awt.Component.setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes) and the java.awt.KeyboardFocusManager.setDefaultFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes) methods when the keystrokes argument contains objects that are not of type AWTKeyStroke. In Java 8, the methods throw a ClassCastException instead of IllegalArgumentException for this error.

This rule flags calls to the setFocusTraversalKeys and setDefaultFocusTraversalKeys methods when the method call is surrounded by a try statement with a catch clause for IllegalArgumentException.

The following example shows a setFocusTraversalKeys method that is flagged.

public static void testSetFocusTraversal(Component comp, int id, Set keys) {

        try {
            comp.setFocusTraversalKeys(id, keys);
        } catch(IllegalArgumentException iae) {
            ...
        }
    }

The setFocusTraversalKeys and setDefaultFocusTraversalKeys methods still throw IllegalArgumentException for other reasons as described in the Java documentation. Evaluate the logic in your catch block to see if you need to also handle ClassCastException or a more general runtime exception type.

For more information on these classes, see the following resources: