Skip to content

Commit 8f747d0

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Migrate off Throwables.propagate, and stop rethrowing exceptions from other threads.
Plus, simplify `CacheLoader` setup slightly. Fixes #7434 RELNOTES=n/a PiperOrigin-RevId: 686728158
1 parent ca12c33 commit 8f747d0

File tree

6 files changed

+34
-66
lines changed

6 files changed

+34
-66
lines changed

android/guava-tests/test/com/google/common/eventbus/EventBusTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.google.common.eventbus;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.Assert.assertThrows;
2021

2122
import com.google.common.collect.ImmutableList;
2223
import com.google.common.collect.Lists;
24+
import com.google.common.util.concurrent.UncheckedExecutionException;
2325
import java.util.List;
2426
import java.util.concurrent.ExecutorService;
2527
import java.util.concurrent.Executors;
@@ -281,7 +283,10 @@ class SubscribesToPrimitive {
281283
@Subscribe
282284
public void toInt(int i) {}
283285
}
284-
assertThrows(IllegalArgumentException.class, () -> bus.register(new SubscribesToPrimitive()));
286+
UncheckedExecutionException thrown =
287+
assertThrows(
288+
UncheckedExecutionException.class, () -> bus.register(new SubscribesToPrimitive()));
289+
assertThat(thrown).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
285290
}
286291

287292
/** Records thrown exception information. */

android/guava/src/com/google/common/eventbus/SubscriberRegistry.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
19-
import static com.google.common.base.Throwables.throwIfUnchecked;
2019

2120
import com.google.common.annotations.VisibleForTesting;
2221
import com.google.common.base.MoreObjects;
2322
import com.google.common.base.Objects;
24-
import com.google.common.base.Throwables;
2523
import com.google.common.cache.CacheBuilder;
2624
import com.google.common.cache.CacheLoader;
2725
import com.google.common.cache.LoadingCache;
@@ -34,7 +32,6 @@
3432
import com.google.common.collect.Multimap;
3533
import com.google.common.primitives.Primitives;
3634
import com.google.common.reflect.TypeToken;
37-
import com.google.common.util.concurrent.UncheckedExecutionException;
3835
import com.google.j2objc.annotations.Weak;
3936
import java.lang.reflect.Method;
4037
import java.util.Arrays;
@@ -150,13 +147,7 @@ Iterator<Subscriber> getSubscribers(Object event) {
150147
private static final LoadingCache<Class<?>, ImmutableList<Method>> subscriberMethodsCache =
151148
CacheBuilder.newBuilder()
152149
.weakKeys()
153-
.build(
154-
new CacheLoader<Class<?>, ImmutableList<Method>>() {
155-
@Override
156-
public ImmutableList<Method> load(Class<?> concreteClass) throws Exception {
157-
return getAnnotatedMethodsNotCached(concreteClass);
158-
}
159-
});
150+
.build(CacheLoader.from(SubscriberRegistry::getAnnotatedMethodsNotCached));
160151

161152
/**
162153
* Returns all subscribers for the given listener grouped by the type of event they subscribe to.
@@ -173,12 +164,7 @@ private Multimap<Class<?>, Subscriber> findAllSubscribers(Object listener) {
173164
}
174165

175166
private static ImmutableList<Method> getAnnotatedMethods(Class<?> clazz) {
176-
try {
177-
return subscriberMethodsCache.getUnchecked(clazz);
178-
} catch (UncheckedExecutionException e) {
179-
throwIfUnchecked(e.getCause());
180-
throw e;
181-
}
167+
return subscriberMethodsCache.getUnchecked(clazz);
182168
}
183169

184170
private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
@@ -220,27 +206,17 @@ private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz
220206
CacheBuilder.newBuilder()
221207
.weakKeys()
222208
.build(
223-
new CacheLoader<Class<?>, ImmutableSet<Class<?>>>() {
224-
// <Class<?>> is actually needed to compile
225-
@SuppressWarnings("RedundantTypeArguments")
226-
@Override
227-
public ImmutableSet<Class<?>> load(Class<?> concreteClass) {
228-
return ImmutableSet.<Class<?>>copyOf(
229-
TypeToken.of(concreteClass).getTypes().rawTypes());
230-
}
231-
});
209+
CacheLoader.from(
210+
concreteClass ->
211+
ImmutableSet.copyOf(TypeToken.of(concreteClass).getTypes().rawTypes())));
232212

233213
/**
234214
* Flattens a class's type hierarchy into a set of {@code Class} objects including all
235215
* superclasses (transitively) and all interfaces implemented by these superclasses.
236216
*/
237217
@VisibleForTesting
238218
static ImmutableSet<Class<?>> flattenHierarchy(Class<?> concreteClass) {
239-
try {
240-
return flattenHierarchyCache.getUnchecked(concreteClass);
241-
} catch (UncheckedExecutionException e) {
242-
throw Throwables.propagate(e.getCause());
243-
}
219+
return flattenHierarchyCache.getUnchecked(concreteClass);
244220
}
245221

246222
private static final class MethodIdentifier {

android/guava/src/com/google/common/util/concurrent/MoreExecutors.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
19+
import static com.google.common.base.Throwables.throwIfUnchecked;
1920
import static java.util.Objects.requireNonNull;
2021

2122
import com.google.common.annotations.GwtCompatible;
2223
import com.google.common.annotations.GwtIncompatible;
2324
import com.google.common.annotations.J2ktIncompatible;
2425
import com.google.common.annotations.VisibleForTesting;
2526
import com.google.common.base.Supplier;
26-
import com.google.common.base.Throwables;
2727
import com.google.common.collect.Lists;
2828
import com.google.common.collect.Queues;
2929
import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
3030
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3131
import java.lang.reflect.InvocationTargetException;
32+
import java.lang.reflect.UndeclaredThrowableException;
3233
import java.util.Collection;
3334
import java.util.Iterator;
3435
import java.util.List;
@@ -730,7 +731,9 @@ public static ThreadFactory platformThreadFactory() {
730731
} catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
731732
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
732733
} catch (InvocationTargetException e) {
733-
throw Throwables.propagate(e.getCause());
734+
throwIfUnchecked(e.getCause());
735+
// This should be impossible: `currentRequestThreadFactory` has no `throws` clause.
736+
throw new UndeclaredThrowableException(e.getCause());
734737
}
735738
}
736739

