Skip to content

Commit

Permalink
Update code for forthcoming nullness annotations for JDK classes.
Browse files Browse the repository at this point in the history
The Guava changes include further annotations [from "On Leveraging Tests to Infer Nullable Annotations."](#6510)

RELNOTES=n/a
PiperOrigin-RevId: 559605577
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Aug 24, 2023
1 parent bf874f1 commit f39084a
Show file tree
Hide file tree
Showing 23 changed files with 124 additions and 56 deletions.
8 changes: 7 additions & 1 deletion android/guava/src/com/google/common/cache/LocalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ class LocalCache<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V>
// will result in random eviction behavior.
int segmentShift = 0;
int segmentCount = 1;
while (segmentCount < concurrencyLevel && (!evictsBySize() || segmentCount * 20 <= maxWeight)) {
while (segmentCount < concurrencyLevel
&& (!evictsBySize() || segmentCount * 20L <= maxWeight)) {
++segmentShift;
segmentCount <<= 1;
}
Expand Down Expand Up @@ -3430,6 +3431,11 @@ public LoadingValueReference() {
this(LocalCache.<K, V>unset());
}

/*
* TODO(cpovirk): Consider making this implementation closer to the mainline implementation.
* (The difference was introduced as part of Java-8-specific changes in cl/132882204, but we
* could probably make *some* of those changes here in the backport, too.)
*/
public LoadingValueReference(ValueReference<K, V> oldValue) {
this.oldValue = oldValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -478,7 +479,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
setInverse((AbstractBiMap<V, K>) stream.readObject());
setInverse((AbstractBiMap<V, K>) requireNonNull(stream.readObject()));
}

@GwtIncompatible // Not needed in the emulated source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand Down Expand Up @@ -599,7 +600,7 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
stream.defaultReadObject();
@SuppressWarnings("unchecked") // reading data stored by writeObject
ConcurrentMap<E, Integer> deserializedCountMap =
(ConcurrentMap<E, Integer>) stream.readObject();
(ConcurrentMap<E, Integer>) requireNonNull(stream.readObject());
FieldSettersHolder.COUNT_MAP_FIELD_SETTER.set(this, deserializedCountMap);
}

Expand Down
5 changes: 3 additions & 2 deletions android/guava/src/com/google/common/collect/EnumBiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Platform.getDeclaringClassOrObjectForJ2cl;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -151,8 +152,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@GwtIncompatible // java.io.ObjectInputStream
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) stream.readObject();
valueTypeOrObjectUnderJ2cl = (Class<V>) stream.readObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) requireNonNull(stream.readObject());
valueTypeOrObjectUnderJ2cl = (Class<V>) requireNonNull(stream.readObject());
setDelegates(
new EnumMap<K, V>(keyTypeOrObjectUnderJ2cl), new EnumMap<V, K>(valueTypeOrObjectUnderJ2cl));
Serialization.populateMap(this, stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -128,7 +129,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@GwtIncompatible // java.io.ObjectInputStream
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) stream.readObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) requireNonNull(stream.readObject());
/*
* TODO: cpovirk - Pre-size the HashMap based on the number of enum values? (But *not* based on
* the number of entries in the map, as that makes it easy for hostile inputs to trigger lots of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -298,7 +299,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
@SuppressWarnings("unchecked") // reading data stored by writeObject
Class<E> localType = (Class<E>) stream.readObject();
Class<E> localType = (Class<E>) requireNonNull(stream.readObject());
type = localType;
enumConstants = type.getEnumConstants();
counts = new int[enumConstants.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.collect;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand Down Expand Up @@ -397,15 +399,15 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
int tmpSize = 0;

for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
Object key = requireNonNull(stream.readObject());
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}

ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder();
for (int j = 0; j < valueCount; j++) {
valuesBuilder.add(stream.readObject());
valuesBuilder.add(requireNonNull(stream.readObject()));
}
builder.put(key, valuesBuilder.build());
tmpSize += valueCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -530,15 +531,15 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
int tmpSize = 0;

for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
Object key = requireNonNull(stream.readObject());
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}

ImmutableSet.Builder<Object> valuesBuilder = valuesBuilder(valueComparator);
for (int j = 0; j < valueCount; j++) {
valuesBuilder.add(stream.readObject());
valuesBuilder.add(requireNonNull(stream.readObject()));
}
ImmutableSet<Object> valueSet = valuesBuilder.build();
if (valueSet.size() != valueCount) {
Expand Down
16 changes: 8 additions & 8 deletions android/guava/src/com/google/common/collect/Multimaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
factory = (Supplier<? extends Collection<V>>) stream.readObject();
Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
factory = (Supplier<? extends Collection<V>>) requireNonNull(stream.readObject());
Map<K, Collection<V>> map = (Map<K, Collection<V>>) requireNonNull(stream.readObject());
setMap(map);
}

Expand Down Expand Up @@ -273,8 +273,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
factory = (Supplier<? extends List<V>>) stream.readObject();
Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
factory = (Supplier<? extends List<V>>) requireNonNull(stream.readObject());
Map<K, Collection<V>> map = (Map<K, Collection<V>>) requireNonNull(stream.readObject());
setMap(map);
}

Expand Down Expand Up @@ -381,8 +381,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
factory = (Supplier<? extends Set<V>>) stream.readObject();
Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
factory = (Supplier<? extends Set<V>>) requireNonNull(stream.readObject());
Map<K, Collection<V>> map = (Map<K, Collection<V>>) requireNonNull(stream.readObject());
setMap(map);
}

Expand Down Expand Up @@ -475,9 +475,9 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
factory = (Supplier<? extends SortedSet<V>>) stream.readObject();
factory = (Supplier<? extends SortedSet<V>>) requireNonNull(stream.readObject());
valueComparator = factory.get().comparator();
Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
Map<K, Collection<V>> map = (Map<K, Collection<V>>) requireNonNull(stream.readObject());
setMap(map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
stream.defaultReadObject();
@SuppressWarnings("unchecked")
// reading data stored by writeObject
Comparator<? super E> comparator = (Comparator<? super E>) stream.readObject();
Comparator<? super E> comparator = (Comparator<? super E>) requireNonNull(stream.readObject());
Serialization.getFieldSetter(AbstractSortedMultiset.class, "comparator").set(this, comparator);
Serialization.getFieldSetter(TreeMultiset.class, "range")
.set(this, GeneralRange.all(comparator));
Expand Down
23 changes: 17 additions & 6 deletions guava/src/com/google/common/cache/LocalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ class LocalCache<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V>
// will result in random eviction behavior.
int segmentShift = 0;
int segmentCount = 1;
while (segmentCount < concurrencyLevel && (!evictsBySize() || segmentCount * 20 <= maxWeight)) {
while (segmentCount < concurrencyLevel
&& (!evictsBySize() || segmentCount * 20L <= maxWeight)) {
++segmentShift;
segmentCount <<= 1;
}
Expand Down Expand Up @@ -2220,7 +2221,11 @@ V waitForLoadingValue(ReferenceEntry<K, V> e, K key, ValueReference<K, V> valueR
}
}

V compute(K key, int hash, BiFunction<? super K, ? super @Nullable V, ? extends V> function) {
@CheckForNull
V compute(
K key,
int hash,
BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> function) {
ReferenceEntry<K, V> e;
ValueReference<K, V> valueReference = null;
ComputingValueReference<K, V> computingValueReference = null;
Expand Down Expand Up @@ -3516,7 +3521,7 @@ public LoadingValueReference() {
this(null);
}

public LoadingValueReference(ValueReference<K, V> oldValue) {
public LoadingValueReference(@CheckForNull ValueReference<K, V> oldValue) {
this.oldValue = (oldValue == null) ? LocalCache.unset() : oldValue;
}

Expand Down Expand Up @@ -3593,7 +3598,9 @@ public ListenableFuture<V> loadFuture(K key, CacheLoader<? super K, V> loader) {
}
}

public V compute(K key, BiFunction<? super K, ? super @Nullable V, ? extends V> function) {
@CheckForNull
public V compute(
K key, BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> function) {
stopwatch.start();
V previousValue;
try {
Expand Down Expand Up @@ -4255,7 +4262,9 @@ public V putIfAbsent(K key, V value) {
}

@Override
public V compute(K key, BiFunction<? super K, ? super @Nullable V, ? extends V> function) {
@CheckForNull
public V compute(
K key, BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> function) {
checkNotNull(key);
checkNotNull(function);
int hash = hash(key);
Expand All @@ -4270,7 +4279,9 @@ public V computeIfAbsent(K key, Function<? super K, ? extends V> function) {
}

@Override
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> function) {
@CheckForNull
public V computeIfPresent(
K key, BiFunction<? super K, ? super V, ? extends @Nullable V> function) {
checkNotNull(key);
checkNotNull(function);
return compute(key, (k, oldValue) -> (oldValue == null) ? null : function.apply(k, oldValue));
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/AbstractBiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -502,7 +503,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@SuppressWarnings("unchecked") // reading data stored by writeObject
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
setInverse((AbstractBiMap<V, K>) stream.readObject());
setInverse((AbstractBiMap<V, K>) requireNonNull(stream.readObject()));
}

@GwtIncompatible // Not needed in the emulated source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand Down Expand Up @@ -599,7 +600,7 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
stream.defaultReadObject();
@SuppressWarnings("unchecked") // reading data stored by writeObject
ConcurrentMap<E, Integer> deserializedCountMap =
(ConcurrentMap<E, Integer>) stream.readObject();
(ConcurrentMap<E, Integer>) requireNonNull(stream.readObject());
FieldSettersHolder.COUNT_MAP_FIELD_SETTER.set(this, deserializedCountMap);
}

Expand Down
5 changes: 3 additions & 2 deletions guava/src/com/google/common/collect/EnumBiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Platform.getDeclaringClassOrObjectForJ2cl;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -151,8 +152,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@GwtIncompatible // java.io.ObjectInputStream
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) stream.readObject();
valueTypeOrObjectUnderJ2cl = (Class<V>) stream.readObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) requireNonNull(stream.readObject());
valueTypeOrObjectUnderJ2cl = (Class<V>) requireNonNull(stream.readObject());
setDelegates(
new EnumMap<K, V>(keyTypeOrObjectUnderJ2cl), new EnumMap<V, K>(valueTypeOrObjectUnderJ2cl));
Serialization.populateMap(this, stream);
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/EnumHashBiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -128,7 +129,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@GwtIncompatible // java.io.ObjectInputStream
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) stream.readObject();
keyTypeOrObjectUnderJ2cl = (Class<K>) requireNonNull(stream.readObject());
/*
* TODO: cpovirk - Pre-size the HashMap based on the number of enum values? (But *not* based on
* the number of entries in the map, as that makes it easy for hostile inputs to trigger lots of
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/EnumMultiset.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.CollectPreconditions.checkRemove;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -309,7 +310,7 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
@SuppressWarnings("unchecked") // reading data stored by writeObject
Class<E> localType = (Class<E>) stream.readObject();
Class<E> localType = (Class<E>) requireNonNull(stream.readObject());
type = localType;
enumConstants = type.getEnumConstants();
counts = new int[enumConstants.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.collect;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand Down Expand Up @@ -473,15 +475,15 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
int tmpSize = 0;

for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
Object key = requireNonNull(stream.readObject());
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}

ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder();
for (int j = 0; j < valueCount; j++) {
valuesBuilder.add(stream.readObject());
valuesBuilder.add(requireNonNull(stream.readObject()));
}
builder.put(key, valuesBuilder.build());
tmpSize += valueCount;
Expand Down
Loading

0 comments on commit f39084a

Please sign in to comment.