From 6d0b9ddd95d85f96b5c16f10b9764c340afe9f10 Mon Sep 17 00:00:00 2001 From: prrace Date: Mon, 26 Jan 2026 20:21:46 -0800 Subject: [PATCH] 8376423 --- .../share/classes/javax/swing/UIManager.java | 26 ++-- .../swing/plaf/metal/DefaultMetalTheme.java | 5 +- .../classes/sun/swing/SwingAccessor.java | 27 +++++ .../classes/sun/swing/SwingUtilities2.java | 6 - .../javax/swing/UIManager/Test6657026.java | 60 ---------- .../plaf/metal/MetalUtils/bug6190373.java | 112 ------------------ 6 files changed, 40 insertions(+), 196 deletions(-) delete mode 100644 test/jdk/javax/swing/UIManager/Test6657026.java delete mode 100644 test/jdk/javax/swing/plaf/metal/MetalUtils/bug6190373.java diff --git a/src/java.desktop/share/classes/javax/swing/UIManager.java b/src/java.desktop/share/classes/javax/swing/UIManager.java index 7a2ae226f33..d34f703fc45 100644 --- a/src/java.desktop/share/classes/javax/swing/UIManager.java +++ b/src/java.desktop/share/classes/javax/swing/UIManager.java @@ -59,6 +59,7 @@ import java.util.Objects; import sun.awt.AppContext; import sun.awt.AWTAccessor; +import sun.swing.SwingAccessor; /** * {@code UIManager} manages the current look and feel, the set of @@ -233,8 +234,9 @@ public class UIManager implements Serializable */ public UIManager() {} + private static final LAFState LAF_STATE = new LAFState(); /** - * Return the LAFState object, lazily create one if necessary. + * Return the LAFState object. * All access to the LAFState fields is done via this method, * for example: *
@@ -242,22 +244,16 @@ public class UIManager implements Serializable
      * 
*/ private static LAFState getLAFState() { - LAFState rv = (LAFState)SwingUtilities.appContextGet( - SwingUtilities2.LAF_STATE_KEY); - if (rv == null) { - synchronized (classLock) { - rv = (LAFState)SwingUtilities.appContextGet( - SwingUtilities2.LAF_STATE_KEY); - if (rv == null) { - SwingUtilities.appContextPut( - SwingUtilities2.LAF_STATE_KEY, - (rv = new LAFState())); - } - } - } - return rv; + return LAF_STATE; } + static { + SwingAccessor.setLAFStateAccessor(UIManager::isLafStateInitialized); + } + + private static boolean isLafStateInitialized() { + return LAF_STATE.initialized; + } /* Keys used in the swing.properties properties file. * See loadUserProperties(), initialize(). diff --git a/src/java.desktop/share/classes/javax/swing/plaf/metal/DefaultMetalTheme.java b/src/java.desktop/share/classes/javax/swing/plaf/metal/DefaultMetalTheme.java index 8ee862fb0b3..26d5866f9bf 100644 --- a/src/java.desktop/share/classes/javax/swing/plaf/metal/DefaultMetalTheme.java +++ b/src/java.desktop/share/classes/javax/swing/plaf/metal/DefaultMetalTheme.java @@ -29,7 +29,7 @@ import javax.swing.plaf.*; import javax.swing.*; import java.awt.*; -import sun.awt.AppContext; +import sun.swing.SwingAccessor; import sun.swing.SwingUtilities2; /** @@ -153,8 +153,7 @@ public class DefaultMetalTheme extends MetalTheme { static int getDefaultFontStyle(int key) { if (key != WINDOW_TITLE_FONT) { Object boldMetal = null; - if (AppContext.getAppContext().get( - SwingUtilities2.LAF_STATE_KEY) != null) { + if (SwingAccessor.getLAFStateAccessor().lafStateIsInitialized()) { // Only access the boldMetal key if a look and feel has // been loaded, otherwise we'll trigger loading the look // and feel. diff --git a/src/java.desktop/share/classes/sun/swing/SwingAccessor.java b/src/java.desktop/share/classes/sun/swing/SwingAccessor.java index 5c561de4a42..2ffca14cbd0 100644 --- a/src/java.desktop/share/classes/sun/swing/SwingAccessor.java +++ b/src/java.desktop/share/classes/sun/swing/SwingAccessor.java @@ -133,6 +133,33 @@ public final class SwingAccessor { KeyStroke create(); } + /* + * An accessor for the LAFState class state + */ + public interface LAFStateAccessor { + boolean lafStateIsInitialized(); + } + + private static LAFStateAccessor lafStateAccessor; + /** + * Set an accessor object for the LAFState class. + */ + public static void setLAFStateAccessor(LAFStateAccessor accessor) { + lafStateAccessor = accessor; + } + + /** + * Retrieve the accessor object for the LAFState class + */ + public static LAFStateAccessor getLAFStateAccessor() { + var access = lafStateAccessor; + if (access == null) { + ensureClassInitialized(UIManager.class); + access = lafStateAccessor; + } + return access; + } + /** * The javax.swing.JComponent class accessor object. */ diff --git a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java index 20a07c608a0..cdc5b48e7da 100644 --- a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java +++ b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java @@ -120,12 +120,6 @@ import static java.awt.geom.AffineTransform.TYPE_TRANSLATION; * */ public class SwingUtilities2 { - /** - * The {@code AppContext} key for our one {@code LAFState} - * instance. - */ - public static final Object LAF_STATE_KEY = - new StringBuffer("LookAndFeel State"); public static final Object MENU_SELECTION_MANAGER_LISTENER_KEY = new StringBuffer("MenuSelectionManager listener key"); diff --git a/test/jdk/javax/swing/UIManager/Test6657026.java b/test/jdk/javax/swing/UIManager/Test6657026.java deleted file mode 100644 index 5ce1ea73de4..00000000000 --- a/test/jdk/javax/swing/UIManager/Test6657026.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2009, 2015, 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 6657026 - * @summary Tests shared UIManager in different application contexts - * @author Sergey Malenkov - * @modules java.desktop/sun.awt - */ - -import sun.awt.SunToolkit; - -import javax.swing.UIManager; -import javax.swing.UIManager.LookAndFeelInfo; - -public class Test6657026 implements Runnable { - - public static void main(String[] args) throws Exception { - if (UIManager.getInstalledLookAndFeels().length == 0) { - throw new Error("unexpected amount of look&feels"); - } - UIManager.setInstalledLookAndFeels(new LookAndFeelInfo[0]); - if (UIManager.getInstalledLookAndFeels().length != 0) { - throw new Error("unexpected amount of look&feels"); - } - - ThreadGroup group = new ThreadGroup("$$$"); - Thread thread = new Thread(group, new Test6657026()); - thread.start(); - thread.join(); - } - - public void run() { - SunToolkit.createNewAppContext(); - if (UIManager.getInstalledLookAndFeels().length == 0) { - throw new Error("shared look&feels"); - } - } -} diff --git a/test/jdk/javax/swing/plaf/metal/MetalUtils/bug6190373.java b/test/jdk/javax/swing/plaf/metal/MetalUtils/bug6190373.java deleted file mode 100644 index 07211c7a569..00000000000 --- a/test/jdk/javax/swing/plaf/metal/MetalUtils/bug6190373.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (c) 2005, 2018, 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. - */ - -import java.awt.Graphics; -import java.awt.image.BufferedImage; -import java.util.concurrent.CyclicBarrier; - -import javax.swing.JButton; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; - -import sun.awt.AppContext; -import sun.awt.SunToolkit; - -import static javax.swing.UIManager.getInstalledLookAndFeels; - -/** - * @test - * @bug 6190373 - * @summary Tests 6190373 - * @author Scott Violet - * @modules java.desktop/sun.awt - */ -public final class bug6190373 { - - private static AppContext app1; - private static AppContext app2; - private static final int LOOP_COUNT = 10000; - private static final CyclicBarrier barrier = new CyclicBarrier(2); - - public static void main(final String[] args) throws Exception { - final Thread t1 = new Thread(new ThreadGroup("firstGroup"), () -> { - app1 = SunToolkit.createNewAppContext(); - test(true); - }); - final Thread t2 = new Thread(new ThreadGroup("secondGroup"), () -> { - app2 = SunToolkit.createNewAppContext(); - test(false); - }); - - t1.start(); - t2.start(); - t1.join(); - t2.join(); - app1.dispose(); - app2.dispose(); - } - - private static void test(final boolean lock) { - for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) { - try { - SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf)); - barrier.await(); - SwingUtilities.invokeAndWait(() -> slam(lock)); - barrier.await(); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } - } - - private static void slam(final boolean lock) { - JButton button = new JButton("HI"); - button.setSize(100, 100); - BufferedImage image = new BufferedImage(100, 100, - BufferedImage.TYPE_INT_RGB); - for (int i = 0; i < LOOP_COUNT; i++) { - Graphics g = image.getGraphics(); - if (lock) { - synchronized (button.getTreeLock()) { - button.paint(g); - } - } else { - button.paint(g); - } - g.dispose(); - } - } - - private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) { - try { - UIManager.setLookAndFeel(laf.getClassName()); - System.out.println("LookAndFeel: " + laf.getClassName()); - } catch (final UnsupportedLookAndFeelException ignored){ - System.out.println("Unsupported LookAndFeel: " + laf.getClassName()); - } catch (ClassNotFoundException | InstantiationException | - IllegalAccessException e) { - throw new RuntimeException(e); - } - } -}