From e9a71827edb1afa90bd7fb3cc51dd80589220d8c Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Fri, 27 May 2022 01:36:10 +0200 Subject: [PATCH] Apply suggestions from code review Establishing symmetry with `afterExecuteTask` Co-authored-by: Sebastian Stenzel --- .../cryptomator/common/CatchingExecutors.java | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/cryptomator/common/CatchingExecutors.java b/src/main/java/org/cryptomator/common/CatchingExecutors.java index 59bacbed7..469fc3741 100644 --- a/src/main/java/org/cryptomator/common/CatchingExecutors.java +++ b/src/main/java/org/cryptomator/common/CatchingExecutors.java @@ -49,16 +49,12 @@ public final class CatchingExecutors { } private static void afterExecute0(Runnable runnable, Throwable throwable) { - if (throwable == null) { - if (runnable instanceof Task) { - afterExecuteTask(Thread.currentThread(), (Task) runnable); - return; - } - throwable = getThrowable(runnable); - } - if (throwable != null) { callHandler(Thread.currentThread(), throwable); + } else if (runnable instanceof Task t) { + afterExecuteTask(Thread.currentThread(), t); + } else if (runnable instanceof Future f) { + afterExecuteFuture(f); } //Errors in this method are delegated to the UncaughtExceptionHandler of the current thread } @@ -79,21 +75,17 @@ public final class CatchingExecutors { }); } - private static Throwable getThrowable(Runnable runnable) { - assert !(runnable instanceof Task); - - if (runnable instanceof Future && ((Future) runnable).isDone()) { - try { - ((Future) runnable).get(); - } catch (CancellationException ce) { - return ce; - } catch (ExecutionException ee) { - return ee.getCause(); - } catch (InterruptedException ie) { - //Ignore/Reset - Thread.currentThread().interrupt(); - } + private static void afterExecuteFuture(Future future) { + assert future.isDone(); + try { + future.get(); + } catch (CancellationException ce) { + callHandler(Thread.currentThread(), ce); + } catch (ExecutionException ee) { + callHandler(Thread.currentThread(), ee.getCause()); + } catch (InterruptedException ie) { + //Ignore/Reset + Thread.currentThread().interrupt(); } - return null; } } \ No newline at end of file