Skip to content

Commit

Permalink
Removed special-casing UndeclaredThrowableException in Futures.transf…
Browse files Browse the repository at this point in the history
…orm()

RELNOTES=`util.concurrent`: Removed special-casing `UndeclaredThrowableException` in `Futures.transform()`.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201046764
  • Loading branch information
jlavallee authored and cgdecker committed Jun 28, 2018
1 parent 39fda5d commit 9466b62
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 24 deletions.
Expand Up @@ -22,7 +22,6 @@
import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly;

import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -58,7 +57,7 @@ protected String getSuccessfulResult() {

private class ChainingFunction implements AsyncFunction<Integer, String> {
@Override
public ListenableFuture<String> apply(Integer input) {
public ListenableFuture<String> apply(Integer input) throws Exception {
switch (input) {
case VALID_INPUT_DATA:
outputFuture.set(RESULT_DATA);
Expand All @@ -69,8 +68,8 @@ public ListenableFuture<String> apply(Integer input) {
funcIsWaitingLatch.countDown();
awaitUninterruptibly(funcCompletionLatch);
break;
default:
throw new UndeclaredThrowableException(EXCEPTION);
case EXCEPTION_DATA:
throw EXCEPTION;
}
return outputFuture;
}
Expand Down
Expand Up @@ -29,6 +29,8 @@
*/
public class FuturesTransformTest extends AbstractChainedListenableFutureTest<String> {
private static final String RESULT_DATA = "SUCCESS";
private static final UndeclaredThrowableException WRAPPED_EXCEPTION =
new UndeclaredThrowableException(EXCEPTION);

@Override
protected ListenableFuture<String> buildChainingFuture(ListenableFuture<Integer> inputFuture) {
Expand All @@ -46,13 +48,13 @@ public String apply(Integer input) {
if (input.intValue() == VALID_INPUT_DATA) {
return RESULT_DATA;
} else {
throw new UndeclaredThrowableException(EXCEPTION);
throw WRAPPED_EXCEPTION;
}
}
}

public void testFutureGetThrowsFunctionException() throws Exception {
inputFuture.set(EXCEPTION_DATA);
listener.assertException(EXCEPTION);
listener.assertException(WRAPPED_EXCEPTION);
}
}
Expand Up @@ -21,7 +21,6 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import com.google.errorprone.annotations.ForOverride;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -109,10 +108,6 @@ public final void run() {
T transformResult;
try {
transformResult = doTransform(localFunction, sourceResult);
} catch (UndeclaredThrowableException e) {
// Set the cause of the exception as this future's exception.
setException(e.getCause());
return;
} catch (Throwable t) {
// This exception is irrelevant in this thread, but useful for the client.
setException(t);
Expand Down Expand Up @@ -237,7 +232,6 @@ private static final class TransformFuture<I, O>
@NullableDecl
O doTransform(Function<? super I, ? extends O> function, @NullableDecl I input) {
return function.apply(input);
// TODO(lukes): move the UndeclaredThrowable catch block here?
}

@Override
Expand Down
Expand Up @@ -22,7 +22,6 @@
import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly;

import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -58,7 +57,7 @@ protected String getSuccessfulResult() {

private class ChainingFunction implements AsyncFunction<Integer, String> {
@Override
public ListenableFuture<String> apply(Integer input) {
public ListenableFuture<String> apply(Integer input) throws Exception {
switch (input) {
case VALID_INPUT_DATA:
outputFuture.set(RESULT_DATA);
Expand All @@ -69,8 +68,8 @@ public ListenableFuture<String> apply(Integer input) {
funcIsWaitingLatch.countDown();
awaitUninterruptibly(funcCompletionLatch);
break;
default:
throw new UndeclaredThrowableException(EXCEPTION);
case EXCEPTION_DATA:
throw EXCEPTION;
}
return outputFuture;
}
Expand Down
Expand Up @@ -29,6 +29,8 @@
*/
public class FuturesTransformTest extends AbstractChainedListenableFutureTest<String> {
private static final String RESULT_DATA = "SUCCESS";
private static final UndeclaredThrowableException WRAPPED_EXCEPTION =
new UndeclaredThrowableException(EXCEPTION);

@Override
protected ListenableFuture<String> buildChainingFuture(ListenableFuture<Integer> inputFuture) {
Expand All @@ -46,13 +48,13 @@ public String apply(Integer input) {
if (input.intValue() == VALID_INPUT_DATA) {
return RESULT_DATA;
} else {
throw new UndeclaredThrowableException(EXCEPTION);
throw WRAPPED_EXCEPTION;
}
}
}

public void testFutureGetThrowsFunctionException() throws Exception {
inputFuture.set(EXCEPTION_DATA);
listener.assertException(EXCEPTION);
listener.assertException(WRAPPED_EXCEPTION);
}
}
Expand Up @@ -21,7 +21,6 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import com.google.errorprone.annotations.ForOverride;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -109,10 +108,6 @@ public final void run() {
T transformResult;
try {
transformResult = doTransform(localFunction, sourceResult);
} catch (UndeclaredThrowableException e) {
// Set the cause of the exception as this future's exception.
setException(e.getCause());
return;
} catch (Throwable t) {
// This exception is irrelevant in this thread, but useful for the client.
setException(t);
Expand Down Expand Up @@ -236,7 +231,6 @@ private static final class TransformFuture<I, O>
@Nullable
O doTransform(Function<? super I, ? extends O> function, @Nullable I input) {
return function.apply(input);
// TODO(lukes): move the UndeclaredThrowable catch block here?
}

@Override
Expand Down

0 comments on commit 9466b62

Please sign in to comment.