Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverted CheckedFunction*.apply throws Exception -> throws Throwable #2352

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 16 additions & 35 deletions vavr/generator/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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") + "<R>"
case (true, 0) => ", " + im.getType("java.util.concurrent.Callable") + "<R>"
case (false, 1) => ", " + im.getType("java.util.function.Function") + "<T1, R>"
case (false, 2) => ", " + im.getType("java.util.function.BiFunction") + "<T1, T2, R>"
case _ => ""
Expand Down Expand Up @@ -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")(", ")
Expand All @@ -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()}.
*
Expand Down Expand Up @@ -1731,18 +1719,18 @@ def generateMainClasses(): Unit = {
${if (i == 0) xs"""
${if (checked) xs"""
final Lazy<R> 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<R> & 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;
Expand Down Expand Up @@ -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<String> callable = () -> s;
assertThat(callable.call()).isEqualTo(s);
}
""" else xs"""
${(i == 0 && !checked).gen(
xs"""
@$test
public void shouldGetValue() {
final String s = "test";
Expand Down
38 changes: 13 additions & 25 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction0.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,7 +39,7 @@
* @author Daniel Dietrich
*/
@FunctionalInterface
public interface CheckedFunction0<R> extends Serializable, Callable<R> {
public interface CheckedFunction0<R> extends Serializable {

/**
* The <a href="https://docs.oracle.com/javase/8/docs/api/index.html">serial version uid</a>.
Expand Down Expand Up @@ -138,20 +137,9 @@ static <R> CheckedFunction0<R> narrow(CheckedFunction0<? extends R> 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.
Expand Down Expand Up @@ -202,18 +190,18 @@ default CheckedFunction0<R> memoized() {
return this;
} else {
final Lazy<R> 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<R> & Memoized) () -> {
try {
return lazy.get();
} catch(RuntimeException x) {
throw (Exception) x.getCause();
}
try {
return lazy.get();
} catch(RuntimeException x) {
throw x.getCause();
}
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction1.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ static <T> CheckedFunction1<T, T> 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.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction2.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ static <T1, T2, R> CheckedFunction2<T1, T2, R> narrow(CheckedFunction2<? super T
* @param t1 argument 1
* @param t2 argument 2
* @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) throws Exception;
R apply(T1 t1, T2 t2) throws Throwable;

/**
* Applies this function partially to one argument.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction3.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ static <T1, T2, T3, R> CheckedFunction3<T1, T2, T3, R> narrow(CheckedFunction3<?
* @param t2 argument 2
* @param t3 argument 3
* @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) throws Exception;
R apply(T1 t1, T2 t2, T3 t3) throws Throwable;

/**
* Applies this function partially to one argument.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction4.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ static <T1, T2, T3, T4, R> CheckedFunction4<T1, T2, T3, T4, R> 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.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction5.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ static <T1, T2, T3, T4, T5, R> CheckedFunction5<T1, T2, T3, T4, T5, R> 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.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction6.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ static <T1, T2, T3, T4, T5, T6, R> CheckedFunction6<T1, T2, T3, T4, T5, T6, R> 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.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction7.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ static <T1, T2, T3, T4, T5, T6, T7, R> CheckedFunction7<T1, T2, T3, T4, T5, T6,
* @param t6 argument 6
* @param t7 argument 7
* @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, T7 t7) throws Exception;
R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) throws Throwable;

/**
* Applies this function partially to one argument.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src-gen/main/java/io/vavr/CheckedFunction8.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ static <T1, T2, T3, T4, T5, T6, T7, T8, R> CheckedFunction8<T1, T2, T3, T4, T5,
* @param t7 argument 7
* @param t8 argument 8
* @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, T7 t7, T8 t8) throws Exception;
R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) throws Throwable;

/**
* Applies this function partially to one argument.
Expand Down
7 changes: 0 additions & 7 deletions vavr/src-gen/test/java/io/vavr/CheckedFunction0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ public void shouldLiftPartialFunction() {
assertThat(CheckedFunction0.lift(() -> { while(true); })).isNotNull();
}

@Test
public void shouldCallValue() throws Exception {
final String s = "test";
final CheckedFunction0<String> callable = () -> s;
assertThat(callable.call()).isEqualTo(s);
}

@Test
public void shouldGetArity() {
final CheckedFunction0<Object> f = () -> null;
Expand Down
4 changes: 2 additions & 2 deletions vavr/src/main/java/io/vavr/CheckedConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static <T> CheckedConsumer<T> of(CheckedConsumer<T> 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)}
Expand Down
4 changes: 2 additions & 2 deletions vavr/src/main/java/io/vavr/CheckedPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static <T> CheckedPredicate<T> of(CheckedPredicate<T> 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.
Expand Down
4 changes: 2 additions & 2 deletions vavr/src/main/java/io/vavr/CheckedRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <em>sneaky throw</em> if an exceptions occurs when running the unit of work.
Expand Down
9 changes: 1 addition & 8 deletions vavr/src/test/java/io/vavr/concurrent/Concurrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,13 @@ static <T> CheckedFunction0<T> zZz(T value) {
};
}

static <T, X extends Exception> CheckedFunction0<T> zZz(X exception) {
static <T, X extends Throwable> CheckedFunction0<T> zZz(X exception) {
return () -> {
zZz();
throw exception;
};
}

static <T, X extends Error> CheckedFunction0<T> zZz(X error) {
return () -> {
zZz();
throw error;
};
}

static void gracefullyFinishThreads() throws TimeoutException {
final boolean isQuiescent = ForkJoinPool.commonPool().awaitQuiescence(1L, TimeUnit.MINUTES);
if (isQuiescent) {
Expand Down
6 changes: 3 additions & 3 deletions vavr/src/test/java/io/vavr/concurrent/FutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1264,14 +1264,14 @@ private static Future<Void> blocking(CheckedRunnable computation) {
private static <T> Future<T> blocking(CheckedFunction0<? extends T> computation) {
return Future.of(() -> {
final AtomicReference<T> result = new AtomicReference<>(null);
final AtomicReference<Exception> errorRef = new AtomicReference<>(null);
final AtomicReference<Throwable> 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;
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vavr/src/test/java/io/vavr/control/TryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down