diff --git a/android/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java b/android/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java index 5c907f7cd468..0ad82e917a70 100644 --- a/android/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java +++ b/android/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java @@ -123,7 +123,7 @@ public Builder putAll(Map, ? exten return this; } - private static T cast(Class type, B value) { + private static T cast(Class type, Object value) { return Primitives.wrap(type).cast(value); } diff --git a/android/guava/src/com/google/common/collect/MutableClassToInstanceMap.java b/android/guava/src/com/google/common/collect/MutableClassToInstanceMap.java index 0111ccade791..ea5199561e6e 100644 --- a/android/guava/src/com/google/common/collect/MutableClassToInstanceMap.java +++ b/android/guava/src/com/google/common/collect/MutableClassToInstanceMap.java @@ -96,7 +96,8 @@ private MutableClassToInstanceMap(Map, B> delegate) @Override @ParametricNullness public B setValue(@ParametricNullness B value) { - return super.setValue(cast(getKey(), value)); + cast(getKey(), value); + return super.setValue(value); } }; } @@ -148,7 +149,8 @@ public Object[] toArray() { @CanIgnoreReturnValue @CheckForNull public B put(Class key, @ParametricNullness B value) { - return super.put(key, cast(key, value)); + cast(key, value); + return super.put(key, value); } @Override @@ -175,7 +177,7 @@ public T putInstance(Class<@NonNull T> type, @ParametricNullness T @CanIgnoreReturnValue @CheckForNull - private static T cast(Class<@NonNull T> type, @CheckForNull B value) { + private static T cast(Class type, @CheckForNull Object value) { return Primitives.wrap(type).cast(value); } diff --git a/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java b/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java index b3b22865095b..450d2b296bda 100644 --- a/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java +++ b/android/guava/src/com/google/common/util/concurrent/MoreExecutors.java @@ -16,6 +16,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import com.google.common.annotations.GwtCompatible; @@ -891,7 +892,8 @@ private static boolean isAppEngineWithApiClasses() { static Thread newThread(String name, Runnable runnable) { checkNotNull(name); checkNotNull(runnable); - Thread result = platformThreadFactory().newThread(runnable); + // TODO(b/139726489): Confirm that null is impossible here. + Thread result = requireNonNull(platformThreadFactory().newThread(runnable)); try { result.setName(name); } catch (SecurityException e) { diff --git a/android/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java b/android/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java index c0116f80b3d1..80727c59a8d7 100644 --- a/android/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java +++ b/android/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java @@ -171,6 +171,8 @@ private static ThreadFactory doBuild(ThreadFactoryBuilder builder) { @Override public Thread newThread(Runnable runnable) { Thread thread = backingThreadFactory.newThread(runnable); + // TODO(b/139735208): Figure out what to do when the factory returns null. + requireNonNull(thread); if (nameFormat != null) { // requireNonNull is safe because we create `count` if (and only if) we have a nameFormat. thread.setName(format(nameFormat, requireNonNull(count).getAndIncrement())); diff --git a/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java b/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java index 5c907f7cd468..0ad82e917a70 100644 --- a/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java +++ b/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java @@ -123,7 +123,7 @@ public Builder putAll(Map, ? exten return this; } - private static T cast(Class type, B value) { + private static T cast(Class type, Object value) { return Primitives.wrap(type).cast(value); } diff --git a/guava/src/com/google/common/collect/MutableClassToInstanceMap.java b/guava/src/com/google/common/collect/MutableClassToInstanceMap.java index 740d66715bcc..98b4026142e3 100644 --- a/guava/src/com/google/common/collect/MutableClassToInstanceMap.java +++ b/guava/src/com/google/common/collect/MutableClassToInstanceMap.java @@ -97,7 +97,8 @@ private MutableClassToInstanceMap(Map, B> delegate) @Override @ParametricNullness public B setValue(@ParametricNullness B value) { - return super.setValue(cast(getKey(), value)); + cast(getKey(), value); + return super.setValue(value); } }; } @@ -155,7 +156,8 @@ public Object[] toArray() { @CanIgnoreReturnValue @CheckForNull public B put(Class key, @ParametricNullness B value) { - return super.put(key, cast(key, value)); + cast(key, value); + return super.put(key, value); } @Override @@ -182,7 +184,7 @@ public T putInstance(Class<@NonNull T> type, @ParametricNullness T @CanIgnoreReturnValue @CheckForNull - private static T cast(Class<@NonNull T> type, @CheckForNull B value) { + private static T cast(Class type, @CheckForNull Object value) { return Primitives.wrap(type).cast(value); } diff --git a/guava/src/com/google/common/util/concurrent/MoreExecutors.java b/guava/src/com/google/common/util/concurrent/MoreExecutors.java index 55a778a5c213..d244254eb513 100644 --- a/guava/src/com/google/common/util/concurrent/MoreExecutors.java +++ b/guava/src/com/google/common/util/concurrent/MoreExecutors.java @@ -17,6 +17,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.util.concurrent.Internal.toNanosSaturated; +import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import com.google.common.annotations.GwtCompatible; @@ -972,7 +973,8 @@ private static boolean isAppEngineWithApiClasses() { static Thread newThread(String name, Runnable runnable) { checkNotNull(name); checkNotNull(runnable); - Thread result = platformThreadFactory().newThread(runnable); + // TODO(b/139726489): Confirm that null is impossible here. + Thread result = requireNonNull(platformThreadFactory().newThread(runnable)); try { result.setName(name); } catch (SecurityException e) { diff --git a/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java b/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java index c0116f80b3d1..80727c59a8d7 100644 --- a/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java +++ b/guava/src/com/google/common/util/concurrent/ThreadFactoryBuilder.java @@ -171,6 +171,8 @@ private static ThreadFactory doBuild(ThreadFactoryBuilder builder) { @Override public Thread newThread(Runnable runnable) { Thread thread = backingThreadFactory.newThread(runnable); + // TODO(b/139735208): Figure out what to do when the factory returns null. + requireNonNull(thread); if (nameFormat != null) { // requireNonNull is safe because we create `count` if (and only if) we have a nameFormat. thread.setName(format(nameFormat, requireNonNull(count).getAndIncrement()));