From f367b9bccad668e508214429e66b1865ee600913 Mon Sep 17 00:00:00 2001 From: Sean Burns Date: Wed, 24 Feb 2021 14:11:32 +0000 Subject: [PATCH 1/2] Do the entire search in a single event on the swing thread rather queue hundreds possible millions of blocking events for the swing thread. --- .../assertj/swing/core/FinderDelegate.java | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java b/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java index 5a175f4e..b63ef88f 100644 --- a/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java +++ b/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java @@ -19,6 +19,7 @@ import java.awt.Component; import java.util.Collection; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nonnull; @@ -35,11 +36,15 @@ final class FinderDelegate { @RunsInEDT @Nonnull Collection find(@Nonnull ComponentHierarchy h, @Nonnull ComponentMatcher m) { - Set found = newLinkedHashSet(); - for (Component c : rootsOf(h)) { - find(h, m, checkNotNull(c), found); - } - return found; + AtomicReference> responseReference = new AtomicReference<>(); + execute(() -> { + Set found = newLinkedHashSet(); + for (Component c : rootsOf(h)) { + find(h, m, checkNotNull(c), found); + } + responseReference.set(found); + }); + return responseReference.get(); } @RunsInEDT @@ -56,29 +61,33 @@ private void find(@Nonnull ComponentHierarchy h, @Nonnull ComponentMatcher m, @N @RunsInEDT @Nonnull private static Collection childrenOfComponent(final @Nonnull Component c, final @Nonnull ComponentHierarchy h) { - Collection children = execute(() -> h.childrenOf(c)); + Collection children = h.childrenOf(c); return checkNotNull(children); } @RunsInEDT private static boolean isMatching(@Nonnull final Component c, @Nonnull final ComponentMatcher m) { - Boolean matching = execute(() -> m.matches(c)); + Boolean matching = m.matches(c); return checkNotNull(matching); } @RunsInEDT @Nonnull Collection find(@Nonnull ComponentHierarchy h, @Nonnull GenericTypeMatcher m) { - Set found = newLinkedHashSet(); - for (Component c : rootsOf(h)) { - find(h, m, checkNotNull(c), found); - } - return found; + AtomicReference> responseReference = new AtomicReference<>(); + execute(() -> { + Set found = newLinkedHashSet(); + for (Component c : rootsOf(h)) { + find(h, m, checkNotNull(c), found); + } + responseReference.set(found); + }); + return responseReference.get(); } @RunsInEDT @Nonnull private static Collection rootsOf(final @Nonnull ComponentHierarchy h) { - return checkNotNull(execute(() -> h.roots())); + return checkNotNull(h.roots()); } @RunsInEDT @@ -95,7 +104,7 @@ private void find(@Nonnull ComponentHierarchy h, @Nonnull @RunsInEDT private static boolean isMatching(final @Nonnull Component c, final @Nonnull GenericTypeMatcher m) { - Boolean matching = execute(() -> m.matches(c)); + Boolean matching = m.matches(c); return checkNotNull(matching); } } From 99e14065d3d55b8d01a75a2e228d242210e32195 Mon Sep 17 00:00:00 2001 From: Sean Burns Date: Thu, 25 Feb 2021 08:58:36 +0000 Subject: [PATCH 2/2] Use effectively final instead of atomic reference. --- .../java/org/assertj/swing/core/FinderDelegate.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java b/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java index b63ef88f..e841b8c0 100644 --- a/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java +++ b/assertj-swing/src/main/java/org/assertj/swing/core/FinderDelegate.java @@ -19,7 +19,6 @@ import java.awt.Component; import java.util.Collection; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nonnull; @@ -36,15 +35,13 @@ final class FinderDelegate { @RunsInEDT @Nonnull Collection find(@Nonnull ComponentHierarchy h, @Nonnull ComponentMatcher m) { - AtomicReference> responseReference = new AtomicReference<>(); + Set found = newLinkedHashSet(); execute(() -> { - Set found = newLinkedHashSet(); for (Component c : rootsOf(h)) { find(h, m, checkNotNull(c), found); } - responseReference.set(found); }); - return responseReference.get(); + return found; } @RunsInEDT @@ -74,15 +71,13 @@ private static boolean isMatching(@Nonnull final Component c, @Nonnull final Com @RunsInEDT @Nonnull Collection find(@Nonnull ComponentHierarchy h, @Nonnull GenericTypeMatcher m) { - AtomicReference> responseReference = new AtomicReference<>(); + Set found = newLinkedHashSet(); execute(() -> { - Set found = newLinkedHashSet(); for (Component c : rootsOf(h)) { find(h, m, checkNotNull(c), found); } - responseReference.set(found); }); - return responseReference.get(); + return found; } @RunsInEDT