Skip to content

Commit 6d69084

Browse files
author
Andy Goryachev
committed
8342457: Remove calls to doPrivileged in swing
Reviewed-by: kcr, psadhukhan
1 parent 927f07b commit 6d69084

File tree

5 files changed

+31
-105
lines changed

5 files changed

+31
-105
lines changed

modules/javafx.swing/src/main/java/com/sun/javafx/embed/swing/Disposer.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,30 +51,19 @@ public class Disposer implements Runnable {
5151
static {
5252
disposerInstance = new Disposer();
5353

54+
/* The thread must be a member of a thread group
55+
* which will not get GCed before VM exit.
56+
* Make its parent the top-level thread group.
57+
*/
5458
ThreadGroup tg = Thread.currentThread().getThreadGroup();
55-
@SuppressWarnings("removal")
56-
var dummy = java.security.AccessController.doPrivileged(
57-
new java.security.PrivilegedAction() {
58-
@Override
59-
public Object run() {
60-
/* The thread must be a member of a thread group
61-
* which will not get GCed before VM exit.
62-
* Make its parent the top-level thread group.
63-
*/
64-
ThreadGroup tg = Thread.currentThread().getThreadGroup();
65-
for (ThreadGroup tgn = tg;
66-
tgn != null;
67-
tg = tgn, tgn = tg.getParent());
68-
Thread t =
69-
new Thread(tg, disposerInstance, "SwingNode Disposer");
70-
t.setContextClassLoader(null);
71-
t.setDaemon(true);
72-
t.setPriority(Thread.MAX_PRIORITY);
73-
t.start();
74-
return null;
75-
}
76-
}
77-
);
59+
for (ThreadGroup tgn = tg;
60+
tgn != null;
61+
tg = tgn, tgn = tg.getParent());
62+
Thread t = new Thread(tg, disposerInstance, "SwingNode Disposer");
63+
t.setContextClassLoader(null);
64+
t.setDaemon(true);
65+
t.setPriority(Thread.MAX_PRIORITY);
66+
t.start();
7867
}
7968

8069
/**

modules/javafx.swing/src/main/java/com/sun/javafx/embed/swing/FXDnD.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,8 +32,6 @@
3232
import java.awt.dnd.DragSource;
3333
import java.awt.dnd.DropTarget;
3434
import java.awt.dnd.InvalidDnDOperationException;
35-
import java.security.AccessController;
36-
import java.security.PrivilegedAction;
3735
import javafx.embed.swing.SwingNode;
3836
import com.sun.javafx.embed.swing.newimpl.FXDnDInteropN;
3937

@@ -42,22 +40,9 @@
4240
* It allows Swing content to use the FX machinery for performing DnD.
4341
*/
4442
final public class FXDnD {
45-
public static boolean fxAppThreadIsDispatchThread;
43+
public static boolean fxAppThreadIsDispatchThread = "true".equals(System.getProperty("javafx.embed.singleThread"));
4644
private FXDnDInteropN fxdndiop;
4745

48-
static {
49-
@SuppressWarnings("removal")
50-
var dummy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
51-
@Override
52-
public Object run() {
53-
fxAppThreadIsDispatchThread =
54-
"true".equals(System.getProperty("javafx.embed.singleThread"));
55-
return null;
56-
}
57-
});
58-
59-
}
60-
6146
public FXDnD(SwingNode node) {
6247
fxdndiop = new FXDnDInteropN();
6348
fxdndiop.setNode(node);

modules/javafx.swing/src/main/java/com/sun/javafx/embed/swing/SwingFXUtilsImpl.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,8 +26,6 @@
2626
package com.sun.javafx.embed.swing;
2727

2828
import java.awt.EventQueue;
29-
import java.security.AccessController;
30-
import java.security.PrivilegedAction;
3129
import com.sun.javafx.embed.swing.newimpl.SwingFXUtilsImplInteropN;
3230

3331
public class SwingFXUtilsImpl {
@@ -38,10 +36,8 @@ public class SwingFXUtilsImpl {
3836
swFXUtilIOP = new SwingFXUtilsImplInteropN();
3937
}
4038

41-
@SuppressWarnings("removal")
4239
private static EventQueue getEventQueue() {
43-
return AccessController.doPrivileged(
44-
(PrivilegedAction<EventQueue>) () -> java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue());
40+
return java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue();
4541
}
4642

4743
//Called with reflection from PlatformImpl to avoid dependency

modules/javafx.swing/src/main/java/javafx/embed/swing/JFXPanel.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
import java.awt.image.DataBufferInt;
5858
import java.nio.IntBuffer;
5959
import java.util.concurrent.atomic.AtomicInteger;
60-
import java.security.AccessController;
61-
import java.security.PrivilegedAction;
6260
import javax.swing.JComponent;
6361
import javax.swing.SwingUtilities;
6462

@@ -237,10 +235,7 @@ private synchronized static void initFx() {
237235
if (fxInitialized) {
238236
return;
239237
}
240-
@SuppressWarnings("removal")
241-
EventQueue eventQueue = AccessController.doPrivileged(
242-
(PrivilegedAction<EventQueue>) java.awt.Toolkit
243-
.getDefaultToolkit()::getSystemEventQueue);
238+
EventQueue eventQueue = java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue();
244239
if (eventQueue.isDispatchThread()) {
245240
// We won't block EDT by FX initialization
246241
SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop();
@@ -321,10 +316,7 @@ public void setScene(final Scene newScene) {
321316
if (Toolkit.getToolkit().isFxUserThread()) {
322317
setSceneImpl(newScene);
323318
} else {
324-
@SuppressWarnings("removal")
325-
EventQueue eventQueue = AccessController.doPrivileged(
326-
(PrivilegedAction<EventQueue>) java.awt.Toolkit
327-
.getDefaultToolkit()::getSystemEventQueue);
319+
EventQueue eventQueue = java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue();
328320
SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop();
329321
Platform.runLater(() -> {
330322
try {
@@ -949,18 +941,14 @@ private void setFxEnabled(boolean enabled) {
949941
* method is invoked, the chain of parent components is set up with
950942
* KeyboardAction event listeners.
951943
*/
952-
@SuppressWarnings("removal")
953944
@Override
954945
public void addNotify() {
955946
super.addNotify();
956947

957948
registerFinishListener();
958949

959-
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
960-
JFXPanel.this.getToolkit().addAWTEventListener(ungrabListener,
961-
jfxPanelIOP.getMask());
962-
return null;
963-
});
950+
getToolkit().addAWTEventListener(ungrabListener, jfxPanelIOP.getMask());
951+
964952
updateComponentSize(); // see RT-23603
965953
SwingNodeHelper.runOnFxThread(() -> {
966954
if ((stage != null) && !stage.isShowing()) {
@@ -984,7 +972,6 @@ public InputMethodRequests getInputMethodRequests() {
984972
* When this method is invoked, any KeyboardActions set up in the the
985973
* chain of parent components are removed.
986974
*/
987-
@SuppressWarnings("removal")
988975
@Override public void removeNotify() {
989976
SwingNodeHelper.runOnFxThread(() -> {
990977
if ((stage != null) && stage.isShowing()) {
@@ -998,10 +985,7 @@ public InputMethodRequests getInputMethodRequests() {
998985

999986
super.removeNotify();
1000987

1001-
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
1002-
JFXPanel.this.getToolkit().removeAWTEventListener(ungrabListener);
1003-
return null;
1004-
});
988+
getToolkit().removeAWTEventListener(ungrabListener);
1005989

1006990
/* see CR 4867453 */
1007991
getInputContext().removeNotify(this);

modules/javafx.swing/src/main/java/javafx/embed/swing/SwingNode.java

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -55,8 +55,6 @@
5555
import javafx.stage.Window;
5656
import java.lang.ref.WeakReference;
5757
import java.nio.IntBuffer;
58-
import java.security.AccessController;
59-
import java.security.PrivilegedAction;
6058
import com.sun.javafx.embed.swing.Disposer;
6159
import com.sun.javafx.geom.BaseBounds;
6260
import com.sun.javafx.geom.transform.BaseTransform;
@@ -117,20 +115,9 @@
117115
* @since JavaFX 8.0
118116
*/
119117
public class SwingNode extends Node {
120-
private static boolean isThreadMerged;
118+
private static boolean isThreadMerged = Boolean.valueOf(System.getProperty("javafx.embed.singleThread"));
121119

122120
static {
123-
@SuppressWarnings("removal")
124-
var dummy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
125-
@Override
126-
public Object run() {
127-
isThreadMerged = Boolean.valueOf(
128-
System.getProperty("javafx.embed.singleThread"));
129-
return null;
130-
}
131-
});
132-
133-
134121
// This is used by classes in different packages to get access to
135122
// private and package private methods.
136123
SwingNodeHelper.setSwingNodeAccessor(new SwingNodeHelper.SwingNodeAccessor() {
@@ -562,12 +549,10 @@ private boolean doComputeContains(double localX, double localY) {
562549
locateLwFrame();
563550
};
564551

565-
@SuppressWarnings("removal")
566552
private final EventHandler<FocusUngrabEvent> ungrabHandler = event -> {
567553
if (!skipBackwardUnrgabNotification) {
568554
if (lwFrame != null) {
569-
AccessController.doPrivileged(new PostEventAction(
570-
swNodeIOP.createUngrabEvent(lwFrame)));
555+
postAWTEvent(swNodeIOP.createUngrabEvent(lwFrame));
571556
}
572557
}
573558
};
@@ -830,17 +815,9 @@ private void ungrabFocus(boolean postUngrabEvent) {
830815
}
831816
}
832817

833-
private class PostEventAction implements PrivilegedAction<Void> {
834-
private AWTEvent event;
835-
PostEventAction(AWTEvent event) {
836-
this.event = event;
837-
}
838-
@Override
839-
public Void run() {
840-
EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
841-
eq.postEvent(event);
842-
return null;
843-
}
818+
private static void postAWTEvent(AWTEvent event) {
819+
EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
820+
eq.postEvent(event);
844821
}
845822

846823
private class SwingMouseEventHandler implements EventHandler<MouseEvent> {
@@ -890,8 +867,7 @@ public void handle(MouseEvent event) {
890867
frame, swingID, swingWhen, swingModifiers,
891868
relX, relY, absX, absY,
892869
event.getClickCount(), swingPopupTrigger, swingButton);
893-
@SuppressWarnings("removal")
894-
var dummy = AccessController.doPrivileged(new PostEventAction(mouseEvent));
870+
postAWTEvent(mouseEvent);
895871
}
896872
}
897873

@@ -932,8 +908,7 @@ private void sendMouseWheelEvent(Object source, double fxX, double fxY, int swin
932908
int y = (int) Math.round(fxY);
933909
MouseWheelEvent mouseWheelEvent =
934910
swNodeIOP.createMouseWheelEvent(source, swingModifiers, x, y, -wheelRotation);
935-
@SuppressWarnings("removal")
936-
var dummy = AccessController.doPrivileged(new PostEventAction(mouseWheelEvent));
911+
postAWTEvent(mouseWheelEvent);
937912
}
938913
}
939914

@@ -978,10 +953,7 @@ public void handle(KeyEvent event) {
978953
java.awt.event.KeyEvent keyEvent = swNodeIOP.createKeyEvent(frame,
979954
swingID, swingWhen, swingModifiers, swingKeyCode,
980955
swingChar);
981-
@SuppressWarnings("removal")
982-
var dummy = AccessController.doPrivileged(new PostEventAction(keyEvent));
956+
postAWTEvent(keyEvent);
983957
}
984958
}
985959
}
986-
987-

0 commit comments

Comments
 (0)