diff --git a/akka-actor/src/main/scala/akka/actor/Actor.scala b/akka-actor/src/main/scala/akka/actor/Actor.scala index 1c94d4304d..ccf8b3c6c8 100644 --- a/akka-actor/src/main/scala/akka/actor/Actor.scala +++ b/akka-actor/src/main/scala/akka/actor/Actor.scala @@ -16,7 +16,6 @@ import akka.AkkaException import scala.reflect.BeanProperty import scala.util.control.NoStackTrace import com.eaio.uuid.UUID -import java.lang.reflect.InvocationTargetException import java.util.concurrent.TimeUnit import java.util.{ Collection ⇒ JCollection } import java.util.regex.Pattern diff --git a/akka-actor/src/main/scala/akka/actor/ActorSystem.scala b/akka-actor/src/main/scala/akka/actor/ActorSystem.scala index 203c8e3510..5e6f2e72e4 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorSystem.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorSystem.scala @@ -19,7 +19,6 @@ import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigParseOptions import com.typesafe.config.ConfigResolveOptions import com.typesafe.config.ConfigException -import java.lang.reflect.InvocationTargetException import akka.util.{ Helpers, Duration, ReflectiveAccess } import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.{ CountDownLatch, Executors, ConcurrentHashMap } @@ -409,9 +408,8 @@ class ActorSystemImpl(val name: String, applicationConfig: Config) extends Actor val values: Array[AnyRef] = arguments map (_._2) toArray ReflectiveAccess.createInstance[ActorRefProvider](providerClass, types, values) match { - case Left(e: InvocationTargetException) ⇒ throw e.getTargetException - case Left(e) ⇒ throw e - case Right(p) ⇒ p + case Left(e) ⇒ throw e + case Right(p) ⇒ p } } diff --git a/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala b/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala index d9951c61e8..1691713ea3 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala @@ -11,7 +11,6 @@ import java.util.concurrent._ import annotation.tailrec import akka.event.Logging.Error import com.typesafe.config.Config -import java.lang.reflect.InvocationTargetException import akka.actor.ActorContext class MessageQueueAppendFailedException(message: String, cause: Throwable = null) extends AkkaException(message, cause) @@ -393,12 +392,8 @@ class CustomMailboxType(mailboxFQN: String) extends MailboxType { ReflectiveAccess.createInstance[AnyRef](mailboxFQN, constructorSignature, Array[AnyRef](receiver)) match { case Right(instance) ⇒ instance.asInstanceOf[Mailbox] case Left(exception) ⇒ - val cause = exception match { - case i: InvocationTargetException ⇒ i.getTargetException - case _ ⇒ exception - } throw new IllegalArgumentException("Cannot instantiate mailbox [%s] due to: %s". - format(mailboxFQN, cause.toString)) + format(mailboxFQN, exception.toString)) } } diff --git a/akka-actor/src/main/scala/akka/routing/Routing.scala b/akka-actor/src/main/scala/akka/routing/Routing.scala index b047af5888..0ce9e9adfb 100644 --- a/akka-actor/src/main/scala/akka/routing/Routing.scala +++ b/akka-actor/src/main/scala/akka/routing/Routing.scala @@ -5,7 +5,6 @@ package akka.routing import akka.actor._ import akka.japi.Creator -import java.lang.reflect.InvocationTargetException import akka.config.ConfigurationException import java.util.concurrent.atomic.AtomicInteger import akka.util.{ ReflectiveAccess, Timeout } diff --git a/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala b/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala index 065e11ba78..0e738541ae 100644 --- a/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala +++ b/akka-actor/src/main/scala/akka/util/ReflectiveAccess.scala @@ -5,6 +5,7 @@ package akka.util import akka.actor._ +import java.lang.reflect.InvocationTargetException object ReflectiveAccess { @@ -14,22 +15,19 @@ object ReflectiveAccess { def createInstance[T](clazz: Class[_], params: Array[Class[_]], - args: Array[AnyRef]): Either[Exception, T] = try { + args: Array[AnyRef]): Either[Exception, T] = withErrorHandling { assert(clazz ne null) assert(params ne null) assert(args ne null) val ctor = clazz.getDeclaredConstructor(params: _*) ctor.setAccessible(true) Right(ctor.newInstance(args: _*).asInstanceOf[T]) - } catch { - case e: Exception ⇒ - Left(e) } def createInstance[T](fqn: String, params: Array[Class[_]], args: Array[AnyRef], - classloader: ClassLoader = loader): Either[Exception, T] = try { + classloader: ClassLoader = loader): Either[Exception, T] = withErrorHandling { assert(params ne null) assert(args ne null) getClassFor(fqn, classloader) match { @@ -39,9 +37,6 @@ object ReflectiveAccess { Right(ctor.newInstance(args: _*).asInstanceOf[T]) case Left(exception) ⇒ Left(exception) //We could just cast this to Either[Exception, T] but it's ugly } - } catch { - case e: Exception ⇒ - Left(e) } //Obtains a reference to fqn.MODULE$ @@ -100,5 +95,23 @@ object ReflectiveAccess { case e: Exception ⇒ Left(e) } + /** + * Caught exception is returned as Left(exception). + * Unwraps `InvocationTargetException` if its getTargetException is an `Exception`. + * Other `Throwable`, such as `Error` is thrown. + */ + private def withErrorHandling[T](body: ⇒ Either[Exception, T]): Either[Exception, T] = { + try { + body + } catch { + case e: InvocationTargetException ⇒ e.getTargetException match { + case t: Exception ⇒ Left(t) + case t ⇒ throw t + } + case e: Exception ⇒ + Left(e) + } + } + } diff --git a/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala b/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala index f94dfe83ca..c8f9026cbf 100644 --- a/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala +++ b/akka-durable-mailboxes/akka-mailboxes-common/src/main/scala/akka/actor/mailbox/DurableMailbox.scala @@ -4,7 +4,6 @@ package akka.actor.mailbox import akka.util.ReflectiveAccess -import java.lang.reflect.InvocationTargetException import akka.AkkaException import akka.actor.ActorContext import akka.actor.ActorRef