Skip to content
Permalink
Browse files
8274640: Cleanup unnecessary null comparison before instanceof check …
…in java.desktop

Reviewed-by: aivanov
  • Loading branch information
turbanoff authored and aivanov-jdk committed Dec 3, 2021
1 parent dda8f26 commit 18c54b4e1ade2ea5531054ed19441d5bff84fcdc
Showing 122 changed files with 411 additions and 526 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -128,7 +128,7 @@ protected boolean isFocused(final Component c) {
if (!((javax.swing.text.JTextComponent)focusable).isEditable()) return false;
}

return (focusable != null && focusable instanceof JComponent && ((JComponent)focusable).hasFocus());
return (focusable instanceof JComponent jComponent) && jComponent.hasFocus();
}

@Override
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -145,9 +145,9 @@ public boolean hasSmallerInsets(final JComponent c) {
* @param c the component for which this border insets value applies
*/
public Insets getBorderInsets(final Component c) {
if (c == null || !(c instanceof AbstractButton)) return new Insets(0, 0, 0, 0);
if (!(c instanceof AbstractButton button)) return new Insets(0, 0, 0, 0);

Insets margin = ((AbstractButton)c).getMargin();
Insets margin = button.getMargin();
margin = (margin == null) ? new InsetsUIResource(0, 0, 0, 0) : (Insets)margin.clone();

margin.top += sizeVariant.margins.top;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -598,8 +598,7 @@ public Dimension getMinimumSize(final JComponent c) {
final boolean editable = comboBox.isEditable();

final Dimension size;
if (!editable && arrowButton != null && arrowButton instanceof AquaComboBoxButton) {
final AquaComboBoxButton button = (AquaComboBoxButton)arrowButton;
if (!editable && arrowButton instanceof final AquaComboBoxButton button) {
final Insets buttonInsets = button.getInsets();
// Insets insets = comboBox.getInsets();
final Insets insets = new Insets(0, 5, 0, 25);//comboBox.getInsets();
@@ -519,14 +519,14 @@ public void propertyChange(final PropertyChangeEvent e) {

void setPackageIsTraversable(final Object o) {
int newProp = -1;
if (o != null && o instanceof String) newProp = parseTraversableProperty((String)o);
if (o instanceof String s) newProp = parseTraversableProperty(s);
if (newProp != -1) fPackageIsTraversable = newProp;
else fPackageIsTraversable = sGlobalPackageIsTraversable;
}

void setApplicationIsTraversable(final Object o) {
int newProp = -1;
if (o != null && o instanceof String) newProp = parseTraversableProperty((String)o);
if (o instanceof String s) newProp = parseTraversableProperty(s);
if (newProp != -1) fApplicationIsTraversable = newProp;
else fApplicationIsTraversable = sGlobalApplicationIsTraversable;
}
@@ -1985,11 +1985,11 @@ static int parseTraversableProperty(final String s) {

static {
Object o = UIManager.get(PACKAGE_TRAVERSABLE_PROPERTY);
if (o != null && o instanceof String) sGlobalPackageIsTraversable = parseTraversableProperty((String)o);
if (o instanceof String s) sGlobalPackageIsTraversable = parseTraversableProperty(s);
else sGlobalPackageIsTraversable = kOpenConditional;

o = UIManager.get(APPLICATION_TRAVERSABLE_PROPERTY);
if (o != null && o instanceof String) sGlobalApplicationIsTraversable = parseTraversableProperty((String)o);
if (o instanceof String s) sGlobalApplicationIsTraversable = parseTraversableProperty(s);
else sGlobalApplicationIsTraversable = kOpenConditional;
}
static final String sDataPrefix = "FileChooser.";
@@ -188,8 +188,8 @@ public void uninitialize() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventPostProcessor(AquaMnemonicHandler.getInstance());

final PopupFactory popupFactory = PopupFactory.getSharedInstance();
if (popupFactory != null && popupFactory instanceof ScreenPopupFactory) {
((ScreenPopupFactory)popupFactory).setActive(false);
if (popupFactory instanceof ScreenPopupFactory spf) {
spf.setActive(false);
}

super.uninitialize();
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -75,8 +75,7 @@ public void installUI(final JComponent c) {
// it is not since we are going to grab the one that was set on the JFrame. :(
final Component parent = c.getParent();

if (parent != null && parent instanceof JFrame) {
final JFrame frameParent = (JFrame)parent;
if (parent instanceof JFrame frameParent) {
final Color bg = frameParent.getBackground();
if (bg == null || bg instanceof UIResource) {
frameParent.setBackground(UIManager.getColor("Panel.background"));
@@ -126,8 +125,8 @@ public void componentAdded(final ContainerEvent e) {
final Window owningWindow = SwingUtilities.getWindowAncestor(jmb);

// Could be a JDialog, and may have been added to a JRootPane not yet in a window.
if (owningWindow != null && owningWindow instanceof JFrame) {
((AquaMenuBarUI)mbui).setScreenMenuBar((JFrame)owningWindow);
if (owningWindow instanceof JFrame frame) {
((AquaMenuBarUI)mbui).setScreenMenuBar(frame);
}
}
}
@@ -154,8 +153,8 @@ public void componentRemoved(final ContainerEvent e) {
final Window owningWindow = SwingUtilities.getWindowAncestor(jmb);

// Could be a JDialog, and may have been added to a JRootPane not yet in a window.
if (owningWindow != null && owningWindow instanceof JFrame) {
((AquaMenuBarUI)mbui).clearScreenMenuBar((JFrame)owningWindow);
if (owningWindow instanceof JFrame frame) {
((AquaMenuBarUI)mbui).clearScreenMenuBar(frame);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -266,8 +266,8 @@ protected boolean shouldUseArrowThumb() {
if (slider.getPaintTicks() || slider.getPaintLabels()) return true;

final Object shouldPaintArrowThumbProperty = slider.getClientProperty("Slider.paintThumbArrowShape");
if (shouldPaintArrowThumbProperty != null && shouldPaintArrowThumbProperty instanceof Boolean) {
return ((Boolean)shouldPaintArrowThumbProperty).booleanValue();
if (shouldPaintArrowThumbProperty instanceof Boolean b) {
return b;
}

return false;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -103,9 +103,8 @@ static void tickle(final JTableHeader target, final Object selectedColumn, final
}

final TableHeaderUI headerUI = target.getUI();
if (headerUI == null || !(headerUI instanceof AquaTableHeaderUI)) return;
if (!(headerUI instanceof AquaTableHeaderUI aquaHeaderUI)) return;

final AquaTableHeaderUI aquaHeaderUI = (AquaTableHeaderUI)headerUI;
aquaHeaderUI.sortColumn = tableColumn.getModelIndex();
aquaHeaderUI.sortOrder = sortDirection;
final AquaTableCellRenderer renderer = aquaHeaderUI.new AquaTableCellRenderer();
@@ -145,8 +144,7 @@ public Component getTableCellRendererComponent(final JTable localTable, final Ob
}

protected static TableColumn getTableColumn(final JTableHeader target, final Object value) {
if (value == null || !(value instanceof Integer)) return null;
final int columnIndex = ((Integer)value).intValue();
if (!(value instanceof Integer columnIndex)) return null;

final TableColumnModel columnModel = target.getColumnModel();
if (columnIndex < 0 || columnIndex >= columnModel.getColumnCount()) return null;
@@ -351,11 +351,8 @@ private void setVisible(final boolean b) {
// Tell our parent to add/remove us
final MenuContainer parent = getParent();

if (parent != null) {
if (parent instanceof ScreenMenu) {
final ScreenMenu sm = (ScreenMenu)parent;
sm.setChildVisible(fInvoker, b);
}
if (parent instanceof ScreenMenu sm) {
sm.setChildVisible(fInvoker, b);
}
}

@@ -283,9 +283,9 @@ protected boolean cloneStyledFont(FontFamily realFamily, String logicalFamilyNam
if (realFamily == null) return false;

Font2D realFont = realFamily.getFontWithExactStyleMatch(style);
if (realFont == null || !(realFont instanceof CFont)) return false;
if (!(realFont instanceof CFont cFont)) return false;

CFont newFont = new CFont((CFont)realFont, logicalFamilyName);
CFont newFont = new CFont(cFont, logicalFamilyName);
registerGenericFont(newFont, true);

return true;
@@ -627,8 +627,8 @@ public static Accessible getFocusOwner(final Component c) {
return invokeAndWait(new Callable<Accessible>() {
public Accessible call() throws Exception {
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (c == null || !(c instanceof Accessible)) return null;
return CAccessible.getCAccessible((Accessible)c);
if (!(c instanceof Accessible accessible)) return null;
return CAccessible.getCAccessible(accessible);
}
}, c);
}
@@ -244,12 +244,12 @@ static boolean IS(final int bits, final int mask) {
c.execute(ptr -> nativeRevalidateNSWindowShadow(ptr));
}},
new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) {
if (value == null || !(value instanceof java.io.File)) {
if (!(value instanceof java.io.File file)) {
c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, null));
return;
}

final String filename = ((java.io.File)value).getAbsolutePath();
final String filename = file.getAbsolutePath();
c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, filename));
}},
new Property<CPlatformWindow>(WINDOW_FULL_CONTENT) {
@@ -845,10 +845,10 @@ protected void startPage(PageFormat arg0, Printable arg1, int arg2, boolean arg3
@Override
protected MediaSize getMediaSize(Media media, PrintService service,
PageFormat page) {
if (media == null || !(media instanceof MediaSizeName)) {
if (!(media instanceof MediaSizeName msn)) {
return getDefaultMediaSize(page);
}
MediaSize size = MediaSize.getMediaSizeForName((MediaSizeName) media);
MediaSize size = MediaSize.getMediaSizeForName(msn);
return size != null ? size : getDefaultMediaSize(page);
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -210,10 +210,8 @@ public void write(IIOMetadata streamMetadata,

IIOMetadata imageMetadata = image.getMetadata();
BMPMetadata bmpImageMetadata = null;
if (imageMetadata != null
&& imageMetadata instanceof BMPMetadata)
{
bmpImageMetadata = (BMPMetadata)imageMetadata;
if (imageMetadata instanceof BMPMetadata bmp) {
bmpImageMetadata = bmp;
} else {
ImageTypeSpecifier imageType =
new ImageTypeSpecifier(colorModel, sampleModel);
@@ -59,7 +59,7 @@ private SimpleCMYKColorSpace() {
}

public boolean equals(Object o) {
return o != null && o instanceof SimpleCMYKColorSpace;
return o instanceof SimpleCMYKColorSpace;
}

public int hashCode() {
@@ -248,8 +248,7 @@ protected void mergeNativeTree(Node root) throws IIOInvalidTreeException {

Object applicationExtensionData =
applicationExtension.getUserObject();
if (applicationExtensionData == null ||
!(applicationExtensionData instanceof byte[])) {
if (!(applicationExtensionData instanceof byte[])) {
fatal(applicationExtension,
"Bad user object in ApplicationExtension!");
}
@@ -371,8 +371,8 @@ public class JPEGMetadata extends IIOMetadata implements Cloneable {

JPEGImageWriteParam jparam = null;

if ((param != null) && (param instanceof JPEGImageWriteParam)) {
jparam = (JPEGImageWriteParam) param;
if (param instanceof JPEGImageWriteParam p) {
jparam = p;
if (!jparam.areTablesSet()) {
jparam = null;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -274,8 +274,8 @@ protected void initJPEGWriter(boolean supportsStreamMetadata,

// Initialize the ImageWriteParam.
if(this.JPEGParam == null) {
if(param != null && param instanceof JPEGImageWriteParam) {
JPEGParam = (JPEGImageWriteParam)param;
if (param instanceof JPEGImageWriteParam p) {
JPEGParam = p;
} else {
JPEGParam =
new JPEGImageWriteParam(writer != null ?
@@ -2895,7 +2895,7 @@ private void checkParamsEmpty(ImageTypeSpecifier imageType,
int numThumbs = thumbnails.size();
for(int i = 0; i < numThumbs; i++) {
Object thumb = thumbnails.get(i);
if(thumb == null || !(thumb instanceof BufferedImage)) {
if (!(thumb instanceof BufferedImage)) {
throw new IllegalArgumentException
("thumbnails contains null references or objects other than BufferedImages!");
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -787,8 +787,8 @@ public Component getListCellRendererComponent(JList<?> list,

super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (value != null && value instanceof FileFilter) {
setText(((FileFilter)value).getDescription());
if (value instanceof FileFilter fileFilter) {
setText(fileFilter.getDescription());
}

return this;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -110,10 +110,10 @@ public void mousePressed(MouseEvent e) {