From b9243f65e70a9b5cd68eaa115dac4db5dc499615 Mon Sep 17 00:00:00 2001 From: Daniel Dietrich Date: Sat, 19 Jan 2019 16:00:23 +0100 Subject: [PATCH] Reverted CheckedFunction*.apply throws Exception -> throws Throwable --- vavr/generator/Generator.scala | 51 ++++++------------- .../main/java/io/vavr/CheckedFunction0.java | 38 +++++--------- .../main/java/io/vavr/CheckedFunction1.java | 4 +- .../main/java/io/vavr/CheckedFunction2.java | 4 +- .../main/java/io/vavr/CheckedFunction3.java | 4 +- .../main/java/io/vavr/CheckedFunction4.java | 4 +- .../main/java/io/vavr/CheckedFunction5.java | 4 +- .../main/java/io/vavr/CheckedFunction6.java | 4 +- .../main/java/io/vavr/CheckedFunction7.java | 4 +- .../main/java/io/vavr/CheckedFunction8.java | 4 +- .../java/io/vavr/CheckedFunction0Test.java | 7 --- .../main/java/io/vavr/CheckedConsumer.java | 4 +- .../main/java/io/vavr/CheckedPredicate.java | 4 +- .../main/java/io/vavr/CheckedRunnable.java | 4 +- .../java/io/vavr/concurrent/Concurrent.java | 9 +--- .../java/io/vavr/concurrent/FutureTest.java | 6 +-- .../test/java/io/vavr/control/TryTest.java | 2 +- 17 files changed, 56 insertions(+), 101 deletions(-) diff --git a/vavr/generator/Generator.scala b/vavr/generator/Generator.scala index 5e84eb4611..73925e5938 100644 --- a/vavr/generator/Generator.scala +++ b/vavr/generator/Generator.scala @@ -1464,7 +1464,6 @@ def generateMainClasses(): Unit = { val Serializable = im.getType("java.io.Serializable") val additionalExtends = (checked, i) match { case (false, 0) => ", " + im.getType("java.util.function.Supplier") + "" - case (true, 0) => ", " + im.getType("java.util.concurrent.Callable") + "" case (false, 1) => ", " + im.getType("java.util.function.Function") + "" case (false, 2) => ", " + im.getType("java.util.function.BiFunction") + "" case _ => "" @@ -1632,9 +1631,9 @@ def generateMainClasses(): Unit = { * Applies this function to ${arguments(i)} and returns the result. ${(0 to i).gen(j => if (j == 0) "*" else s"* @param t$j argument $j")("\n")} * @return the result of function application - * ${checked.gen("@throws Exception if something goes wrong applying this function to the given arguments")} + * ${checked.gen("@throws Throwable if something goes wrong applying this function to the given arguments")} */ - R apply($paramsDecl)${checked.gen(" throws Exception")}; + R apply($paramsDecl)${checked.gen(" throws Throwable")}; ${(1 until i).gen(j => { val partialApplicationArgs = (1 to j).gen(k => s"T$k t$k")(", ") @@ -1655,19 +1654,8 @@ def generateMainClasses(): Unit = { """ })("\n\n")} - ${(i == 0).gen( - if (checked) xs""" - /$javadoc - * Implementation of {@linkplain java.util.concurrent.Callable#call()}, just calls {@linkplain #apply()}. - * - * @return the result of {@code apply()} - * @throws Exception if something goes wrong when calling this function - */ - @Override - default R call() throws Exception { - return apply(); - } - """ else xs""" + ${(i == 0 && !checked).gen( + xs""" /$javadoc * Implementation of {@linkplain java.util.function.Supplier#get()}, just calls {@linkplain #apply()}. * @@ -1731,18 +1719,18 @@ def generateMainClasses(): Unit = { ${if (i == 0) xs""" ${if (checked) xs""" final Lazy lazy = Lazy.of(() -> { - try { - return apply(); - } catch (Exception x) { - throw new RuntimeException(x); - } + try { + return apply(); + } catch (Throwable x) { + throw new RuntimeException(x); + } }); return (CheckedFunction0 & Memoized) () -> { - try { - return lazy.get(); - } catch(RuntimeException x) { - throw (Exception) x.getCause(); - } + try { + return lazy.get(); + } catch(RuntimeException x) { + throw x.getCause(); + } }; """ else xs""" return ($className$fullGenerics & Memoized) Lazy.of(this)::get; @@ -3077,15 +3065,8 @@ def generateTestClasses(): Unit = { } """)} - ${(i == 0).gen( - if (checked) xs""" - @$test - public void shouldCallValue() throws Exception { - final String s = "test"; - final ${name}0 callable = () -> s; - assertThat(callable.call()).isEqualTo(s); - } - """ else xs""" + ${(i == 0 && !checked).gen( + xs""" @$test public void shouldGetValue() { final String s = "test"; diff --git a/vavr/src-gen/main/java/io/vavr/CheckedFunction0.java b/vavr/src-gen/main/java/io/vavr/CheckedFunction0.java index 61b1203e8a..d4bd501002 100644 --- a/vavr/src-gen/main/java/io/vavr/CheckedFunction0.java +++ b/vavr/src-gen/main/java/io/vavr/CheckedFunction0.java @@ -29,7 +29,6 @@ import io.vavr.control.Try; import java.io.Serializable; import java.util.Objects; -import java.util.concurrent.Callable; import java.util.function.Function; import java.util.function.Supplier; @@ -40,7 +39,7 @@ * @author Daniel Dietrich */ @FunctionalInterface -public interface CheckedFunction0 extends Serializable, Callable { +public interface CheckedFunction0 extends Serializable { /** * The serial version uid. @@ -138,20 +137,9 @@ static CheckedFunction0 narrow(CheckedFunction0 f) { * Applies this function to no arguments and returns the result. * * @return the result of function application - * @throws Exception if something goes wrong applying this function to the given arguments + * @throws Throwable if something goes wrong applying this function to the given arguments */ - R apply() throws Exception; - - /** - * Implementation of {@linkplain java.util.concurrent.Callable#call()}, just calls {@linkplain #apply()}. - * - * @return the result of {@code apply()} - * @throws Exception if something goes wrong when calling this function - */ - @Override - default R call() throws Exception { - return apply(); - } + R apply() throws Throwable; /** * Returns the number of function arguments. @@ -202,18 +190,18 @@ default CheckedFunction0 memoized() { return this; } else { final Lazy lazy = Lazy.of(() -> { - try { - return apply(); - } catch (Exception x) { - throw new RuntimeException(x); - } + try { + return apply(); + } catch (Throwable x) { + throw new RuntimeException(x); + } }); return (CheckedFunction0 & Memoized) () -> { - try { - return lazy.get(); - } catch(RuntimeException x) { - throw (Exception) x.getCause(); - } + try { + return lazy.get(); + } catch(RuntimeException x) { + throw x.getCause(); + } }; } } diff --git a/vavr/src-gen/main/java/io/vavr/CheckedFunction1.java b/vavr/src-gen/main/java/io/vavr/CheckedFunction1.java index c6ea2926dc..42400462d2 100644 --- a/vavr/src-gen/main/java/io/vavr/CheckedFunction1.java +++ b/vavr/src-gen/main/java/io/vavr/CheckedFunction1.java @@ -154,9 +154,9 @@ static CheckedFunction1 identity() { * * @param t1 argument 1 * @return the result of function application - * @throws Exception if something goes wrong applying this function to the given arguments + * @throws Throwable if something goes wrong applying this function to the given arguments */ - R apply(T1 t1) throws Exception; + R apply(T1 t1) throws Throwable; /** * Returns the number of function arguments. diff --git a/vavr/src-gen/main/java/io/vavr/CheckedFunction2.java b/vavr/src-gen/main/java/io/vavr/CheckedFunction2.java index 9b3bdad639..227b5a0c99 100644 --- a/vavr/src-gen/main/java/io/vavr/CheckedFunction2.java +++ b/vavr/src-gen/main/java/io/vavr/CheckedFunction2.java @@ -152,9 +152,9 @@ static CheckedFunction2 narrow(CheckedFunction2 CheckedFunction3 narrow(CheckedFunction3 CheckedFunction4 narrow(CheckedFun * @param t3 argument 3 * @param t4 argument 4 * @return the result of function application - * @throws Exception if something goes wrong applying this function to the given arguments + * @throws Throwable if something goes wrong applying this function to the given arguments */ - R apply(T1 t1, T2 t2, T3 t3, T4 t4) throws Exception; + R apply(T1 t1, T2 t2, T3 t3, T4 t4) throws Throwable; /** * Applies this function partially to one argument. diff --git a/vavr/src-gen/main/java/io/vavr/CheckedFunction5.java b/vavr/src-gen/main/java/io/vavr/CheckedFunction5.java index 7140883715..f5450b9d9e 100644 --- a/vavr/src-gen/main/java/io/vavr/CheckedFunction5.java +++ b/vavr/src-gen/main/java/io/vavr/CheckedFunction5.java @@ -172,9 +172,9 @@ static CheckedFunction5 narrow(Ch * @param t4 argument 4 * @param t5 argument 5 * @return the result of function application - * @throws Exception if something goes wrong applying this function to the given arguments + * @throws Throwable if something goes wrong applying this function to the given arguments */ - R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) throws Exception; + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) throws Throwable; /** * Applies this function partially to one argument. diff --git a/vavr/src-gen/main/java/io/vavr/CheckedFunction6.java b/vavr/src-gen/main/java/io/vavr/CheckedFunction6.java index 6e25874f28..784462b490 100644 --- a/vavr/src-gen/main/java/io/vavr/CheckedFunction6.java +++ b/vavr/src-gen/main/java/io/vavr/CheckedFunction6.java @@ -179,9 +179,9 @@ static CheckedFunction6 n * @param t5 argument 5 * @param t6 argument 6 * @return the result of function application - * @throws Exception if something goes wrong applying this function to the given arguments + * @throws Throwable if something goes wrong applying this function to the given arguments */ - R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) throws Exception; + R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) throws Throwable; /** * Applies this function partially to one argument. diff --git a/vavr/src-gen/main/java/io/vavr/CheckedFunction7.java b/vavr/src-gen/main/java/io/vavr/CheckedFunction7.java index a72035106f..6239d4d20e 100644 --- a/vavr/src-gen/main/java/io/vavr/CheckedFunction7.java +++ b/vavr/src-gen/main/java/io/vavr/CheckedFunction7.java @@ -186,9 +186,9 @@ static CheckedFunction7 CheckedFunction8 { while(true); })).isNotNull(); } - @Test - public void shouldCallValue() throws Exception { - final String s = "test"; - final CheckedFunction0 callable = () -> s; - assertThat(callable.call()).isEqualTo(s); - } - @Test public void shouldGetArity() { final CheckedFunction0 f = () -> null; diff --git a/vavr/src/main/java/io/vavr/CheckedConsumer.java b/vavr/src/main/java/io/vavr/CheckedConsumer.java index ae527269c7..2270fce67b 100644 --- a/vavr/src/main/java/io/vavr/CheckedConsumer.java +++ b/vavr/src/main/java/io/vavr/CheckedConsumer.java @@ -59,9 +59,9 @@ static CheckedConsumer of(CheckedConsumer methodReference) { * Performs side-effects. * * @param t a value of type {@code T} - * @throws Exception if an error occurs + * @throws Throwable if an error occurs */ - void accept(T t) throws Exception; + void accept(T t) throws Throwable; /** * Returns a chained {@code CheckedConsumer} that first executes {@code this.accept(t)} diff --git a/vavr/src/main/java/io/vavr/CheckedPredicate.java b/vavr/src/main/java/io/vavr/CheckedPredicate.java index a19060d198..0b6b4786b7 100644 --- a/vavr/src/main/java/io/vavr/CheckedPredicate.java +++ b/vavr/src/main/java/io/vavr/CheckedPredicate.java @@ -59,9 +59,9 @@ static CheckedPredicate of(CheckedPredicate methodReference) { * * @param t the input argument * @return {@code true} if the input argument matches the predicate, otherwise {@code false} - * @throws Exception if an error occurs + * @throws Throwable if an error occurs */ - boolean test(T t) throws Exception; + boolean test(T t) throws Throwable; /** * Negates this predicate. diff --git a/vavr/src/main/java/io/vavr/CheckedRunnable.java b/vavr/src/main/java/io/vavr/CheckedRunnable.java index 12684c902e..2bef2c2f3c 100644 --- a/vavr/src/main/java/io/vavr/CheckedRunnable.java +++ b/vavr/src/main/java/io/vavr/CheckedRunnable.java @@ -53,9 +53,9 @@ static CheckedRunnable of(CheckedRunnable methodReference) { /** * Performs side-effects. * - * @throws Exception if an error occurs + * @throws Throwable if an error occurs */ - void run() throws Exception; + void run() throws Throwable; /** * Returns an unchecked {@link Runnable} that will sneaky throw if an exceptions occurs when running the unit of work. diff --git a/vavr/src/test/java/io/vavr/concurrent/Concurrent.java b/vavr/src/test/java/io/vavr/concurrent/Concurrent.java index eb896a40bd..7c0b3a58bf 100644 --- a/vavr/src/test/java/io/vavr/concurrent/Concurrent.java +++ b/vavr/src/test/java/io/vavr/concurrent/Concurrent.java @@ -72,20 +72,13 @@ static CheckedFunction0 zZz(T value) { }; } - static CheckedFunction0 zZz(X exception) { + static CheckedFunction0 zZz(X exception) { return () -> { zZz(); throw exception; }; } - static CheckedFunction0 zZz(X error) { - return () -> { - zZz(); - throw error; - }; - } - static void gracefullyFinishThreads() throws TimeoutException { final boolean isQuiescent = ForkJoinPool.commonPool().awaitQuiescence(1L, TimeUnit.MINUTES); if (isQuiescent) { diff --git a/vavr/src/test/java/io/vavr/concurrent/FutureTest.java b/vavr/src/test/java/io/vavr/concurrent/FutureTest.java index b11fef9c4b..38ed5444c0 100644 --- a/vavr/src/test/java/io/vavr/concurrent/FutureTest.java +++ b/vavr/src/test/java/io/vavr/concurrent/FutureTest.java @@ -1264,14 +1264,14 @@ private static Future blocking(CheckedRunnable computation) { private static Future blocking(CheckedFunction0 computation) { return Future.of(() -> { final AtomicReference result = new AtomicReference<>(null); - final AtomicReference errorRef = new AtomicReference<>(null); + final AtomicReference errorRef = new AtomicReference<>(null); ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() { boolean releasable = false; @Override public boolean block() { try { result.set(computation.apply()); - } catch(Exception x) { + } catch(Throwable x) { errorRef.set(x); } return releasable = true; @@ -1281,7 +1281,7 @@ public boolean isReleasable() { return releasable; } }); - final Exception error = errorRef.get(); + final Throwable error = errorRef.get(); if (error != null) { throw error; } else { diff --git a/vavr/src/test/java/io/vavr/control/TryTest.java b/vavr/src/test/java/io/vavr/control/TryTest.java index 7d346bac6c..df05576fee 100644 --- a/vavr/src/test/java/io/vavr/control/TryTest.java +++ b/vavr/src/test/java/io/vavr/control/TryTest.java @@ -899,7 +899,7 @@ public void shouldReturnRecoveredValueOnFailure(){ @Test public void shouldNotRecoverFailureWhenExceptionTypeIsntAssignable(){ - final Exception error = new IllegalStateException(FAILURE); + final Throwable error = new IllegalStateException(FAILURE); assertThat(Try.of(() -> { throw error; }).recoverWith(Error.class, success()).getCause()).isSameAs(error); }