8379229: Remove AppContext from javax.swing.JComponent

Reviewed-by: psadhukhan, serb
This commit is contained in:
Phil Race 2026-03-09 19:32:47 +00:00
parent 93634b5665
commit a95799d62f
8 changed files with 19 additions and 145 deletions

View File

@ -31,6 +31,7 @@ import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FocusTraversalPolicy;
import java.awt.Font;
import java.awt.FontMetrics;
@ -261,16 +262,6 @@ public abstract class JComponent extends Container implements Serializable,
*/
static boolean DEBUG_GRAPHICS_LOADED;
/**
* Key used to look up a value from the AppContext to determine the
* JComponent the InputVerifier is running for. That is, if
* AppContext.get(INPUT_VERIFIER_SOURCE_KEY) returns non-null, it
* indicates the EDT is calling into the InputVerifier from the
* returned component.
*/
private static final Object INPUT_VERIFIER_SOURCE_KEY =
new StringBuilder("InputVerifierSourceKey");
/* The following fields support set methods for the corresponding
* java.awt.Component properties.
*/
@ -414,8 +405,7 @@ public abstract class JComponent extends Container implements Serializable,
/** ActionMap. */
private ActionMap actionMap;
/** Key used to store the default locale in an AppContext **/
private static final String defaultLocale = "JComponent.defaultLocale";
private static volatile Locale defaultLocale;
private static Component componentObtainingGraphicsFrom;
private static Object componentObtainingGraphicsFromLock = new
@ -2841,12 +2831,12 @@ public abstract class JComponent extends Container implements Serializable,
* @since 1.4
*/
public static Locale getDefaultLocale() {
Locale l = (Locale) SwingUtilities.appContextGet(defaultLocale);
if( l == null ) {
Locale l = defaultLocale;
if (l == null) {
//REMIND(bcb) choosing the default value is more complicated
//than this.
l = Locale.getDefault();
JComponent.setDefaultLocale( l );
JComponent.setDefaultLocale(l);
}
return l;
}
@ -2865,8 +2855,8 @@ public abstract class JComponent extends Container implements Serializable,
* @see #setLocale
* @since 1.4
*/
public static void setDefaultLocale( Locale l ) {
SwingUtilities.appContextPut(defaultLocale, l);
public static void setDefaultLocale(Locale l) {
defaultLocale = l;
}
@ -3507,7 +3497,7 @@ public abstract class JComponent extends Container implements Serializable,
// This class is used by the KeyboardState class to provide a single
// instance that can be stored in the AppContext.
// instance.
static final class IntVector {
int[] array = null;
int count = 0;
@ -3538,24 +3528,12 @@ public abstract class JComponent extends Container implements Serializable,
}
}
private static final IntVector intVector = new IntVector();
@SuppressWarnings("serial")
static class KeyboardState implements Serializable {
private static final Object keyCodesKey =
JComponent.KeyboardState.class;
// Get the array of key codes from the AppContext.
static IntVector getKeyCodeArray() {
IntVector iv =
(IntVector)SwingUtilities.appContextGet(keyCodesKey);
if (iv == null) {
iv = new IntVector();
SwingUtilities.appContextPut(keyCodesKey, iv);
}
return iv;
}
static void registerKeyPressed(int keyCode) {
IntVector kca = getKeyCodeArray();
IntVector kca = intVector;
int count = kca.size();
int i;
for(i=0;i<count;i++) {
@ -3568,7 +3546,7 @@ public abstract class JComponent extends Container implements Serializable,
}
static void registerKeyReleased(int keyCode) {
IntVector kca = getKeyCodeArray();
IntVector kca = intVector;
int count = kca.size();
int i;
for(i=0;i<count;i++) {
@ -3580,7 +3558,7 @@ public abstract class JComponent extends Container implements Serializable,
}
static boolean keyIsPressed(int keyCode) {
IntVector kca = getKeyCodeArray();
IntVector kca = intVector;
int count = kca.size();
int i;
for(i=0;i<count;i++) {
@ -3621,6 +3599,8 @@ public abstract class JComponent extends Container implements Serializable,
}
}
static JComponent ivSourceComponent; // accessed only on EDT.
static final sun.awt.RequestFocusController focusController =
new sun.awt.RequestFocusController() {
public boolean acceptRequestFocus(Component from, Component to,
@ -3644,15 +3624,13 @@ public abstract class JComponent extends Container implements Serializable,
if (iv == null) {
return true;
} else {
Object currentSource = SwingUtilities.appContextGet(
INPUT_VERIFIER_SOURCE_KEY);
JComponent currentSource = ivSourceComponent;
if (currentSource == jFocusOwner) {
// We're currently calling into the InputVerifier
// for this component, so allow the focus change.
return true;
}
SwingUtilities.appContextPut(INPUT_VERIFIER_SOURCE_KEY,
jFocusOwner);
ivSourceComponent = jFocusOwner;
try {
return iv.shouldYieldFocus(jFocusOwner, target);
} finally {
@ -3662,11 +3640,9 @@ public abstract class JComponent extends Container implements Serializable,
// we ensure that if the InputVerifier for
// currentSource does a requestFocus, we don't
// try and run the InputVerifier again.
SwingUtilities.appContextPut(
INPUT_VERIFIER_SOURCE_KEY, currentSource);
ivSourceComponent = currentSource;
} else {
SwingUtilities.appContextRemove(
INPUT_VERIFIER_SOURCE_KEY);
ivSourceComponent = null;
}
}
}
@ -4939,7 +4915,7 @@ public abstract class JComponent extends Container implements Serializable,
// which was causing some people grief.
return;
}
if (SunToolkit.isDispatchThreadForAppContext(this)) {
if (EventQueue.isDispatchThread()) {
invalidate();
RepaintManager.currentManager(this).addInvalidComponent(this);
}

View File

@ -1,102 +0,0 @@
/*
* Copyright (c) 2014, 2017, 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
* @key headful
* @bug 8043610
* @summary Tests that JComponent invalidate, revalidate and repaint methods could
* be called from any thread
* @author Petr Pchelko
* @modules java.desktop/sun.awt
*/
import sun.awt.SunToolkit;
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
public class bug8043610 {
private static volatile JFrame frame;
private static volatile JComponent component;
public static void main(String[] args) throws Exception {
ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);
stubThread.start();
stubThread.join();
CountDownLatch startSwingLatch = new CountDownLatch(1);
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new JFrame();
component = new JLabel("Test Text");
frame.add(component);
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
startSwingLatch.countDown();
});
}).start();
startSwingLatch.await();
AtomicReference<Exception> caughtException = new AtomicReference<>();
Thread checkThread = new Thread(getRootThreadGroup(), () -> {
try {
component.invalidate();
component.revalidate();
component.repaint(new Rectangle(0, 0, 0, 0));
} catch (Exception e) {
caughtException.set(e);
}
});
checkThread.start();
checkThread.join();
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!", caughtException.get());
}
} finally {
new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {
if (frame != null) {
frame.dispose();
}
})).start();
}
}
private static ThreadGroup getRootThreadGroup() {
ThreadGroup currentTG = Thread.currentThread().getThreadGroup();
ThreadGroup parentTG = currentTG.getParent();
while (parentTG != null) {
currentTG = parentTG;
parentTG = currentTG.getParent();
}
return currentTG;
}
}