From 57ae1f30b2c6e8f705eea796cd3b36932049b5aa Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Thu, 3 Jan 2013 14:04:20 +0100 Subject: SI-6905 - Switch to sneakyThrows instead of Unsafe.throwException as per new jsr166y to avoid issues with Android --- .../scala/concurrent/forkjoin/ForkJoinPool.java | 2 +- .../scala/concurrent/forkjoin/ForkJoinTask.java | 30 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java index 65654be69b..8dbca6da4b 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java +++ b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinPool.java @@ -1372,7 +1372,7 @@ public class ForkJoinPool extends AbstractExecutorService { } if (ex != null) // rethrow - U.throwException(ex); + ForkJoinTask.rethrow(ex); } diff --git a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java index 15c60118b3..839fd26b39 100644 --- a/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java +++ b/src/forkjoin/scala/concurrent/forkjoin/ForkJoinTask.java @@ -595,6 +595,30 @@ public abstract class ForkJoinTask implements Future, Serializable { } } } + + /** + * A version of "sneaky throw" to relay exceptions + */ + static void rethrow(final Throwable ex) { + if (ex != null) { + if (ex instanceof Error) + throw (Error)ex; + if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + ForkJoinTask.uncheckedThrow(ex); + } + } + + /** + * The sneaky part of sneaky throw, relying on generics + * limitations to evade compiler complaints about rethrowing + * unchecked exceptions + */ + @SuppressWarnings("unchecked") static + void uncheckedThrow(Throwable t) throws T { + if (t != null) + throw (T)t; // rely on vacuous cast + } /** * Throws exception, if any, associated with the given status. @@ -604,7 +628,7 @@ public abstract class ForkJoinTask implements Future, Serializable { (s == EXCEPTIONAL) ? getThrowableException() : null); if (ex != null) - U.throwException(ex); + ForkJoinTask.rethrow(ex); } // public methods @@ -742,7 +766,7 @@ public abstract class ForkJoinTask implements Future, Serializable { } } if (ex != null) - U.throwException(ex); + ForkJoinTask.rethrow(ex); } /** @@ -799,7 +823,7 @@ public abstract class ForkJoinTask implements Future, Serializable { } } if (ex != null) - U.throwException(ex); + ForkJoinTask.rethrow(ex); return tasks; } -- cgit v1.2.3