Skip to content

Commit cba0b0a

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Fix/suppress a few nullness mismatches.
We could still stand to give actual thought to the `util.concurrent` mismatches: - #3566 - #3568 Those two mismatches will be detected when we begin using a checker that contains [an annotated copy of `ThreadFactory`](jspecify/jdk@24191c6). The mismatch in `MutableClassToInstanceMap` is currently not detected. That's a bug. But for some reason, it _is_ detected when we use type-use anntotations. (I included _additional_ edits to `MutableClassToInstanceMap` and `ImmutableClassToInstanceMap`—specifically, in their `cast` methods. Those changes aren't necessary to the main change here. I had just started to change them to be consistent with the principle we'd discussed in cl/526184065, which is to use a non-null bound for a type parameter if all its usages would otherwise be projected. And then I realized that the second type parameter was serving no purpose, so I simplified further.) RELNOTES=n/a PiperOrigin-RevId: 531012514
1 parent 667a9d4 commit cba0b0a

File tree

8 files changed

+22
-10
lines changed

8 files changed

+22
-10
lines changed

android/guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public <T extends B> Builder<B> putAll(Map<? extends Class<? extends T>, ? exten
123123
return this;
124124
}
125125

126-
private static <B, T extends B> T cast(Class<T> type, B value) {
126+
private static <T> T cast(Class<T> type, Object value) {
127127
return Primitives.wrap(type).cast(value);
128128
}
129129

android/guava/src/com/google/common/collect/MutableClassToInstanceMap.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ private MutableClassToInstanceMap(Map<Class<? extends @NonNull B>, B> delegate)
9696
@Override
9797
@ParametricNullness
9898
public B setValue(@ParametricNullness B value) {
99-
return super.setValue(cast(getKey(), value));
99+
cast(getKey(), value);
100+
return super.setValue(value);
100101
}
101102
};
102103
}
@@ -148,7 +149,8 @@ public Object[] toArray() {
148149
@CanIgnoreReturnValue
149150
@CheckForNull
150151
public B put(Class<? extends @NonNull B> key, @ParametricNullness B value) {
151-
return super.put(key, cast(key, value));
152+
cast(key, value);
153+
return super.put(key, value);
152154
}
153155

154156
@Override
@@ -175,7 +177,7 @@ public <T extends B> T putInstance(Class<@NonNull T> type, @ParametricNullness T
175177

176178
@CanIgnoreReturnValue
177179
@CheckForNull
178-
private static <B, T extends B> T cast(Class<@NonNull T> type, @CheckForNull B value) {
180+
private static <T> T cast(Class<T> type, @CheckForNull Object value) {
179181
return Primitives.wrap(type).cast(value);
180182
}
181183

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

+3-1
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 java.util.Objects.requireNonNull;
1920

2021
import com.google.common.annotations.Beta;
2122
import com.google.common.annotations.GwtCompatible;
@@ -891,7 +892,8 @@ private static boolean isAppEngineWithApiClasses() {
891892
static Thread newThread(String name, Runnable runnable) {
892893
checkNotNull(name);
893894
checkNotNull(runnable);
894-
Thread result = platformThreadFactory().newThread(runnable);
895+
// TODO(b/139726489): Confirm that null is impossible here.
896+
Thread result = requireNonNull(platformThreadFactory().newThread(runnable));
895897
try {
896898
result.setName(name);
897899
} catch (SecurityException e) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ private static ThreadFactory doBuild(ThreadFactoryBuilder builder) {
171171
@Override
172172
public Thread newThread(Runnable runnable) {
173173
Thread thread = backingThreadFactory.newThread(runnable);
174+
// TODO(b/139735208): Figure out what to do when the factory returns null.
175+
requireNonNull(thread);
174176
if (nameFormat != null) {
175177
// requireNonNull is safe because we create `count` if (and only if) we have a nameFormat.
176178
thread.setName(format(nameFormat, requireNonNull(count).getAndIncrement()));

guava/src/com/google/common/collect/ImmutableClassToInstanceMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public <T extends B> Builder<B> putAll(Map<? extends Class<? extends T>, ? exten
123123
return this;
124124
}
125125

126-
private static <B, T extends B> T cast(Class<T> type, B value) {
126+
private static <T> T cast(Class<T> type, Object value) {
127127
return Primitives.wrap(type).cast(value);
128128
}
129129

guava/src/com/google/common/collect/MutableClassToInstanceMap.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ private MutableClassToInstanceMap(Map<Class<? extends @NonNull B>, B> delegate)
9797
@Override
9898
@ParametricNullness
9999
public B setValue(@ParametricNullness B value) {
100-
return super.setValue(cast(getKey(), value));
100+
cast(getKey(), value);
101+
return super.setValue(value);
101102
}
102103
};
103104
}
@@ -155,7 +156,8 @@ public Object[] toArray() {
155156
@CanIgnoreReturnValue
156157
@CheckForNull
157158
public B put(Class<? extends @NonNull B> key, @ParametricNullness B value) {
158-
return super.put(key, cast(key, value));
159+
cast(key, value);
160+
return super.put(key, value);
159161
}
160162

161163
@Override
@@ -182,7 +184,7 @@ public <T extends B> T putInstance(Class<@NonNull T> type, @ParametricNullness T
182184

183185
@CanIgnoreReturnValue
184186
@CheckForNull
185-
private static <B, T extends B> T cast(Class<@NonNull T> type, @CheckForNull B value) {
187+
private static <T> T cast(Class<T> type, @CheckForNull Object value) {
186188
return Primitives.wrap(type).cast(value);
187189
}
188190

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919
import static com.google.common.util.concurrent.Internal.toNanosSaturated;
20+
import static java.util.Objects.requireNonNull;
2021

2122
import com.google.common.annotations.Beta;
2223
import com.google.common.annotations.GwtCompatible;
@@ -972,7 +973,8 @@ private static boolean isAppEngineWithApiClasses() {
972973
static Thread newThread(String name, Runnable runnable) {
973974
checkNotNull(name);
974975
checkNotNull(runnable);
975-
Thread result = platformThreadFactory().newThread(runnable);
976+
// TODO(b/139726489): Confirm that null is impossible here.
977+
Thread result = requireNonNull(platformThreadFactory().newThread(runnable));
976978
try {
977979
result.setName(name);
978980
} catch (SecurityException e) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ private static ThreadFactory doBuild(ThreadFactoryBuilder builder) {
171171
@Override
172172
public Thread newThread(Runnable runnable) {
173173
Thread thread = backingThreadFactory.newThread(runnable);
174+
// TODO(b/139735208): Figure out what to do when the factory returns null.
175+
requireNonNull(thread);
174176
if (nameFormat != null) {
175177
// requireNonNull is safe because we create `count` if (and only if) we have a nameFormat.
176178
thread.setName(format(nameFormat, requireNonNull(count).getAndIncrement()));

0 commit comments

Comments
 (0)