summaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorValerij Fredriksen <valerijf@yahooinc.com>2022-05-25 14:07:44 +0200
committerValerij Fredriksen <valerijf@yahooinc.com>2022-05-25 15:53:22 +0200
commit6024c19db7858d49d783f53b765709399598bc51 (patch)
tree2a6d044afab88351bcd1735b8b364f8bee6418e7 /yolean
parent69bb4e0e94cd71aec2b6b20ba146e40394c8a087 (diff)
uncheck() all checked exceptions
Diffstat (limited to 'yolean')
-rw-r--r--yolean/abi-spec.json31
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java67
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/UncheckedException.java23
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java2
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java6
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java2
6 files changed, 92 insertions, 39 deletions
diff --git a/yolean/abi-spec.json b/yolean/abi-spec.json
index 8c10006e7e1..568535fbf39 100644
--- a/yolean/abi-spec.json
+++ b/yolean/abi-spec.json
@@ -1,5 +1,5 @@
{
- "com.yahoo.yolean.Exceptions$RunnableThrowingIOException": {
+ "com.yahoo.yolean.Exceptions$RunnableThrowingException": {
"superClass": "java.lang.Object",
"interfaces": [],
"attributes": [
@@ -25,7 +25,7 @@
],
"fields": []
},
- "com.yahoo.yolean.Exceptions$SupplierThrowingIOException": {
+ "com.yahoo.yolean.Exceptions$SupplierThrowingException": {
"superClass": "java.lang.Object",
"interfaces": [],
"attributes": [
@@ -48,25 +48,38 @@
"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$RunnableThrowingIOException)",
+ "public static void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingException)",
"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$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 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 java.lang.RuntimeException throwUnchecked(java.lang.Throwable)"
],
"fields": []
},
- "com.yahoo.yolean.UncheckedInterruptedException": {
+ "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",
+ "interfaces": [],
+ "attributes": [
+ "public"
+ ],
+ "methods": [
"public void <init>(java.lang.String, java.lang.InterruptedException, boolean)",
"public void <init>(java.lang.InterruptedException, boolean)",
"public void <init>(java.lang.String, boolean)",
diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
index 89b4e76368b..9776bdb54a8 100644
--- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
+++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
@@ -62,22 +62,26 @@ public class Exceptions {
/**
* Wraps any IOException thrown from a runnable in an UncheckedIOException.
*/
- public static void uncheck(RunnableThrowingIOException runnable) {
+ public static void uncheck(RunnableThrowingException 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) {
- try {
- runnable.run();
- } catch (InterruptedException e) {
- throw new UncheckedInterruptedException(e, false);
- }
+ uncheck(runnable::run);
}
+ @Deprecated(since="7", forRemoval = true)
public static void uncheckInterruptedAndRestoreFlag(RunnableThrowingInterruptedException runnable) {
try {
runnable.run();
@@ -89,17 +93,25 @@ public class Exceptions {
/**
* Wraps any IOException thrown from a runnable in an UncheckedIOException w/message.
*/
- public static void uncheck(RunnableThrowingIOException runnable, String format, String... args) {
+ public static void uncheck(RunnableThrowingException 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. */
- public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingIOException runnable, Class<T> exceptionToIgnore) {
+ @Deprecated(since="7", forRemoval = true)
+ public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingException runnable, Class<T> exceptionToIgnore) {
try {
runnable.run();
} catch (UncheckedIOException e) {
@@ -111,48 +123,58 @@ public class Exceptions {
throw e;
}
// Do nothing - OK
- } catch (IOException e) {
+ } catch (Exception e) {
try {
e.getClass().asSubclass(exceptionToIgnore);
} catch (ClassCastException f) {
- throw new UncheckedIOException(e);
+ throw new UncheckedException(e);
}
// Do nothing - OK
}
}
- @FunctionalInterface
- public interface RunnableThrowingIOException {
- void run() throws IOException;
- }
-
+ @FunctionalInterface public interface RunnableThrowingException { void run() throws Exception; }
@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(SupplierThrowingIOException<T> supplier) {
+ public static <T> T uncheck(SupplierThrowingException<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(SupplierThrowingIOException<T> supplier, String format, String... args) {
+ public static <T> T uncheck(SupplierThrowingException<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. */
- public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingIOException<R> supplier, Class<T> exceptionToIgnore) {
+ @Deprecated(since="7", forRemoval = true)
+ public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingException<R> supplier, Class<T> exceptionToIgnore) {
try {
return supplier.get();
} catch (UncheckedIOException e) {
@@ -164,21 +186,16 @@ public class Exceptions {
throw e;
}
return null;
- } catch (IOException e) {
+ } catch (Exception e) {
try {
e.getClass().asSubclass(exceptionToIgnore);
} catch (ClassCastException f) {
- throw new UncheckedIOException(e);
+ throw new UncheckedException(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
new file mode 100644
index 00000000000..39892c102a2
--- /dev/null
+++ b/yolean/src/main/java/com/yahoo/yolean/UncheckedException.java
@@ -0,0 +1,23 @@
+// 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 d3317b5fb26..f7b6d50465a 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 RuntimeException {
+public class UncheckedInterruptedException extends UncheckedException {
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 530be935bc1..16a304aca25 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.uncheckInterrupted;
+import static com.yahoo.yolean.Exceptions.uncheck;
/**
* An abstraction used for mocking {@link Thread#sleep(long)} in unit tests.
@@ -14,12 +14,12 @@ import static com.yahoo.yolean.Exceptions.uncheckInterrupted;
*/
public interface Sleeper {
default void sleep(Duration duration) throws UncheckedInterruptedException {
- uncheckInterrupted(() -> sleepChecked(duration.toMillis()));
+ uncheck(() -> sleepChecked(duration.toMillis()));
}
default void sleepChecked(Duration duration) throws InterruptedException { sleepChecked(duration.toMillis()); }
- default void sleep(long millis) throws UncheckedInterruptedException { uncheckInterrupted(() -> sleepChecked(millis)); }
+ default void sleep(long millis) throws UncheckedInterruptedException { uncheck(() -> 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 53cf3efe363..ed36a550928 100644
--- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
+++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
@@ -9,7 +9,6 @@ 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;
/**
@@ -42,6 +41,7 @@ public class ExceptionsTestCase {
}
@Test
+ @SuppressWarnings({"removal"})
public void testUnchecks() {
try {
Exceptions.uncheck(this::throwNoSuchFileException);