Simplify Throwables.rethrow by using type erasure

https://blog.jooq.org/2012/09/14/throw-checked-exceptions-like-runtime-exceptions-in-java/


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9ec2b5be
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9ec2b5be
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9ec2b5be

Branch: refs/heads/LOG4J-1181
Commit: 9ec2b5bee38c4913b520211c2cb9105b53f65c93
Parents: 3aa04f9
Author: Matt Sicker <[email protected]>
Authored: Mon Jun 6 19:24:09 2016 -0500
Committer: Matt Sicker <[email protected]>
Committed: Mon Jun 6 19:24:09 2016 -0500

----------------------------------------------------------------------
 .../logging/log4j/core/util/Throwables.java     | 23 ++++++++------------
 .../logging/log4j/core/util/ThrowablesTest.java |  4 +---
 2 files changed, 10 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9ec2b5be/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Throwables.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Throwables.java 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Throwables.java
index 4f825e6..5f006a0 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Throwables.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Throwables.java
@@ -22,7 +22,6 @@ import java.io.LineNumberReader;
 import java.io.PrintWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
-import java.lang.reflect.UndeclaredThrowableException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -50,7 +49,7 @@ public final class Throwables {
 
     /**
      * Returns the deepest cause of the given {@code throwable}.
-     * 
+     *
      * @param throwable the throwable to navigate
      * @return the deepest throwable or the given throwable
      */
@@ -79,7 +78,7 @@ public final class Throwables {
 
     /**
      * Returns true if the getSuppressed method is available.
-     * 
+     *
      * @return True if getSuppressed is available. As of 2.4, always returns 
true.
      * @deprecated Will be removed in 2.5. As of 2.4, always returns true.
      */
@@ -123,21 +122,17 @@ public final class Throwables {
     }
 
     /**
-     * Rethrows a {@link Throwable}, wrapping checked exceptions into an 
{@link UndeclaredThrowableException}.
+     * Rethrows a {@link Throwable}.
      *
      * @param t the Throwable to throw.
-     * @throws RuntimeException if {@code t} is a RuntimeException
-     * @throws Error if {@code t} is an Error
-     * @throws UndeclaredThrowableException if {@code t} is a checked Exception
      * @since 2.1
      */
     public static void rethrow(final Throwable t) {
-        if (t instanceof RuntimeException) {
-            throw (RuntimeException) t;
-        }
-        if (t instanceof Error) {
-            throw (Error) t;
-        }
-        throw new UndeclaredThrowableException(t);
+        Throwables.<RuntimeException>rethrow0(t);
+    }
+
+    @SuppressWarnings("unchecked")
+    private static <T extends Throwable> void rethrow0(final Throwable t) 
throws T {
+        throw (T) t;
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9ec2b5be/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ThrowablesTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ThrowablesTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ThrowablesTest.java
index 50d462c..e8f82f5 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ThrowablesTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ThrowablesTest.java
@@ -16,8 +16,6 @@
  */
 package org.apache.logging.log4j.core.util;
 
-import java.lang.reflect.UndeclaredThrowableException;
-
 import org.junit.Test;
 
 public class ThrowablesTest {
@@ -51,7 +49,7 @@ public class ThrowablesTest {
         Throwables.rethrow(new UnknownError());
     }
 
-    @Test(expected = UndeclaredThrowableException.class)
+    @Test(expected = NoSuchMethodException.class)
     public void testRethrowCheckedException() throws Exception {
         Throwables.rethrow(new NoSuchMethodException());
     }

Reply via email to