From fcc2a2922fe0312758a9eef5f1ea371e5803bc8b Mon Sep 17 00:00:00 2001 From: Phil Race Date: Thu, 26 Feb 2026 16:23:49 +0000 Subject: [PATCH] 8378297: Remove AppContext from several Swing component and related classes Reviewed-by: azvegint, psadhukhan, dnguyen --- .../classes/javax/swing/DebugGraphics.java | 16 ++-- .../share/classes/javax/swing/JDialog.java | 24 ++---- .../share/classes/javax/swing/JFrame.java | 24 ++---- .../share/classes/javax/swing/JPopupMenu.java | 20 +---- .../classes/javax/swing/PopupFactory.java | 63 ++++++---------- .../classes/javax/swing/SwingUtilities.java | 20 ++--- .../classes/javax/swing/ToolTipManager.java | 13 ++-- .../swing/ToolTipManager/Test6657026.java | 75 ------------------- 8 files changed, 57 insertions(+), 198 deletions(-) delete mode 100644 test/jdk/javax/swing/ToolTipManager/Test6657026.java diff --git a/src/java.desktop/share/classes/javax/swing/DebugGraphics.java b/src/java.desktop/share/classes/javax/swing/DebugGraphics.java index 83b44716c04..512b56ee28a 100644 --- a/src/java.desktop/share/classes/javax/swing/DebugGraphics.java +++ b/src/java.desktop/share/classes/javax/swing/DebugGraphics.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, 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 @@ -1492,14 +1492,12 @@ public class DebugGraphics extends Graphics { /** Returns DebugGraphicsInfo, or creates one if none exists. */ static DebugGraphicsInfo info() { - DebugGraphicsInfo debugGraphicsInfo = (DebugGraphicsInfo) - SwingUtilities.appContextGet(debugGraphicsInfoKey); - if (debugGraphicsInfo == null) { - debugGraphicsInfo = new DebugGraphicsInfo(); - SwingUtilities.appContextPut(debugGraphicsInfoKey, - debugGraphicsInfo); + synchronized (DebugGraphicsInfo.class) { + if (debugGraphicsInfo == null) { + debugGraphicsInfo = new DebugGraphicsInfo(); + } + return debugGraphicsInfo; } - return debugGraphicsInfo; } - private static final Class debugGraphicsInfoKey = DebugGraphicsInfo.class; + private static DebugGraphicsInfo debugGraphicsInfo; } diff --git a/src/java.desktop/share/classes/javax/swing/JDialog.java b/src/java.desktop/share/classes/javax/swing/JDialog.java index 99d6385cd7c..a7d8791c72c 100644 --- a/src/java.desktop/share/classes/javax/swing/JDialog.java +++ b/src/java.desktop/share/classes/javax/swing/JDialog.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, 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 @@ -101,13 +101,6 @@ public class JDialog extends Dialog implements WindowConstants, RootPaneContainer, TransferHandler.HasGetTransferHandler { - /** - * Key into the AppContext, used to check if should provide decorations - * by default. - */ - private static final Object defaultLookAndFeelDecoratedKey = - new StringBuffer("JDialog.defaultLookAndFeelDecorated"); - private int defaultCloseOperation = HIDE_ON_CLOSE; /** @@ -1119,6 +1112,8 @@ public class JDialog extends Dialog implements WindowConstants, } } + private static boolean defaultLAFDecorated; + /** * Provides a hint as to whether or not newly created {@code JDialog}s * should have their Window decorations (such as borders, widgets to @@ -1144,11 +1139,7 @@ public class JDialog extends Dialog implements WindowConstants, * @since 1.4 */ public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { - if (defaultLookAndFeelDecorated) { - SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); - } else { - SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); - } + defaultLAFDecorated = defaultLookAndFeelDecorated; } /** @@ -1160,12 +1151,7 @@ public class JDialog extends Dialog implements WindowConstants, * @since 1.4 */ public static boolean isDefaultLookAndFeelDecorated() { - Boolean defaultLookAndFeelDecorated = - (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); - if (defaultLookAndFeelDecorated == null) { - defaultLookAndFeelDecorated = Boolean.FALSE; - } - return defaultLookAndFeelDecorated.booleanValue(); + return defaultLAFDecorated; } /** diff --git a/src/java.desktop/share/classes/javax/swing/JFrame.java b/src/java.desktop/share/classes/javax/swing/JFrame.java index 8bde7e18f03..bab215625b9 100644 --- a/src/java.desktop/share/classes/javax/swing/JFrame.java +++ b/src/java.desktop/share/classes/javax/swing/JFrame.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, 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 @@ -126,13 +126,6 @@ public class JFrame extends Frame implements WindowConstants, RootPaneContainer, TransferHandler.HasGetTransferHandler { - /** - * Key into the AppContext, used to check if should provide decorations - * by default. - */ - private static final Object defaultLookAndFeelDecoratedKey = - new StringBuffer("JFrame.defaultLookAndFeelDecorated"); - private int defaultCloseOperation = HIDE_ON_CLOSE; /** @@ -755,6 +748,8 @@ public class JFrame extends Frame implements WindowConstants, } } + private static boolean defaultLAFDecorated; + /** * Provides a hint as to whether or not newly created JFrames * should have their Window decorations (such as borders, widgets to @@ -780,11 +775,7 @@ public class JFrame extends Frame implements WindowConstants, * @since 1.4 */ public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { - if (defaultLookAndFeelDecorated) { - SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); - } else { - SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); - } + defaultLAFDecorated = defaultLookAndFeelDecorated; } @@ -797,12 +788,7 @@ public class JFrame extends Frame implements WindowConstants, * @since 1.4 */ public static boolean isDefaultLookAndFeelDecorated() { - Boolean defaultLookAndFeelDecorated = - (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); - if (defaultLookAndFeelDecorated == null) { - defaultLookAndFeelDecorated = Boolean.FALSE; - } - return defaultLookAndFeelDecorated.booleanValue(); + return defaultLAFDecorated; } /** diff --git a/src/java.desktop/share/classes/javax/swing/JPopupMenu.java b/src/java.desktop/share/classes/javax/swing/JPopupMenu.java index 1b04e8c6169..2ecc7cf3b1e 100644 --- a/src/java.desktop/share/classes/javax/swing/JPopupMenu.java +++ b/src/java.desktop/share/classes/javax/swing/JPopupMenu.java @@ -112,12 +112,6 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { */ private static final String uiClassID = "PopupMenuUI"; - /** - * Key used in AppContext to determine if light way popups are the default. - */ - private static final Object defaultLWPopupEnabledKey = - new StringBuffer("JPopupMenu.defaultLWPopupEnabledKey"); - /** Bug#4425878-Property javax.swing.adjustPopupLocationToFit introduced */ static boolean popupPositionFixDisabled = System.getProperty("javax.swing.adjustPopupLocationToFit","").equals("false"); @@ -153,6 +147,8 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { private static final boolean VERBOSE = false; // show reuse hits/misses private static final boolean DEBUG = false; // show bad params, misc. + private static boolean defaultLWPopupEnabled = true; + /** * Sets the default value of the lightWeightPopupEnabled * property. @@ -163,8 +159,7 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { * @see #setLightWeightPopupEnabled */ public static void setDefaultLightWeightPopupEnabled(boolean aFlag) { - SwingUtilities.appContextPut(defaultLWPopupEnabledKey, - Boolean.valueOf(aFlag)); + defaultLWPopupEnabled = aFlag; } /** @@ -177,14 +172,7 @@ public class JPopupMenu extends JComponent implements Accessible,MenuElement { * @see #setDefaultLightWeightPopupEnabled */ public static boolean getDefaultLightWeightPopupEnabled() { - Boolean b = (Boolean) - SwingUtilities.appContextGet(defaultLWPopupEnabledKey); - if (b == null) { - SwingUtilities.appContextPut(defaultLWPopupEnabledKey, - Boolean.TRUE); - return true; - } - return b.booleanValue(); + return defaultLWPopupEnabled; } /** diff --git a/src/java.desktop/share/classes/javax/swing/PopupFactory.java b/src/java.desktop/share/classes/javax/swing/PopupFactory.java index 2b274c816b1..208177fc55e 100644 --- a/src/java.desktop/share/classes/javax/swing/PopupFactory.java +++ b/src/java.desktop/share/classes/javax/swing/PopupFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2026, 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 @@ -80,13 +80,6 @@ public class PopupFactory { } }); } - /** - * The shared instanceof PopupFactory is per - * AppContext. This is the key used in the - * AppContext to locate the PopupFactory. - */ - private static final Object SharedInstanceKey = - new StringBuffer("PopupFactory.SharedInstanceKey"); /** * Max number of items to store in any one particular cache. @@ -118,6 +111,7 @@ public class PopupFactory { */ public PopupFactory() {} + static PopupFactory sharedFactory; /** * Sets the PopupFactory that will be used to obtain * Popups. @@ -132,7 +126,9 @@ public class PopupFactory { if (factory == null) { throw new IllegalArgumentException("PopupFactory can not be null"); } - SwingUtilities.appContextPut(SharedInstanceKey, factory); + synchronized (PopupFactory.class) { + sharedFactory = factory; + } } /** @@ -142,14 +138,12 @@ public class PopupFactory { * @return Shared PopupFactory */ public static PopupFactory getSharedInstance() { - PopupFactory factory = (PopupFactory)SwingUtilities.appContextGet( - SharedInstanceKey); - - if (factory == null) { - factory = new PopupFactory(); - setSharedInstance(factory); + synchronized (PopupFactory.class) { + if (sharedFactory == null) { + sharedFactory = new PopupFactory(); + } + return sharedFactory; } - return factory; } @@ -351,8 +345,6 @@ public class PopupFactory { * Popup implementation that uses a Window as the popup. */ private static class HeavyWeightPopup extends Popup { - private static final Object heavyWeightPopupCacheKey = - new StringBuffer("PopupFactory.heavyWeightPopupCache"); private volatile boolean isCacheEnabled = true; @@ -434,21 +426,16 @@ public class PopupFactory { } } + private static Map> cache; /** * Returns the cache to use for heavy weight popups. Maps from * Window to a List of * HeavyWeightPopups. */ - @SuppressWarnings("unchecked") private static Map> getHeavyWeightPopupCache() { synchronized (HeavyWeightPopup.class) { - Map> cache = (Map>)SwingUtilities.appContextGet( - heavyWeightPopupCacheKey); - if (cache == null) { cache = new HashMap<>(2); - SwingUtilities.appContextPut(heavyWeightPopupCacheKey, - cache); } return cache; } @@ -728,18 +715,17 @@ public class PopupFactory { return popup; } + private static List cache; /** * Returns the cache to use for heavy weight popups. */ - @SuppressWarnings("unchecked") private static List getLightWeightPopupCache() { - List cache = (List)SwingUtilities.appContextGet( - lightWeightPopupCacheKey); - if (cache == null) { - cache = new ArrayList<>(); - SwingUtilities.appContextPut(lightWeightPopupCacheKey, cache); + synchronized (LightWeightPopup.class) { + if (cache == null) { + cache = new ArrayList<>(); + } + return cache; } - return cache; } /** @@ -849,8 +835,6 @@ public class PopupFactory { * Popup implementation that uses a Panel as the popup. */ private static class MediumWeightPopup extends ContainerPopup { - private static final Object mediumWeightPopupCacheKey = - new StringBuffer("PopupFactory.mediumPopupCache"); /** Child of the panel. The contents are added to this. */ private JRootPane rootPane; @@ -877,19 +861,18 @@ public class PopupFactory { return popup; } + private static List cache; /** * Returns the cache to use for medium weight popups. */ @SuppressWarnings("unchecked") private static List getMediumWeightPopupCache() { - List cache = (List)SwingUtilities.appContextGet( - mediumWeightPopupCacheKey); - - if (cache == null) { - cache = new ArrayList<>(); - SwingUtilities.appContextPut(mediumWeightPopupCacheKey, cache); + synchronized (MediumWeightPopup.class) { + if (cache == null) { + cache = new ArrayList<>(); + } + return cache; } - return cache; } /** diff --git a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index bc6441212de..afe1c444c31 100644 --- a/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2026, 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 @@ -1899,11 +1899,6 @@ public class SwingUtilities implements SwingConstants return null; } - - // Don't use String, as it's not guaranteed to be unique in a Hashtable. - private static final Object sharedOwnerFrameKey = - new StringBuffer("SwingUtilities.sharedOwnerFrame"); - @SuppressWarnings("serial") // JDK-implementation class static class SharedOwnerFrame extends Frame implements WindowListener { public void addNotify() { @@ -1961,6 +1956,7 @@ public class SwingUtilities implements SwingConstants } } + private static Frame sharedOwnerFrame; /** * Returns a toolkit-private, shared, invisible Frame * to be the owner for JDialogs and JWindows created with @@ -1970,14 +1966,12 @@ public class SwingUtilities implements SwingConstants * @see java.awt.GraphicsEnvironment#isHeadless */ static Frame getSharedOwnerFrame() throws HeadlessException { - Frame sharedOwnerFrame = - (Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey); - if (sharedOwnerFrame == null) { - sharedOwnerFrame = new SharedOwnerFrame(); - SwingUtilities.appContextPut(sharedOwnerFrameKey, - sharedOwnerFrame); + synchronized (SharedOwnerFrame.class) { + if (sharedOwnerFrame == null) { + sharedOwnerFrame = new SharedOwnerFrame(); + } + return sharedOwnerFrame; } - return sharedOwnerFrame; } /** diff --git a/src/java.desktop/share/classes/javax/swing/ToolTipManager.java b/src/java.desktop/share/classes/javax/swing/ToolTipManager.java index a9e2c6e69bb..4c3d0209823 100644 --- a/src/java.desktop/share/classes/javax/swing/ToolTipManager.java +++ b/src/java.desktop/share/classes/javax/swing/ToolTipManager.java @@ -61,7 +61,6 @@ public final class ToolTipManager extends MouseAdapter implements MouseMotionLis JComponent insideComponent; MouseEvent mouseEvent; boolean showImmediately; - private static final Object TOOL_TIP_MANAGER_KEY = new Object(); transient Popup tipWindow; /** The Window tip is being displayed in. This will be non-null if * the Window tip is in differs from that of insideComponent's Window. @@ -394,19 +393,19 @@ public final class ToolTipManager extends MouseAdapter implements MouseMotionLis } } + private static ToolTipManager manager; /** * Returns a shared ToolTipManager instance. * * @return a shared ToolTipManager object */ public static ToolTipManager sharedInstance() { - Object value = SwingUtilities.appContextGet(TOOL_TIP_MANAGER_KEY); - if (value instanceof ToolTipManager) { - return (ToolTipManager) value; + synchronized(ToolTipManager.class) { + if (manager == null) { + manager = new ToolTipManager(); + } + return manager; } - ToolTipManager manager = new ToolTipManager(); - SwingUtilities.appContextPut(TOOL_TIP_MANAGER_KEY, manager); - return manager; } // add keylistener here to trigger tip for access diff --git a/test/jdk/javax/swing/ToolTipManager/Test6657026.java b/test/jdk/javax/swing/ToolTipManager/Test6657026.java deleted file mode 100644 index 0678d57f768..00000000000 --- a/test/jdk/javax/swing/ToolTipManager/Test6657026.java +++ /dev/null @@ -1,75 +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 ToolTipManager in different application contexts - * @author Sergey Malenkov - * @modules java.desktop/sun.awt - */ - -import sun.awt.SunToolkit; -import javax.swing.ToolTipManager; - -public class Test6657026 implements Runnable { - - private static final int DISMISS = 4000; - private static final int INITIAL = 750; - private static final int RESHOW = 500; - - public static void main(String[] args) throws InterruptedException { - ToolTipManager manager = ToolTipManager.sharedInstance(); - if (DISMISS != manager.getDismissDelay()) { - throw new Error("unexpected dismiss delay"); - } - if (INITIAL != manager.getInitialDelay()) { - throw new Error("unexpected initial delay"); - } - if (RESHOW != manager.getReshowDelay()) { - throw new Error("unexpected reshow delay"); - } - manager.setDismissDelay(DISMISS + 1); - manager.setInitialDelay(INITIAL + 1); - manager.setReshowDelay(RESHOW + 1); - - ThreadGroup group = new ThreadGroup("$$$"); - Thread thread = new Thread(group, new Test6657026()); - thread.start(); - thread.join(); - } - - public void run() { - SunToolkit.createNewAppContext(); - ToolTipManager manager = ToolTipManager.sharedInstance(); - if (DISMISS != manager.getDismissDelay()) { - throw new Error("shared dismiss delay"); - } - if (INITIAL != manager.getInitialDelay()) { - throw new Error("shared initial delay"); - } - if (RESHOW != manager.getReshowDelay()) { - throw new Error("shared reshow delay"); - } - } -}