JDK-8283813 : Add final or sealed modifier to appropriate java.awt API classes
  • Type: CSR
  • Component: client-libs
  • Sub-Component: java.awt
  • Priority: P4
  • Status: Draft
  • Resolution: Unresolved
  • Fix Versions: 19
  • Submitted: 2022-03-29
  • Updated: 2022-03-29
Related Reports
CSR :  
Description
Summary
-------

Make several AWT API classes sealed or final.

Problem
-------

There are classes in the AWT API which have a known set of direct sub-classes, or no sub-classes. These should have the sealed or final modifier applied to indicate this.

Solution
--------

Apply the sealed modifier from JEP 409 ( https://openjdk.java.net/jeps/409 )  to explicitly document and limit the direct API sub-classes of existing API classes which have no public or protected constructors - and so can be compatibly updated in this way. Similarly identified classes which have no sub-classes should be marked final. 

Sub-classes of newly sealed classes are made non-sealed as needed, or final if they are not already - if that is a compatible change.

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

java.awt.GridBagLayoutInfo

<pre>
-public class GridBagLayoutInfo implements java.io.Serializable
+public final class GridBagLayoutInfo implements java.io.Serializable
</pre>

--------------

java.awt.PointerInfo

<pre>
-public class PointerInfo 
+public final class PointerInfo 
</pre>

--------------

java.awt. ScrollPaneAdjustable

<pre>
-public class ScrollPaneAdjustable implements Adjustable, Serializable 
+public final class ScrollPaneAdjustable implements Adjustable, Serializable 
</pre>

-------
java.awt.dnd.DropTargetContext

<pre>

-public class DropTargetContext implements Serializable 
+public final class DropTargetContext implements Serializable
</pre>

-------------------

java.awt.TextComponent

<pre>
-public class TextComponent extends Component implements Accessible {
+public sealed class TextComponent extends Component implements Accessible
+     permits TextArea,
+             TextField {
</pre>

------

java.awt.TextField

<pre>
-public class TextField extends TextComponent 
+public non-sealed class TextField extends TextComponent 
</pre>

-------
java.awt.TextArea

<pre>
-public class TextArea extends TextComponent 
+public non-sealed class TextArea extends TextComponent 
</pre>

--------

java.awt.InputEvent

<pre>
-public abstract class InputEvent extends ComponentEvent 
+public abstract sealed class InputEvent extends ComponentEvent
+    permits KeyEvent,
+            MouseEvent {
</pre>

---------

java.awt.event.KeyEvent

<pre>
-public class KeyEvent extends InputEvent 
+public non-sealed class KeyEvent extends InputEvent 
 </pre>

------

java.awt.event.MouseEvent

<pre>
-public class MouseEvent extends InputEvent 
+public non-sealed class MouseEvent extends InputEvent 
</pre>


------

java.awt.desktop.AppEvent

<pre>
-public class AppEvent extends EventObject {
+public sealed class AppEvent extends EventObject
+    permits AboutEvent,
+            AppForegroundEvent,
+            AppHiddenEvent,
+            AppReopenedEvent,
+            FilesEvent,
+            OpenURIEvent,
+            PreferencesEvent,
+            QuitEvent,
+            ScreenSleepEvent,
+            SystemSleepEvent,
+            UserSessionEvent {
</pre>


--------


java.awt.desktop.FilesEvent

<pre>
-public class FilesEvent extends AppEvent {
+public sealed class FilesEvent extends AppEvent
+    permits OpenFilesEvent,
+            PrintFilesEvent {
</pre>

---------

Comments
>>The compatibility risk is both source and binary since as well as compile time checking of this, the VM enforces it at runtime. I guess this sentence needs some rephrasing as it not coming out clearly, it seems to me. Maybe "The compatibility risk is in both source and binary as VM will check at compile-time and also at runtime" > Make several AWT API classes sealed or final. I see at least one class is marked non-sealed too.. > of existing API classes which have no public or protected constructors Maybe we should add "and at least one package access constructor" too
29-03-2022