diff options
author | Valerij Fredriksen <freva@users.noreply.github.com> | 2022-05-30 09:24:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-30 09:24:53 +0200 |
commit | 29bbcf6e2c3cf7133a1109c51d93fedc7e493b97 (patch) | |
tree | f1eb4b6c9c08fc492d9568e7d3296f7b2cc58e16 /yolean | |
parent | 9aebfc50b7f0f967bb68e5e6986bc03afc469bd5 (diff) |
Revert "uncheck() all checked exceptions"
Diffstat (limited to 'yolean')
6 files changed, 39 insertions, 92 deletions
diff --git a/yolean/abi-spec.json b/yolean/abi-spec.json index 568535fbf39..8c10006e7e1 100644 --- a/yolean/abi-spec.json +++ b/yolean/abi-spec.json @@ -1,5 +1,5 @@ { - "com.yahoo.yolean.Exceptions$RunnableThrowingException": { + "com.yahoo.yolean.Exceptions$RunnableThrowingIOException": { "superClass": "java.lang.Object", "interfaces": [], "attributes": [ @@ -25,7 +25,7 @@ ], "fields": [] }, - "com.yahoo.yolean.Exceptions$SupplierThrowingException": { + "com.yahoo.yolean.Exceptions$SupplierThrowingIOException": { "superClass": "java.lang.Object", "interfaces": [], "attributes": [ @@ -48,33 +48,20 @@ "public void <init>()", "public static java.lang.String toMessageString(java.lang.Throwable)", "public static java.util.Optional findCause(java.lang.Throwable, java.lang.Class)", - "public static void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingException)", + "public static void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingIOException)", "public static void uncheckInterrupted(com.yahoo.yolean.Exceptions$RunnableThrowingInterruptedException)", "public static void uncheckInterruptedAndRestoreFlag(com.yahoo.yolean.Exceptions$RunnableThrowingInterruptedException)", - "public static varargs void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingException, java.lang.String, java.lang.String[])", - "public static void uncheckAndIgnore(com.yahoo.yolean.Exceptions$RunnableThrowingException, java.lang.Class)", - "public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingException)", - "public static varargs java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingException, java.lang.String, java.lang.String[])", - "public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingException, java.lang.Class)", + "public static varargs void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.String, java.lang.String[])", + "public static void uncheckAndIgnore(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.Class)", + "public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException)", + "public static varargs java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.String, java.lang.String[])", + "public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)", "public static java.lang.RuntimeException throwUnchecked(java.lang.Throwable)" ], "fields": [] }, - "com.yahoo.yolean.UncheckedException": { - "superClass": "java.lang.RuntimeException", - "interfaces": [], - "attributes": [ - "public" - ], - "methods": [ - "public void <init>(java.lang.String, java.lang.Exception)", - "public void <init>(java.lang.Exception)", - "public void <init>(java.lang.String)" - ], - "fields": [] - }, "com.yahoo.yolean.UncheckedInterruptedException": { - "superClass": "com.yahoo.yolean.UncheckedException", + "superClass": "java.lang.RuntimeException", "interfaces": [], "attributes": [ "public" diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java index 9776bdb54a8..89b4e76368b 100644 --- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java +++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java @@ -62,26 +62,22 @@ public class Exceptions { /** * Wraps any IOException thrown from a runnable in an UncheckedIOException. */ - public static void uncheck(RunnableThrowingException runnable) { + public static void uncheck(RunnableThrowingIOException runnable) { try { runnable.run(); } catch (IOException e) { throw new UncheckedIOException(e); - } catch (InterruptedException e) { - throw new UncheckedInterruptedException(e); - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException) e; - throw new UncheckedException(e); } } - /** Use {@link #uncheck(RunnableThrowingException)} instead */ - @Deprecated(since="7", forRemoval = true) public static void uncheckInterrupted(RunnableThrowingInterruptedException runnable) { - uncheck(runnable::run); + try { + runnable.run(); + } catch (InterruptedException e) { + throw new UncheckedInterruptedException(e, false); + } } - @Deprecated(since="7", forRemoval = true) public static void uncheckInterruptedAndRestoreFlag(RunnableThrowingInterruptedException runnable) { try { runnable.run(); @@ -93,25 +89,17 @@ public class Exceptions { /** * Wraps any IOException thrown from a runnable in an UncheckedIOException w/message. */ - public static void uncheck(RunnableThrowingException runnable, String format, String... args) { + public static void uncheck(RunnableThrowingIOException runnable, String format, String... args) { try { runnable.run(); } catch (IOException e) { String message = String.format(format, (Object[]) args); throw new UncheckedIOException(message, e); - } catch (InterruptedException e) { - String message = String.format(format, (Object[]) args); - throw new UncheckedInterruptedException(message, e); - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException) e; - String message = String.format(format, (Object[]) args); - throw new UncheckedException(message, e); } } /** Similar to uncheck(), except an exceptionToIgnore exception is silently ignored. */ - @Deprecated(since="7", forRemoval = true) - public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingException runnable, Class<T> exceptionToIgnore) { + public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingIOException runnable, Class<T> exceptionToIgnore) { try { runnable.run(); } catch (UncheckedIOException e) { @@ -123,58 +111,48 @@ public class Exceptions { throw e; } // Do nothing - OK - } catch (Exception e) { + } catch (IOException e) { try { e.getClass().asSubclass(exceptionToIgnore); } catch (ClassCastException f) { - throw new UncheckedException(e); + throw new UncheckedIOException(e); } // Do nothing - OK } } - @FunctionalInterface public interface RunnableThrowingException { void run() throws Exception; } + @FunctionalInterface + public interface RunnableThrowingIOException { + void run() throws IOException; + } + @FunctionalInterface public interface RunnableThrowingInterruptedException { void run() throws InterruptedException; } - @FunctionalInterface public interface SupplierThrowingException<T> { T get() throws Exception; } /** * Wraps any IOException thrown from a supplier in an UncheckedIOException. */ - public static <T> T uncheck(SupplierThrowingException<T> supplier) { + public static <T> T uncheck(SupplierThrowingIOException<T> supplier) { try { return supplier.get(); } catch (IOException e) { throw new UncheckedIOException(e); - } catch (InterruptedException e) { - throw new UncheckedInterruptedException(e); - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException) e; - throw new UncheckedException(e); } } /** * Wraps any IOException thrown from a supplier in an UncheckedIOException w/message. */ - public static <T> T uncheck(SupplierThrowingException<T> supplier, String format, String... args) { + public static <T> T uncheck(SupplierThrowingIOException<T> supplier, String format, String... args) { try { return supplier.get(); } catch (IOException e) { String message = String.format(format, (Object[]) args); throw new UncheckedIOException(message, e); - } catch (InterruptedException e) { - String message = String.format(format, (Object[]) args); - throw new UncheckedInterruptedException(message, e); - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException) e; - String message = String.format(format, (Object[]) args); - throw new UncheckedException(message, e); } } /** Similar to uncheck(), except null is returned if exceptionToIgnore is thrown. */ - @Deprecated(since="7", forRemoval = true) - public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingException<R> supplier, Class<T> exceptionToIgnore) { + public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingIOException<R> supplier, Class<T> exceptionToIgnore) { try { return supplier.get(); } catch (UncheckedIOException e) { @@ -186,16 +164,21 @@ public class Exceptions { throw e; } return null; - } catch (Exception e) { + } catch (IOException e) { try { e.getClass().asSubclass(exceptionToIgnore); } catch (ClassCastException f) { - throw new UncheckedException(e); + throw new UncheckedIOException(e); } return null; } } + @FunctionalInterface + public interface SupplierThrowingIOException<T> { + T get() throws IOException; + } + /** * Allows treating checked exceptions as unchecked. * Usage: diff --git a/yolean/src/main/java/com/yahoo/yolean/UncheckedException.java b/yolean/src/main/java/com/yahoo/yolean/UncheckedException.java deleted file mode 100644 index 39892c102a2..00000000000 --- a/yolean/src/main/java/com/yahoo/yolean/UncheckedException.java +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.yolean; - -/** - * Wraps an {@link Exception} with an unchecked exception. - * - * @author freva - */ -public class UncheckedException extends RuntimeException { - - public UncheckedException(String message, Exception cause) { - super(message, cause); - if (cause instanceof RuntimeException) - throw new IllegalArgumentException(cause.getClass() + " is not a checked exception"); - } - - public UncheckedException(Exception cause) { - this(cause.toString(), cause); - } - - public UncheckedException(String message) { this(message, null); } - -} diff --git a/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java b/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java index f7b6d50465a..d3317b5fb26 100644 --- a/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java +++ b/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java @@ -6,7 +6,7 @@ package com.yahoo.yolean; * * @author bjorncs */ -public class UncheckedInterruptedException extends UncheckedException { +public class UncheckedInterruptedException extends RuntimeException { public UncheckedInterruptedException(String message, InterruptedException cause, boolean restoreInterruptFlag) { super(message, cause); diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java index 16a304aca25..530be935bc1 100644 --- a/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java +++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java @@ -5,7 +5,7 @@ import com.yahoo.yolean.UncheckedInterruptedException; import java.time.Duration; -import static com.yahoo.yolean.Exceptions.uncheck; +import static com.yahoo.yolean.Exceptions.uncheckInterrupted; /** * An abstraction used for mocking {@link Thread#sleep(long)} in unit tests. @@ -14,12 +14,12 @@ import static com.yahoo.yolean.Exceptions.uncheck; */ public interface Sleeper { default void sleep(Duration duration) throws UncheckedInterruptedException { - uncheck(() -> sleepChecked(duration.toMillis())); + uncheckInterrupted(() -> sleepChecked(duration.toMillis())); } default void sleepChecked(Duration duration) throws InterruptedException { sleepChecked(duration.toMillis()); } - default void sleep(long millis) throws UncheckedInterruptedException { uncheck(() -> sleepChecked(millis)); } + default void sleep(long millis) throws UncheckedInterruptedException { uncheckInterrupted(() -> sleepChecked(millis)); } void sleepChecked(long millis) throws InterruptedException; diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java index ed36a550928..53cf3efe363 100644 --- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java +++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java @@ -9,6 +9,7 @@ import java.nio.file.NoSuchFileException; import java.util.Optional; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; /** @@ -41,7 +42,6 @@ public class ExceptionsTestCase { } @Test - @SuppressWarnings({"removal"}) public void testUnchecks() { try { Exceptions.uncheck(this::throwNoSuchFileException); |