Skip to content

Commit df28cec

Browse files
author
Alisen Chung
committed
8345144: Robot does not specify all causes of IllegalThreadStateException
Reviewed-by: prr
1 parent 9f3c5f9 commit df28cec

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/java.desktop/share/classes/java/awt/Robot.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
* queue. For example, {@code Robot.mouseMove} will actually move
5959
* the mouse cursor instead of just generating mouse move events.
6060
* <p>
61+
* @apiNote When {@code autoWaitForIdle()} is enabled, mouse and key related methods
62+
* cannot be called on the AWT EDT. This is because when {@code autoWaitForIdle()}
63+
* is enabled, the mouse and key methods implicitly call {@code waitForIdle()}
64+
* which will throw {@code IllegalThreadStateException} when called on the AWT EDT.
65+
* In addition, screen capture operations can be lengthy
66+
* and {@code delay(long ms)} clearly inserts a delay, so these also
67+
* should not be called on the EDT. Taken together, this means that as much as possible,
68+
* methods on this class should not be called on the EDT.
69+
* <p>
6170
* Note that some platforms require special privileges or extensions
6271
* to access low-level input control. If the current platform configuration
6372
* does not allow input control, an {@code AWTException} will be thrown
@@ -216,6 +225,8 @@ private static void checkIsScreenDevice(GraphicsDevice device) {
216225
*
217226
* @param x X position
218227
* @param y Y position
228+
* @throws IllegalThreadStateException if called on the AWT event dispatching
229+
* thread and {@code isAutoWaitForIdle} would return true
219230
*/
220231
public synchronized void mouseMove(int x, int y) {
221232
peer.mouseMove(x, y);
@@ -268,6 +279,7 @@ public synchronized void mouseMove(int x, int y) {
268279
* and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java
269280
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button
270281
* that does not exist on the mouse and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java
282+
* @throws IllegalThreadStateException if called on the AWT event dispatching thread and {@code isAutoWaitForIdle} would return true
271283
* @see #mouseRelease(int)
272284
* @see InputEvent#getMaskForButton(int)
273285
* @see Toolkit#areExtraMouseButtonsEnabled()
@@ -325,6 +337,7 @@ public synchronized void mousePress(int buttons) {
325337
* and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java
326338
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button
327339
* that does not exist on the mouse and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java
340+
* @throws IllegalThreadStateException if called on the AWT event dispatching thread and {@code isAutoWaitForIdle} would return true
328341
* @see #mousePress(int)
329342
* @see InputEvent#getMaskForButton(int)
330343
* @see Toolkit#areExtraMouseButtonsEnabled()
@@ -349,6 +362,8 @@ private static void checkButtonsArgument(int buttons) {
349362
* @param wheelAmt number of "notches" to move the mouse wheel
350363
* Negative values indicate movement up/away from the user,
351364
* positive values indicate movement down/towards the user.
365+
* @throws IllegalThreadStateException if called on the AWT event dispatching
366+
* thread and {@code isAutoWaitForIdle} would return true
352367
*
353368
* @since 1.4
354369
*/
@@ -368,6 +383,8 @@ public synchronized void mouseWheel(int wheelAmt) {
368383
* @param keycode Key to press (e.g. {@code KeyEvent.VK_A})
369384
* @throws IllegalArgumentException if {@code keycode} is not
370385
* a valid key
386+
* @throws IllegalThreadStateException if called on the AWT event
387+
* dispatching thread and {@code isAutoWaitForIdle} would return true
371388
* @see #keyRelease(int)
372389
* @see java.awt.event.KeyEvent
373390
*/
@@ -387,7 +404,9 @@ public synchronized void keyPress(int keycode) {
387404
* @param keycode Key to release (e.g. {@code KeyEvent.VK_A})
388405
* @throws IllegalArgumentException if {@code keycode} is not a
389406
* valid key
390-
* @see #keyPress(int)
407+
* @throws IllegalThreadStateException if called on the AWT event
408+
* dispatching thread and {@code isAutoWaitForIdle} would return true
409+
* @see #keyPress(int)
391410
* @see java.awt.event.KeyEvent
392411
*/
393412
public synchronized void keyRelease(int keycode) {
@@ -487,6 +506,12 @@ public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
487506
* nativeResImage = resolutionVariants.get(0);
488507
* }
489508
* }</pre>
509+
*
510+
* @apiNote It is recommended to avoid calling this method on
511+
* the AWT Event Dispatch Thread since screen capture may be a lengthy
512+
* operation, particularly if acquiring permissions is needed and involves
513+
* user interaction.
514+
*
490515
* @param screenRect Rect to capture in screen coordinates
491516
* @return The captured image
492517
* @throws IllegalArgumentException if {@code screenRect} width and height
@@ -641,6 +666,10 @@ public synchronized boolean isAutoWaitForIdle() {
641666
/**
642667
* Sets whether this Robot automatically invokes {@code waitForIdle}
643668
* after generating an event.
669+
* <p>
670+
* @apiNote Setting this to true means you cannot call mouse and key-controlling events
671+
* on the AWT Event Dispatching Thread
672+
*
644673
* @param isOn Whether {@code waitForIdle} is automatically invoked
645674
*/
646675
public synchronized void setAutoWaitForIdle(boolean isOn) {
@@ -692,6 +721,10 @@ private void autoDelay() {
692721
* already set, this method returns immediately with the interrupt status
693722
* set.
694723
*
724+
* @apiNote It is recommended to avoid calling this method on
725+
* the AWT Event Dispatch Thread since delay may be a lengthy
726+
* operation.
727+
*
695728
* @param ms time to sleep in milliseconds
696729
* @throws IllegalArgumentException if {@code ms} is not between {@code 0}
697730
* and {@code 60,000} milliseconds inclusive

0 commit comments

Comments
 (0)