guava-tests/test/com/google/common/eventbus/EventBusTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.google.common.eventbus;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.Assert.assertThrows;
2021

2122
import com.google.common.collect.ImmutableList;
2223
import com.google.common.collect.Lists;
24+
import com.google.common.util.concurrent.UncheckedExecutionException;
2325
import java.util.List;
2426
import java.util.concurrent.ExecutorService;
2527
import java.util.concurrent.Executors;
@@ -281,7 +283,10 @@ class SubscribesToPrimitive {
281283
@Subscribe
282284
public void toInt(int i) {}
283285
}
284-
assertThrows(IllegalArgumentException.class, () -> bus.register(new SubscribesToPrimitive()));
286+
UncheckedExecutionException thrown =
287+
assertThrows(
288+
UncheckedExecutionException.class, () -> bus.register(new SubscribesToPrimitive()));
289+
assertThat(thrown).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
285290
}
286291

287292
/** Records thrown exception information. */

guava/src/com/google/common/eventbus/SubscriberRegistry.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
19-
import static com.google.common.base.Throwables.throwIfUnchecked;
2019

2120
import com.google.common.annotations.VisibleForTesting;
2221
import com.google.common.base.MoreObjects;
2322
import com.google.common.base.Objects;
24-
import com.google.common.base.Throwables;
2523
import com.google.common.cache.CacheBuilder;
2624
import com.google.common.cache.CacheLoader;
2725
import com.google.common.cache.LoadingCache;
@@ -34,7 +32,6 @@
3432
import com.google.common.collect.Multimap;
3533
import com.google.common.primitives.Primitives;
3634
import com.google.common.reflect.TypeToken;
37-
import com.google.common.util.concurrent.UncheckedExecutionException;
3835
import com.google.j2objc.annotations.Weak;
3936
import java.lang.reflect.Method;
4037
import java.util.Arrays;
@@ -150,13 +147,7 @@ Iterator<Subscriber> getSubscribers(Object event) {
150147
private static final LoadingCache<Class<?>, ImmutableList<Method>> subscriberMethodsCache =
151148
CacheBuilder.newBuilder()
152149
.weakKeys()
153-
.build(
154-
new CacheLoader<Class<?>, ImmutableList<Method>>() {
155-
@Override
156-
public ImmutableList<Method> load(Class<?> concreteClass) throws Exception {
157-
return getAnnotatedMethodsNotCached(concreteClass);
158-
}
159-
});
150+
.build(CacheLoader.from(SubscriberRegistry::getAnnotatedMethodsNotCached));
160151

161152
/**
162153
* Returns all subscribers for the given listener grouped by the type of event they subscribe to.
@@ -173,12 +164,7 @@ private Multimap<Class<?>, Subscriber> findAllSubscribers(Object listener) {
173164
}
174165

175166
private static ImmutableList<Method> getAnnotatedMethods(Class<?> clazz) {
176-
try {
177-
return subscriberMethodsCache.getUnchecked(clazz);
178-
} catch (UncheckedExecutionException e) {
179-
throwIfUnchecked(e.getCause());
180-
throw e;
181-
}
167+
return subscriberMethodsCache.getUnchecked(clazz);
182168
}
183169

184170
private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz) {
@@ -220,27 +206,17 @@ private static ImmutableList<Method> getAnnotatedMethodsNotCached(Class<?> clazz
220206
CacheBuilder.newBuilder()
221207
.weakKeys()
222208
.build(
223-
new CacheLoader<Class<?>, ImmutableSet<Class<?>>>() {
224-
// <Class<?>> is actually needed to compile
225-
@SuppressWarnings("RedundantTypeArguments")
226-
@Override
227-
public ImmutableSet<Class<?>> load(Class<?> concreteClass) {
228-
return ImmutableSet.<Class<?>>copyOf(
229-
TypeToken.of(concreteClass).getTypes().rawTypes());
230-
}
231-
});
209+
CacheLoader.from(
210+
concreteClass ->
211+
ImmutableSet.copyOf(TypeToken.of(concreteClass).getTypes().rawTypes())));
232212

233213
/**
234214
* Flattens a class's type hierarchy into a set of {@code Class} objects including all
235215
* superclasses (transitively) and all interfaces implemented by these superclasses.
236216
*/
237217
@VisibleForTesting
238218
static ImmutableSet<Class<?>> flattenHierarchy(Class<?> concreteClass) {
239-
try {
240-
return flattenHierarchyCache.getUnchecked(concreteClass);
241-
} catch (UncheckedExecutionException e) {
242-
throw Throwables.propagate(e.getCause());
243-
}
219+
return flattenHierarchyCache.getUnchecked(concreteClass);
244220
}
245221

246222
private static final class MethodIdentifier {

guava/src/com/google/common/util/concurrent/MoreExecutors.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
19+
import static com.google.common.base.Throwables.throwIfUnchecked;
1920
import static com.google.common.util.concurrent.Internal.toNanosSaturated;
2021
import static java.util.Objects.requireNonNull;
2122

@@ -24,12 +25,12 @@
2425
import com.google.common.annotations.J2ktIncompatible;
2526
import com.google.common.annotations.VisibleForTesting;
2627
import com.google.common.base.Supplier;
27-
import com.google.common.base.Throwables;
2828
import com.google.common.collect.Lists;
2929
import com.google.common.collect.Queues;
3030
import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
3131
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3232
import java.lang.reflect.InvocationTargetException;
33+
import java.lang.reflect.UndeclaredThrowableException;
3334
import java.time.Duration;
3435
import java.util.Collection;
3536
import java.util.Iterator;
@@ -808,7 +809,9 @@ public static ThreadFactory platformThreadFactory() {
808809
} catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
809810
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
810811
} catch (InvocationTargetException e) {
811-
throw Throwables.propagate(e.getCause());
812+
throwIfUnchecked(e.getCause());
813+
// This should be impossible: `currentRequestThreadFactory` has no `throws` clause.
814+
throw new UndeclaredThrowableException(e.getCause());
812815
}
813816
}
814817

0 commit comments

Comments
 (0)