6567433: JComponent.updateUI() may create StackOverflowError

Reviewed-by: alexsch, rchamyal
This commit is contained in:
Ajit Ghaisas 2016-07-13 16:05:36 +05:30
parent b0db527992
commit cc741b81f7
10 changed files with 632 additions and 45 deletions

View File

@ -168,6 +168,9 @@ implements ItemSelectable,ListDataListener,ActionListener, Accessible {
// Flag to ensure the we don't get multiple ActionEvents on item selection.
private boolean selectingItem = false;
// Flag to indicate UI update is in progress
private transient boolean updateInProgress;
/**
* Creates a <code>JComboBox</code> that takes its items from an
* existing <code>ComboBoxModel</code>. Since the
@ -268,11 +271,18 @@ implements ItemSelectable,ListDataListener,ActionListener, Accessible {
* @see JComponent#updateUI
*/
public void updateUI() {
setUI((ComboBoxUI)UIManager.getUI(this));
if (!updateInProgress) {
updateInProgress = true;
try {
setUI((ComboBoxUI)UIManager.getUI(this));
ListCellRenderer<? super E> renderer = getRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
ListCellRenderer<? super E> renderer = getRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
} finally {
updateInProgress = false;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -337,6 +337,11 @@ public class JList<E> extends JComponent implements Scrollable, Accessible
*/
private transient DropLocation dropLocation;
/**
* Flag to indicate UI update is in progress
*/
private transient boolean updateInProgress;
/**
* A subclass of <code>TransferHandler.DropLocation</code> representing
* a drop location for a <code>JList</code>.
@ -531,11 +536,18 @@ public class JList<E> extends JComponent implements Scrollable, Accessible
* @see SwingUtilities#updateComponentTreeUI
*/
public void updateUI() {
setUI((ListUI)UIManager.getUI(this));
if (!updateInProgress) {
updateInProgress = true;
try {
setUI((ListUI)UIManager.getUI(this));
ListCellRenderer<? super E> renderer = getCellRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
ListCellRenderer<? super E> renderer = getCellRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
} finally {
updateInProgress = false;
}
}
}

View File

@ -454,6 +454,11 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
*/
private transient DropLocation dropLocation;
/**
* Flag to indicate UI update is in progress
*/
private transient boolean updateInProgress;
/**
* A subclass of <code>TransferHandler.DropLocation</code> representing
* a drop location for a <code>JTable</code>.
@ -3621,36 +3626,46 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
* @see JComponent#updateUI
*/
public void updateUI() {
// Update the UIs of the cell renderers, cell editors and header renderers.
TableColumnModel cm = getColumnModel();
for(int column = 0; column < cm.getColumnCount(); column++) {
TableColumn aColumn = cm.getColumn(column);
SwingUtilities.updateRendererOrEditorUI(aColumn.getCellRenderer());
SwingUtilities.updateRendererOrEditorUI(aColumn.getCellEditor());
SwingUtilities.updateRendererOrEditorUI(aColumn.getHeaderRenderer());
if (updateInProgress) {
return;
}
// Update the UIs of all the default renderers.
Enumeration<?> defaultRenderers = defaultRenderersByColumnClass.elements();
while (defaultRenderers.hasMoreElements()) {
SwingUtilities.updateRendererOrEditorUI(defaultRenderers.nextElement());
updateInProgress = true;
try {
// Update the UIs of the cell renderers, cell editors and header renderers.
TableColumnModel cm = getColumnModel();
for(int column = 0; column < cm.getColumnCount(); column++) {
TableColumn aColumn = cm.getColumn(column);
SwingUtilities.updateRendererOrEditorUI(aColumn.getCellRenderer());
SwingUtilities.updateRendererOrEditorUI(aColumn.getCellEditor());
SwingUtilities.updateRendererOrEditorUI(aColumn.getHeaderRenderer());
}
// Update the UIs of all the default renderers.
Enumeration<?> defaultRenderers = defaultRenderersByColumnClass.elements();
while (defaultRenderers.hasMoreElements()) {
SwingUtilities.updateRendererOrEditorUI(defaultRenderers.nextElement());
}
// Update the UIs of all the default editors.
Enumeration<?> defaultEditors = defaultEditorsByColumnClass.elements();
while (defaultEditors.hasMoreElements()) {
SwingUtilities.updateRendererOrEditorUI(defaultEditors.nextElement());
}
// Update the UI of the table header
if (tableHeader != null && tableHeader.getParent() == null) {
tableHeader.updateUI();
}
// Update UI applied to parent ScrollPane
configureEnclosingScrollPaneUI();
setUI((TableUI)UIManager.getUI(this));
} finally {
updateInProgress = false;
}
// Update the UIs of all the default editors.
Enumeration<?> defaultEditors = defaultEditorsByColumnClass.elements();
while (defaultEditors.hasMoreElements()) {
SwingUtilities.updateRendererOrEditorUI(defaultEditors.nextElement());
}
// Update the UI of the table header
if (tableHeader != null && tableHeader.getParent() == null) {
tableHeader.updateUI();
}
// Update UI applied to parent ScrollPane
configureEnclosingScrollPaneUI();
setUI((TableUI)UIManager.getUI(this));
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -320,6 +320,11 @@ public class JTree extends JComponent implements Scrollable, Accessible
*/
private transient DropLocation dropLocation;
/**
* Flag to indicate UI update is in progress
*/
private transient boolean updateInProgress;
/**
* A subclass of <code>TransferHandler.DropLocation</code> representing
* a drop location for a <code>JTree</code>.
@ -713,10 +718,19 @@ public class JTree extends JComponent implements Scrollable, Accessible
* @see JComponent#updateUI
*/
public void updateUI() {
setUI((TreeUI)UIManager.getUI(this));
if (!updateInProgress) {
SwingUtilities.updateRendererOrEditorUI(getCellRenderer());
SwingUtilities.updateRendererOrEditorUI(getCellEditor());
updateInProgress = true;
try {
setUI((TreeUI)UIManager.getUI(this));
SwingUtilities.updateRendererOrEditorUI(getCellRenderer());
SwingUtilities.updateRendererOrEditorUI(getCellEditor());
} finally {
updateInProgress = false;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -118,6 +118,11 @@ public class JTableHeader extends JComponent implements TableColumnModelListener
*/
private TableCellRenderer defaultRenderer;
/**
* Flag to indicate UI update is in progress
*/
private transient boolean updateInProgress;
//
// Constructors
//
@ -475,11 +480,18 @@ public class JTableHeader extends JComponent implements TableColumnModelListener
* @see JComponent#updateUI
*/
public void updateUI(){
setUI((TableHeaderUI)UIManager.getUI(this));
if (!updateInProgress) {
updateInProgress = true;
try {
setUI((TableHeaderUI)UIManager.getUI(this));
TableCellRenderer renderer = getDefaultRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
TableCellRenderer renderer = getDefaultRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
} finally {
updateInProgress = false;
}
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 6567433
*
* @summary JComboBox.updateUI() invokes updateUI() on its cellrenderer via
* SwingUtilities.updateComponentTreeUI().
* If the cellrenderer is a parent of this JComboBox the method recurses
* endless.
* This test tests that the fix is effective in avoiding recursion.
*
* @run main/othervm UpdateUIRecursionTest
*/
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
public class UpdateUIRecursionTest extends JFrame implements ListCellRenderer {
JComboBox combo;
DefaultListCellRenderer renderer;
public UpdateUIRecursionTest() {
super("UpdateUIRecursionTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
String[] listData = {
"First", "Second", "Third", "Fourth", "Fifth", "Sixth"
};
combo = new JComboBox(listData);
combo.setRenderer(this);
renderer = new DefaultListCellRenderer();
getContentPane().add(new JScrollPane(combo), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
UpdateUIRecursionTest obj = new UpdateUIRecursionTest();
obj.test();
obj.disposeUI();
}
});
}
public void test() {
combo.updateUI();
}
public void disposeUI() {
setVisible(false);
dispose();
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
return renderer.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
}

View File

@ -0,0 +1,97 @@
/*
* Copyright (c) 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 6567433
*
* @summary JList.updateUI() for invokes updateUI() on its cellrenderer via
* SwingUtilities.updateComponentTreeUI().
* If the cellrenderer is a parent of this JList the method recurses
* endless.
* This test tests that the fix is effective in avoiding recursion.
*
* @run main/othervm UpdateUIRecursionTest
*/
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
public class UpdateUIRecursionTest extends JFrame implements ListCellRenderer {
JList list;
DefaultListCellRenderer renderer;
public UpdateUIRecursionTest() {
super("UpdateUIRecursionTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
String[] listData = {
"First", "Second", "Third", "Fourth", "Fifth", "Sixth"
};
list = new JList(listData);
list.setCellRenderer(this);
renderer = new DefaultListCellRenderer();
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
UpdateUIRecursionTest obj = new UpdateUIRecursionTest();
obj.test();
obj.disposeUI();
}
});
}
public void test() {
list.updateUI();
}
public void disposeUI() {
setVisible(false);
dispose();
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
return renderer.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 6567433
*
* @summary JTable.updateUI() invokes updateUI() on its TableCellrenderer via
* SwingUtilities.updateRendererOrEditorUI().
* If the TableCellrenderer is a parent of this JTable the method recurses
* endless.
* This test tests that the fix is effective in avoiding recursion.
*
* @run main/othervm UpdateUIRecursionTest
*/
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class UpdateUIRecursionTest extends JFrame implements TableCellRenderer {
JTable table;
DefaultTableCellRenderer renderer;
public UpdateUIRecursionTest() {
super("UpdateUIRecursionTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
String[] columnNames = {
"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};
table = new JTable(data, columnNames);
renderer = new DefaultTableCellRenderer();
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
table.setDefaultRenderer(table.getColumnClass(1), this);
setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
UpdateUIRecursionTest obj = new UpdateUIRecursionTest();
obj.test();
obj.disposeUI();
}
});
}
public void test() {
table.updateUI();
}
public void disposeUI() {
setVisible(false);
dispose();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column)
{
return renderer.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 6567433
*
* @summary JTableHeader.updateUI() invokes updateUI() on its TableCellrenderer via
* SwingUtilities.updateComponentTreeUI().
* If the Tablecellrenderer is a parent of this JTableHeader, the method recurses
* endless.
* This test tests that the fix is effective in avoiding recursion.
*
* @run main/othervm UpdateUIRecursionTest
*/
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
public class UpdateUIRecursionTest extends JFrame implements TableCellRenderer {
JTable table;
public UpdateUIRecursionTest() {
super("UpdateUIRecursionTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
String[] columnNames = {
"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};
JTableHeader tableHeader = new JTableHeader();
table = new JTable(data, columnNames);
table.setTableHeader(tableHeader);
tableHeader.setDefaultRenderer(this);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
UpdateUIRecursionTest obj = new UpdateUIRecursionTest();
obj.test();
obj.disposeUI();
}
});
}
public void test() {
SwingUtilities.updateComponentTreeUI(this);
}
public void disposeUI() {
setVisible(false);
dispose();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
return new JLabel(String.valueOf(value));
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 6567433
*
* @summary JTree.updateUI() invokes updateUI() on its TreeCellrenderer via
* SwingUtilities.updateRendererOrEditorUI().
* If the TreeCellrenderer is a parent of this JTree, the method recurses
* endless.
* This test tests that the fix is effective in avoiding recursion.
*
* @run main/othervm UpdateUIRecursionTest
*/
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.DefaultTreeCellRenderer;
public class UpdateUIRecursionTest extends JFrame implements TreeCellRenderer {
JTree tree;
DefaultTreeCellRenderer renderer;
public UpdateUIRecursionTest() {
super("UpdateUIRecursionTest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
String[] listData = {
"First", "Second", "Third", "Fourth", "Fifth", "Sixth"
};
tree = new JTree(listData);
renderer = new DefaultTreeCellRenderer();
getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
tree.setCellRenderer(this);
setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
UpdateUIRecursionTest obj = new UpdateUIRecursionTest();
obj.test();
obj.disposeUI();
}
});
}
public void test() {
tree.updateUI();
}
public void disposeUI() {
setVisible(false);
dispose();
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf,
int row, boolean hasFocus)
{
return renderer.getTreeCellRendererComponent(tree, value, leaf,
expanded, leaf, row, hasFocus);
}
}