Skip to content

Commit a87f0a5

Browse files
author
Andy Goryachev
committedMar 14, 2025
8351067: Enforce Platform threading use
Reviewed-by: kcr, mstrauss
1 parent d2ab2c8 commit a87f0a5

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed
 

‎modules/javafx.graphics/src/main/java/javafx/application/Platform.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025, 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
@@ -413,6 +413,7 @@ public static boolean canStartNestedEventLoop() {
413413
private static ReadOnlyBooleanWrapper accessibilityActiveProperty;
414414

415415
public static boolean isAccessibilityActive() {
416+
Toolkit.getToolkit().checkFxUserThread();
416417
return accessibilityActiveProperty == null ? false : accessibilityActiveProperty.get();
417418
}
418419

@@ -421,14 +422,17 @@ public static boolean isAccessibilityActive() {
421422
* This property is typically set to true the first time an
422423
* assistive technology, such as a screen reader, requests
423424
* information about any JavaFX window or its children.
424-
*
425-
* <p>This method may be called from any thread.</p>
425+
* <p>
426+
* This property can be accessed only from the JavaFX Application Thread.
426427
*
427428
* @return the read-only boolean property indicating if accessibility is active
428429
*
430+
* @throws IllegalStateException if this method is called on a thread
431+
* other than the JavaFX Application Thread.
429432
* @since JavaFX 8u40
430433
*/
431434
public static ReadOnlyBooleanProperty accessibilityActiveProperty() {
435+
Toolkit.getToolkit().checkFxUserThread();
432436
if (accessibilityActiveProperty == null) {
433437
accessibilityActiveProperty = new ReadOnlyBooleanWrapper(Platform.class, "accessibilityActive");
434438
accessibilityActiveProperty.bind(PlatformImpl.accessibilityActiveProperty());
@@ -445,12 +449,15 @@ public static ReadOnlyBooleanProperty accessibilityActiveProperty() {
445449
* by JavaFX when the operating system reports that a platform preference has changed.
446450
*
447451
* @return the {@code Preferences} instance
452+
* @throws IllegalStateException if this method is called on a thread
453+
* other than the JavaFX Application Thread.
448454
* @see <a href="Platform.Preferences.html#preferences-table-windows">Windows preferences</a>
449455
* @see <a href="Platform.Preferences.html#preferences-table-macos">macOS preferences</a>
450456
* @see <a href="Platform.Preferences.html#preferences-table-linux">Linux preferences</a>
451457
* @since 22
452458
*/
453459
public static Preferences getPreferences() {
460+
Toolkit.getToolkit().checkFxUserThread();
454461
PlatformImpl.checkPreferencesSupport();
455462
return PlatformImpl.getPlatformPreferences();
456463
}

‎tests/system/src/test/java/test/javafx/application/PlatformTest.java

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, 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
@@ -25,24 +25,21 @@
2525

2626
package test.javafx.application;
2727

28-
import org.junit.jupiter.api.AfterAll;
29-
import org.junit.jupiter.api.BeforeAll;
30-
import org.junit.jupiter.api.Test;
31-
import test.util.Util;
32-
33-
import javafx.animation.KeyFrame;
34-
import javafx.animation.Timeline;
35-
import javafx.util.Duration;
36-
import javafx.application.Platform;
37-
38-
import java.util.concurrent.CountDownLatch;
39-
import java.util.concurrent.atomic.AtomicReference;
40-
4128
import static org.junit.jupiter.api.Assertions.assertEquals;
4229
import static org.junit.jupiter.api.Assertions.assertFalse;
4330
import static org.junit.jupiter.api.Assertions.assertNull;
44-
import static org.junit.jupiter.api.Assertions.assertTrue;
4531
import static org.junit.jupiter.api.Assertions.assertThrows;
32+
import static org.junit.jupiter.api.Assertions.assertTrue;
33+
import java.util.concurrent.CountDownLatch;
34+
import java.util.concurrent.atomic.AtomicReference;
35+
import javafx.animation.KeyFrame;
36+
import javafx.animation.Timeline;
37+
import javafx.application.Platform;
38+
import javafx.util.Duration;
39+
import org.junit.jupiter.api.AfterAll;
40+
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.Test;
42+
import test.util.Util;
4643

4744
public class PlatformTest {
4845

@@ -128,4 +125,10 @@ public void testCanNotStartNestedEventLoopInTimeline() {
128125
assertNull(exceptionRef.get(), exceptionRef::toString);
129126
}
130127

128+
@Test
129+
public void testAccessorsNotOnFxThread() {
130+
assertThrows(IllegalStateException.class, Platform::accessibilityActiveProperty);
131+
assertThrows(IllegalStateException.class, Platform::getPreferences);
132+
assertThrows(IllegalStateException.class, Platform::isAccessibilityActive);
133+
}
131134
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Mar 14, 2025

@openjdk-notifier[bot]
Please sign in to comment.