RFR: 8274640: Cleanup unnecessary null comparison before instanceof check in java.desktop

Alexey Ivanov aivanov at openjdk.java.net
Sat Nov 27 18:54:05 UTC 2021


On Sat, 11 Sep 2021 14:59:21 GMT, Andrey Turbanov <duke at openjdk.java.net> wrote:

> Updated code checks both non-null and instance of a class in java.desktop module classes.
> The checks and explicit casts could also be replaced with pattern matching for the instanceof operator. 
> Similar cleanups
> 1. [JDK-8273484](https://bugs.openjdk.java.net/browse/JDK-8273484) java.naming
> 2. [JDK-8258422](https://bugs.openjdk.java.net/browse/JDK-8258422) java.base

src/java.desktop/macosx/classes/com/apple/laf/AquaRootPaneUI.java line 78:

> 76:         final Component parent = c.getParent();
> 77: 
> 78:         if (parent instanceof JFrame frameParent) {

The `frameParent` variable was declared `final` before.
Suggestion:

        if (parent instanceof final JFrame frameParent) {

src/java.desktop/macosx/classes/com/apple/laf/AquaTableHeaderUI.java line 148:

> 146:     protected static TableColumn getTableColumn(final JTableHeader target, final Object value) {
> 147:         if (!(value instanceof Integer idx)) return null;
> 148:         final int columnIndex = idx;

Probably, this expression could be simplified?
Suggestion:

        if (!(value instanceof final Integer columnIndex)) return null;

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java line 247:

> 245:         }},
> 246:         new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) {
> 247:             if (!(value instanceof java.io.File f)) {

Is `file` a better name?

src/java.desktop/share/classes/javax/swing/JOptionPane.java line 2336:

> 2334:         if (icon instanceof Serializable ser) {
> 2335:             values.addElement("icon");
> 2336:             values.addElement(ser);

I suggest omitting `ser` variable declaration: it's not used as `Serializable`, checking with `instanceof` is enough. This comment applies to all similar if-conditions below.

src/java.desktop/share/classes/javax/swing/JPopupMenu.java line 1338:

> 1336:         }
> 1337:         // Save the popup, if it's Serializable.
> 1338:         if (popup instanceof Serializable ser) {

Suggestion:

        if (popup instanceof Serializable) {

src/java.desktop/share/classes/javax/swing/JTree.java line 3132:

> 3130:         s.defaultWriteObject();
> 3131:         // Save the cellRenderer, if it's Serializable.
> 3132:         if (cellRenderer instanceof Serializable ser) {

Suggestion:

        if (cellRenderer instanceof Serializable) {

src/java.desktop/share/classes/javax/swing/tree/DefaultMutableTreeNode.java line 1301:

> 1299:         s.defaultWriteObject();
> 1300:         // Save the userObject, if it's Serializable.
> 1301:         if (userObject instanceof Serializable ser) {

Suggestion:

        if (userObject instanceof Serializable) {

src/java.desktop/share/classes/javax/swing/tree/DefaultTreeModel.java line 694:

> 692:         s.defaultWriteObject();
> 693:         // Save the root, if it's Serializable.
> 694:         if (root instanceof Serializable ser) {

Suggestion:

        if (root instanceof Serializable) {

src/java.desktop/share/classes/javax/swing/tree/DefaultTreeSelectionModel.java line 1221:

> 1219:         s.defaultWriteObject();
> 1220:         // Save the rowMapper, if it implements Serializable
> 1221:         if (rowMapper instanceof Serializable ser) {

Suggestion:

        if (rowMapper instanceof Serializable) {

src/java.desktop/unix/classes/sun/awt/X11/XWindow.java line 312:

> 310:         }
> 311: 
> 312:         return window.getContentWindow();

Is the branch where 0 was returned impossible?

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

PR: https://git.openjdk.java.net/jdk/pull/5482



More information about the client-libs-dev mailing list