JDK-8251211 : Address reliance on default constructors in the AWT APIs
  • Type: CSR
  • Component: client-libs
  • Sub-Component: java.awt
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 16
  • Submitted: 2020-08-06
  • Updated: 2020-08-14
  • Resolved: 2020-08-10
Related Reports
CSR :  
Description
Summary
-------

Add explicit constructors to some classes in Java AWT APIs which using default constructors.

Problem
-------

Default constructors are not recommended for formal API classes.

Solution
--------

Add the public constructors to the public/non-abstract classes, and protected constructors to the public/abstract classes.

Specification
-------------

    src/java.desktop/share/classes/java/awt/ContainerOrderFocusTraversalPolicy.java
 
    +    /**
    +     * Constructs a {@code ContainerOrderFocusTraversalPolicy}.
    +     */
    +    public ContainerOrderFocusTraversalPolicy() {}

    src/java.desktop/share/classes/java/awt/DefaultFocusTraversalPolicy.java
 
    +    /**
    +     * Constructs a {@code DefaultFocusTraversalPolicy}.
    +     */
    +    public DefaultFocusTraversalPolicy() {}

    src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java
 
    +    /**
    +     * Constructs a {@code DefaultKeyboardFocusManager}.
    +     */
    +    public DefaultKeyboardFocusManager() {}

    src/java.desktop/share/classes/java/awt/FocusTraversalPolicy.java
     public abstract class FocusTraversalPolicy {
 
    +    /**
    +     * Constructs a {@code FocusTraversalPolicy}.
    +     */
    +    protected FocusTraversalPolicy() {}

    src/java.desktop/share/classes/java/awt/Toolkit.java
     public abstract class Toolkit {
 
    +    /**
    +     * Constructs a {@code Toolkit}.
    +     */
    +    protected Toolkit() {}

    src/java.desktop/share/classes/java/awt/dnd/DragSourceAdapter.java
         implements DragSourceListener, DragSourceMotionListener {
 
    +    /**
    +     * Constructs a {@code DragSourceAdapter}.
    +     */
    +    protected DragSourceAdapter() {}

    src/java.desktop/share/classes/java/awt/dnd/DropTargetAdapter.java
     public abstract class DropTargetAdapter implements DropTargetListener {
 
    +    /**
    +     * Constructs a {@code DropTargetAdapter}.
    +     */
    +    protected DropTargetAdapter() {}

    src/java.desktop/share/classes/java/awt/event/ComponentAdapter.java
     public abstract class ComponentAdapter implements ComponentListener {
    +
    +    /**
    +     * Constructs a {@code ComponentAdapter}.
    +     */
    +    protected ComponentAdapter() {}

    src/java.desktop/share/classes/java/awt/event/ContainerAdapter.java
     public abstract class ContainerAdapter implements ContainerListener {
    +
    +    /**
    +     * Constructs a {@code ContainerAdapter}.
    +     */
    +    protected ContainerAdapter() {}

    src/java.desktop/share/classes/java/awt/event/FocusAdapter.java
     public abstract class FocusAdapter implements FocusListener {
    +
    +    /**
    +     * Constructs a {@code FocusAdapter}.
    +     */
    +    protected FocusAdapter() {}

    src/java.desktop/share/classes/java/awt/event/HierarchyBoundsAdapter.java
     public abstract class HierarchyBoundsAdapter implements HierarchyBoundsListener
     {
    +    /**
    +     * Constructs a {@code HierarchyBoundsAdapter}.
    +     */
    +    protected HierarchyBoundsAdapter() {}

    src/java.desktop/share/classes/java/awt/event/KeyAdapter.java
     public abstract class KeyAdapter implements KeyListener {
    +
    +    /**
    +     * Constructs a {@code KeyAdapter}.
    +     */
    +    protected KeyAdapter() {}

    src/java.desktop/share/classes/java/awt/event/MouseAdapter.java
     public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener {
    +
    +    /**
    +     * Constructs a {@code MouseAdapter}.
    +     */
    +    protected MouseAdapter() {}

    src/java.desktop/share/classes/java/awt/event/MouseMotionAdapter.java
     public abstract class MouseMotionAdapter implements MouseMotionListener {
    +
    +    /**
    +     * Constructs a {@code MouseMotionAdapter}.
    +     */
    +    protected MouseMotionAdapter() {}

    src/java.desktop/share/classes/java/awt/event/WindowAdapter.java
         public abstract class WindowAdapter implements WindowListener, WindowStateListener, WindowFocusListener
     {
    +    /**
    +     * Constructs a {@code WindowAdapter}.
    +     */
    +    protected WindowAdapter() {}

Link for convenience: http://cr.openjdk.java.net/~serb/8250856/webrev.00


Comments
As far as I know, adding a protected rather than a public constructor should be compatible enough in all the ways we care about (source, binary, behavioral, etc.). It is of course a signature change that will require a JCK update as any signature change would. If there are more edge cases where a protected vs public constructor is problematic, there is sufficient time in the release to flush them out. Note that in other area we have used the convention for constructors on abstract class having the spec "Constructor for subclasses to call."; however this is not mandatory. Moving to Approved.
10-08-2020

Will the change from public to protected constructors in the abstract classes cause any problems with JCK tests? I realize that they are equivalent for abstract classes, but depending on whether / how the JCK checks for unexpected API changes, I can see it causing a problem (or not).
06-08-2020