This commit is contained in:
prrace 2026-01-26 20:21:46 -08:00
parent c69275ddfe
commit 6d0b9ddd95
6 changed files with 40 additions and 196 deletions

View File

@ -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 <code>LAFState</code> object, lazily create one if necessary.
* Return the <code>LAFState</code> object.
* All access to the <code>LAFState</code> fields is done via this method,
* for example:
* <pre>
@ -242,22 +244,16 @@ public class UIManager implements Serializable
* </pre>
*/
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 <code>swing.properties</code> properties file.
* See loadUserProperties(), initialize().

View File

@ -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.

View File

@ -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.
*/

View File

@ -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");

View File

@ -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");
}
}
}

View File

@ -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);
}
}
}