Skip to content

Commit

Permalink
Add a new overload of getChecked() with a different parameter order.
Browse files Browse the repository at this point in the history
We will migrate callers to the new version.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=89434462
  • Loading branch information
cpovirk authored and cgdecker committed Apr 14, 2015
1 parent 9131ddb commit 4f60b48
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 18 deletions.
32 changes: 16 additions & 16 deletions guava-tests/test/com/google/common/util/concurrent/FuturesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3078,15 +3078,15 @@ public void testGetCheckedUntimed_withGoodAndBadExceptionConstructor() throws Ex
public void testGetCheckedTimed_success()
throws TwoArgConstructorException {
assertEquals("foo", getChecked(
immediateFuture("foo"), 0, SECONDS, TwoArgConstructorException.class));
immediateFuture("foo"), TwoArgConstructorException.class, 0, SECONDS));
}

@GwtIncompatible("Futures.getChecked")
public void testGetCheckedTimed_interrupted() {
SettableFuture<String> future = SettableFuture.create();
Thread.currentThread().interrupt();
try {
getChecked(future, 0, SECONDS, TwoArgConstructorException.class);
getChecked(future, TwoArgConstructorException.class, 0, SECONDS);
fail();
} catch (TwoArgConstructorException expected) {
assertTrue(expected.getCause() instanceof InterruptedException);
Expand All @@ -3102,7 +3102,7 @@ public void testGetCheckedTimed_cancelled()
SettableFuture<String> future = SettableFuture.create();
future.cancel(true);
try {
getChecked(future, 0, SECONDS, TwoArgConstructorException.class);
getChecked(future, TwoArgConstructorException.class, 0, SECONDS);
fail();
} catch (CancellationException expected) {
}
Expand All @@ -3111,8 +3111,8 @@ public void testGetCheckedTimed_cancelled()
@GwtIncompatible("Futures.getChecked")
public void testGetCheckedTimed_ExecutionExceptionChecked() {
try {
getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, 0, SECONDS,
TwoArgConstructorException.class);
getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, TwoArgConstructorException.class, 0,
SECONDS);
fail();
} catch (TwoArgConstructorException expected) {
assertEquals(CHECKED_EXCEPTION, expected.getCause());
Expand All @@ -3123,8 +3123,8 @@ public void testGetCheckedTimed_ExecutionExceptionChecked() {
public void testGetCheckedTimed_ExecutionExceptionUnchecked()
throws TwoArgConstructorException {
try {
getChecked(FAILED_FUTURE_UNCHECKED_EXCEPTION, 0, SECONDS,
TwoArgConstructorException.class);
getChecked(FAILED_FUTURE_UNCHECKED_EXCEPTION, TwoArgConstructorException.class, 0,
SECONDS);
fail();
} catch (UncheckedExecutionException expected) {
assertEquals(UNCHECKED_EXCEPTION, expected.getCause());
Expand All @@ -3135,7 +3135,7 @@ public void testGetCheckedTimed_ExecutionExceptionUnchecked()
public void testGetCheckedTimed_ExecutionExceptionError()
throws TwoArgConstructorException {
try {
getChecked(FAILED_FUTURE_ERROR, 0, SECONDS, TwoArgConstructorException.class);
getChecked(FAILED_FUTURE_ERROR, TwoArgConstructorException.class, 0, SECONDS);
fail();
} catch (ExecutionError expected) {
assertEquals(ERROR, expected.getCause());
Expand All @@ -3145,8 +3145,8 @@ public void testGetCheckedTimed_ExecutionExceptionError()
@GwtIncompatible("Futures.getChecked")
public void testGetCheckedTimed_ExecutionExceptionOtherThrowable() {
try {
getChecked(FAILED_FUTURE_OTHER_THROWABLE, 0, SECONDS,
TwoArgConstructorException.class);
getChecked(FAILED_FUTURE_OTHER_THROWABLE, TwoArgConstructorException.class, 0,
SECONDS);
fail();
} catch (TwoArgConstructorException expected) {
assertEquals(OTHER_THROWABLE, expected.getCause());
Expand All @@ -3157,8 +3157,8 @@ public void testGetCheckedTimed_ExecutionExceptionOtherThrowable() {
public void testGetCheckedTimed_RuntimeException()
throws TwoArgConstructorException {
try {
getChecked(RUNTIME_EXCEPTION_FUTURE, 0, SECONDS,
TwoArgConstructorException.class);
getChecked(RUNTIME_EXCEPTION_FUTURE, TwoArgConstructorException.class, 0,
SECONDS);
fail();
} catch (RuntimeException expected) {
assertEquals(RUNTIME_EXCEPTION, expected);
Expand All @@ -3169,7 +3169,7 @@ public void testGetCheckedTimed_RuntimeException()
public void testGetCheckedTimed_TimeoutException() {
SettableFuture<String> future = SettableFuture.create();
try {
getChecked(future, 0, SECONDS, TwoArgConstructorException.class);
getChecked(future, TwoArgConstructorException.class, 0, SECONDS);
fail();
} catch (TwoArgConstructorException expected) {
assertTrue(expected.getCause() instanceof TimeoutException);
Expand All @@ -3180,7 +3180,7 @@ public void testGetCheckedTimed_TimeoutException() {
public void testGetCheckedTimed_badExceptionConstructor_wrapsOriginalChecked() throws Exception {
try {
getChecked(
FAILED_FUTURE_CHECKED_EXCEPTION, 1, TimeUnit.SECONDS, ExceptionWithBadConstructor.class);
FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithBadConstructor.class, 1, TimeUnit.SECONDS);
fail();
} catch (IllegalArgumentException expected) {
assertSame(CHECKED_EXCEPTION, expected.getCause());
Expand All @@ -3190,8 +3190,8 @@ public void testGetCheckedTimed_badExceptionConstructor_wrapsOriginalChecked() t
@GwtIncompatible("Futures.getChecked")
public void testGetCheckedTimed_withGoodAndBadExceptionConstructor() throws Exception {
try {
getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, 1, TimeUnit.SECONDS,
ExceptionWithGoodAndBadConstructor.class);
getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithGoodAndBadConstructor.class, 1,
TimeUnit.SECONDS);
fail();
} catch (ExceptionWithGoodAndBadConstructor expected) {
assertSame(CHECKED_EXCEPTION, expected.getCause());
Expand Down
62 changes: 60 additions & 2 deletions guava/src/com/google/common/util/concurrent/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,11 @@ public static <V, X extends Exception> V getChecked(
* CancellationException}
* @throws IllegalArgumentException if {@code exceptionClass} extends {@code
* RuntimeException} or does not have a suitable constructor
* @since 19.0 (in 10.0 as {@code get})
* @since 19.0 (in 10.0 as {@code get} and with different parameter order)
*/
@GwtIncompatible("TODO")
public static <V, X extends Exception> V getChecked(
Future<V> future, long timeout, TimeUnit unit, Class<X> exceptionClass)
Future<V> future, Class<X> exceptionClass, long timeout, TimeUnit unit)
throws X {
checkNotNull(future);
checkNotNull(unit);
Expand All @@ -2058,6 +2058,64 @@ public static <V, X extends Exception> V getChecked(
}
}

/**
* <b>To be deprecated: We are changing the parameter order to {@link
* #getChecked(Future, Class, long, TimeUnit) future, exceptionClass, timeout,
* unit}.</b>
*
* <p>Returns the result of {@link Future#get(long, TimeUnit)}, converting
* most exceptions to a new instance of the given checked exception type. This
* reduces boilerplate for a common use of {@code Future} in which it is
* unnecessary to programmatically distinguish between exception types or to
* extract other information from the exception instance.
*
* <p>Exceptions from {@code Future.get} are treated as follows:
* <ul>
* <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an
* {@code X} if the cause is a checked exception, an {@link
* UncheckedExecutionException} if the cause is a {@code
* RuntimeException}, or an {@link ExecutionError} if the cause is an
* {@code Error}.
* <li>Any {@link InterruptedException} is wrapped in an {@code X} (after
* restoring the interrupt).
* <li>Any {@link TimeoutException} is wrapped in an {@code X}.
* <li>Any {@link CancellationException} is propagated untouched, as is any
* other {@link RuntimeException} (though {@code get} implementations are
* discouraged from throwing such exceptions).
* </ul>
*
* <p>The overall principle is to continue to treat every checked exception as a
* checked exception, every unchecked exception as an unchecked exception, and
* every error as an error. In addition, the cause of any {@code
* ExecutionException} is wrapped in order to ensure that the new stack trace
* matches that of the current thread.
*
* <p>Instances of {@code exceptionClass} are created by choosing an arbitrary
* public constructor that accepts zero or more arguments, all of type {@code
* String} or {@code Throwable} (preferring constructors with at least one
* {@code String}) and calling the constructor via reflection. If the
* exception did not already have a cause, one is set by calling {@link
* Throwable#initCause(Throwable)} on it. If no such constructor exists, an
* {@code IllegalArgumentException} is thrown.
*
* @throws X if {@code get} throws any checked exception except for an {@code
* ExecutionException} whose cause is not itself a checked exception
* @throws UncheckedExecutionException if {@code get} throws an {@code
* ExecutionException} with a {@code RuntimeException} as its cause
* @throws ExecutionError if {@code get} throws an {@code ExecutionException}
* with an {@code Error} as its cause
* @throws CancellationException if {@code get} throws a {@code
* CancellationException}
* @throws IllegalArgumentException if {@code exceptionClass} extends {@code
* RuntimeException} or does not have a suitable constructor
*/
@GwtIncompatible("TODO")
public static <V, X extends Exception> V getChecked(
Future<V> future, long timeout, TimeUnit unit, Class<X> exceptionClass)
throws X {
return getChecked(future, exceptionClass, timeout, unit);
}

@GwtIncompatible("TODO")
private static <X extends Exception> void wrapAndThrowExceptionOrError(
Throwable cause, Class<X> exceptionClass) throws X {
Expand Down

0 comments on commit 4f60b48

Please sign in to comment.