Ghidra is a software reverse engineering (SRE) framework
GT-3457 - review fixes
dragonmacher89a65406e875
diff --git a/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java b/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.javaindex ea1ee84b70..834c6c5278 100644--- a/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java+++ b/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java@@ -38,7 +38,11 @@ public class ExpiringSwingTimer extends GhidraSwingTimer {private AtomicBoolean didRun = new AtomicBoolean();/**- * Runs the given client runnable when the given condition returns true+ * Runs the given client runnable when the given condition returns true. The returned timer+ * will be running.+ *+ * <p>Once the timer has performed the work, any calls to start the returned timer will+ * not perform any work. You can check {@link #didRun()} to see if the work has been completed.** @param isReady true if the code should be run* @param expireMs the amount of time past which the code will not be run@@ -54,7 +58,6 @@ public class ExpiringSwingTimer extends GhidraSwingTimer {int delay = 250;ExpiringSwingTimer timer =new ExpiringSwingTimer(delay, expireMs, isReady, runnable);- timer.setRepeats(true);timer.start();return timer;}@@ -62,6 +65,9 @@ public class ExpiringSwingTimer extends GhidraSwingTimer {/*** Constructor*+ * <p>Note: this class sets the parent's initial delay to 0. This is to allow the client+ * code to be executed without delay when the ready condition is true.+ ** @param delay the delay between calls to check <code>isReady</code>* @param isReady true if the code should be run* @param expireMs the amount of time past which the code will not be run@@ -69,21 +75,41 @@ public class ExpiringSwingTimer extends GhidraSwingTimer {*/public ExpiringSwingTimer(int delay, int expireMs, BooleanSupplier isReady,Runnable runnable) {- super(delay, null);+ super(0, delay, null);this.expireMs = expireMs;this.isReady = isReady;this.clientCallback = () -> runnable.run();super.setTimerCallback(expiringTimerCallback);+ setRepeats(true);}/**- * Returns true if the client runnable was run+ * Returns true if the client runnable was run* @return true if the client runnable was run*/public boolean didRun() {return didRun.get();}+ @Override+ public void start() {+ if (didRun() || isExpired()) {+ return;+ }++ super.start();+ }++ /**+ * Returns true the initial expiration period has passed+ * @return true if expired+ */+ public boolean isExpired() {+ long now = System.currentTimeMillis();+ int elapsed = (int) (now - startMs);+ return elapsed > expireMs;+ }+@Overridepublic void setTimerCallback(TimerCallback callback) {// overridden to ensure clients cannot overwrite out wrapping callback@@ -95,20 +121,15 @@ public class ExpiringSwingTimer extends GhidraSwingTimer {@Overridepublic void timerFired() {- long now = System.currentTimeMillis();- int passed = (int) (now - startMs);- if (passed > expireMs) {- timer.stop();+ if (isReady.getAsBoolean()) {+ clientCallback.timerFired();+ didRun.set(true);+ stop();return;}-- if (!isReady.getAsBoolean()) {- return;+ else if (isExpired()) {+ stop();}-- clientCallback.timerFired();- stop();- didRun.set(true);}}}diff --git a/Ghidra/Framework/Generic/src/main/java/generic/timer/GhidraSwingTimer.java b/Ghidra/Framework/Generic/src/main/java/generic/timer/GhidraSwingTimer.javaindex f66fcb68eb..e655bffd50 100644--- a/Ghidra/Framework/Generic/src/main/java/generic/timer/GhidraSwingTimer.java+++ b/Ghidra/Framework/Generic/src/main/java/generic/timer/GhidraSwingTimer.java@@ -36,7 +36,7 @@ public class GhidraSwingTimer implements GhidraTimer, ActionListener {public GhidraSwingTimer(int initialDelay, int delay, TimerCallback callback) {this.callback = callback;timer = new Timer(delay, this);- timer.setInitialDelay(delay);+ timer.setInitialDelay(initialDelay);}@Overridediff --git a/Ghidra/Framework/Generic/src/test/java/generic/timer/ExpiringSwingTimerTest.java b/Ghidra/Framework/Generic/src/test/java/generic/timer/ExpiringSwingTimerTest.javaindex 03eeed23c6..849d4ac806 100644--- a/Ghidra/Framework/Generic/src/test/java/generic/timer/ExpiringSwingTimerTest.java+++ b/Ghidra/Framework/Generic/src/test/java/generic/timer/ExpiringSwingTimerTest.java@@ -62,4 +62,25 @@ public class ExpiringSwingTimerTest extends AbstractGenericTest {assertFalse(didRun.get());}++ @Test+ public void testWorkOnlyHappensOnce() {++ BooleanSupplier isReady = () -> {+ return true;+ };++ AtomicInteger runCount = new AtomicInteger();+ Runnable r = () -> {+ runCount.incrementAndGet();+ };++ ExpiringSwingTimer timer = ExpiringSwingTimer.runWhen(isReady, 10000, r);+ waitFor(() -> !timer.isRunning());+ assertEquals(1, runCount.get());++ timer.start();+ waitFor(() -> !timer.isRunning());+ assertEquals(1, runCount.get());+ }}