Skip to content

Commit

Permalink
More reliably use transitional @CheckForNull and `@ParametricNullne…
Browse files Browse the repository at this point in the history
…ss` annotations.

I would assume we're still not 100% consistent in using them, and we might someday consider an Error Prone check to identify the remaining places that we ought to be using them. But in an ideal world, they'll be able to go away entirely before we need to bother.

(There are a few reasons to use these annotations, but the one that I was poking at today was trying to [run NullAway](#2484 (comment)).)

RELNOTES=n/a
PiperOrigin-RevId: 494880081
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Dec 13, 2022
1 parent 732ba2a commit 049867c
Show file tree
Hide file tree
Showing 22 changed files with 77 additions and 49 deletions.
16 changes: 8 additions & 8 deletions android/guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
import javax.annotation.CheckForNull;

/**
* A builder of {@link LoadingCache} and {@link Cache} instances.
Expand Down Expand Up @@ -276,10 +276,10 @@ public long read() {
int concurrencyLevel = UNSET_INT;
long maximumSize = UNSET_INT;
long maximumWeight = UNSET_INT;
@Nullable Weigher<? super K, ? super V> weigher;
@CheckForNull Weigher<? super K, ? super V> weigher;

@Nullable Strength keyStrength;
@Nullable Strength valueStrength;
@CheckForNull Strength keyStrength;
@CheckForNull Strength valueStrength;

@SuppressWarnings("GoodTime") // should be a java.time.Duration
long expireAfterWriteNanos = UNSET_INT;
Expand All @@ -290,11 +290,11 @@ public long read() {
@SuppressWarnings("GoodTime") // should be a java.time.Duration
long refreshNanos = UNSET_INT;

@Nullable Equivalence<Object> keyEquivalence;
@Nullable Equivalence<Object> valueEquivalence;
@CheckForNull Equivalence<Object> keyEquivalence;
@CheckForNull Equivalence<Object> valueEquivalence;

@Nullable RemovalListener<? super K, ? super V> removalListener;
@Nullable Ticker ticker;
@CheckForNull RemovalListener<? super K, ? super V> removalListener;
@CheckForNull Ticker ticker;

Supplier<? extends StatsCounter> statsCounterSupplier = NULL_STATS_COUNTER;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ public boolean hasNext() {
}

@Override
@ParametricNullness
public T next() {
if (!valueIterator.hasNext()) {
Entry<K, Collection<V>> mapEntry = keyIterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A {@link ListMultimap} whose contents will never change, with many other important properties
Expand Down Expand Up @@ -280,7 +279,7 @@ public static <K, V> ImmutableListMultimap<K, V> copyOf(
/** Creates an ImmutableListMultimap from an asMap.entrySet. */
static <K, V> ImmutableListMultimap<K, V> fromMapEntries(
Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries,
@Nullable Comparator<? super V> valueComparator) {
@CheckForNull Comparator<? super V> valueComparator) {
if (mapEntries.isEmpty()) {
return of();
}
Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,7 @@ public interface EntryTransformer<
* @throws NullPointerException if the key or value is null and this transformer does not accept
* null arguments
*/
@ParametricNullness
V2 transformEntry(@ParametricNullness K key, @ParametricNullness V1 value);
}

Expand Down
25 changes: 14 additions & 11 deletions android/guava/src/com/google/common/collect/Synchronized.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public int count(@CheckForNull Object o) {
}

@Override
public int add(E e, int n) {
public int add(@ParametricNullness E e, int n) {
synchronized (mutex) {
return delegate().add(e, n);
}
Expand All @@ -473,14 +473,14 @@ public int remove(@CheckForNull Object o, int n) {
}

@Override
public int setCount(E element, int count) {
public int setCount(@ParametricNullness E element, int count) {
synchronized (mutex) {
return delegate().setCount(element, count);
}
}

@Override
public boolean setCount(E element, int oldCount, int newCount) {
public boolean setCount(@ParametricNullness E element, int oldCount, int newCount) {
synchronized (mutex) {
return delegate().setCount(element, oldCount, newCount);
}
Expand Down Expand Up @@ -588,21 +588,21 @@ public boolean containsEntry(@CheckForNull Object key, @CheckForNull Object valu
}

@Override
public Collection<V> get(K key) {
public Collection<V> get(@ParametricNullness K key) {
synchronized (mutex) {
return typePreservingCollection(delegate().get(key), mutex);
}
}

@Override
public boolean put(K key, V value) {
public boolean put(@ParametricNullness K key, @ParametricNullness V value) {
synchronized (mutex) {
return delegate().put(key, value);
}
}

@Override
public boolean putAll(K key, Iterable<? extends V> values) {
public boolean putAll(@ParametricNullness K key, Iterable<? extends V> values) {
synchronized (mutex) {
return delegate().putAll(key, values);
}
Expand All @@ -616,7 +616,7 @@ public boolean putAll(Multimap<? extends K, ? extends V> multimap) {
}

@Override
public Collection<V> replaceValues(K key, Iterable<? extends V> values) {
public Collection<V> replaceValues(@ParametricNullness K key, Iterable<? extends V> values) {
synchronized (mutex) {
return delegate().replaceValues(key, values); // copy not synchronized
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ public Set<V> values() {

@Override
@CheckForNull
public V forcePut(K key, V value) {
public V forcePut(@ParametricNullness K key, @ParametricNullness V value) {
synchronized (mutex) {
return delegate().forcePut(key, value);
}
Expand Down Expand Up @@ -1989,7 +1989,10 @@ public void clear() {

@Override
@CheckForNull
public V put(R rowKey, C columnKey, V value) {
public V put(
@ParametricNullness R rowKey,
@ParametricNullness C columnKey,
@ParametricNullness V value) {
synchronized (mutex) {
return delegate().put(rowKey, columnKey, value);
}
Expand All @@ -2011,14 +2014,14 @@ public V remove(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
}

@Override
public Map<C, V> row(R rowKey) {
public Map<C, V> row(@ParametricNullness R rowKey) {
synchronized (mutex) {
return map(delegate().row(rowKey), mutex);
}
}

@Override
public Map<R, V> column(C columnKey) {
public Map<R, V> column(@ParametricNullness C columnKey) {
synchronized (mutex) {
return map(delegate().column(columnKey), mutex);
}
Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/io/ByteSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public byte[] read() throws IOException {
*/
@Beta
@CanIgnoreReturnValue // some processors won't return a useful result
@ParametricNullness
public <T extends @Nullable Object> T read(ByteProcessor<T> processor) throws IOException {
checkNotNull(processor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ static final class ListFuture<V extends @Nullable Object>

/** The result of a successful {@code Future}. */
private static final class Present<V extends @Nullable Object> {
final V value;
@ParametricNullness final V value;

Present(V value) {
Present(@ParametricNullness V value) {
this.value = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Implementation of {@link Futures#immediateFuture}. */
Expand Down Expand Up @@ -98,7 +99,8 @@ static final class ImmediateFailedFuture<V extends @Nullable Object> extends Tru
}

static final class ImmediateCancelledFuture<V extends @Nullable Object> extends TrustedFuture<V> {
static final @Nullable ImmediateCancelledFuture<Object> INSTANCE =
@CheckForNull
static final ImmediateCancelledFuture<Object> INSTANCE =
AbstractFuture.GENERATE_CANCELLATION_CAUSES ? null : new ImmediateCancelledFuture<>();

ImmediateCancelledFuture() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler)
return interfaceType.cast(object);
}

@ParametricNullness
private <T extends @Nullable Object> T callWithTimeout(
Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible)
throws Exception {
Expand Down Expand Up @@ -141,6 +142,7 @@ private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler)

@CanIgnoreReturnValue
@Override
@ParametricNullness
public <T extends @Nullable Object> T callWithTimeout(
Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
throws TimeoutException, InterruptedException, ExecutionException {
Expand All @@ -163,6 +165,7 @@ private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler)

@CanIgnoreReturnValue
@Override
@ParametricNullness
public <T extends @Nullable Object> T callUninterruptiblyWithTimeout(
Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
throws TimeoutException, ExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public interface TimeLimiter {
* @since 22.0
*/
@CanIgnoreReturnValue
@ParametricNullness
<T extends @Nullable Object> T callWithTimeout(
Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
throws TimeoutException, InterruptedException, ExecutionException;
Expand All @@ -123,6 +124,7 @@ public interface TimeLimiter {
* @since 22.0
*/
@CanIgnoreReturnValue
@ParametricNullness
<T extends @Nullable Object> T callUninterruptiblyWithTimeout(
Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)
throws TimeoutException, ExecutionException;
Expand Down
16 changes: 8 additions & 8 deletions guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
import javax.annotation.CheckForNull;

/**
* A builder of {@link LoadingCache} and {@link Cache} instances.
Expand Down Expand Up @@ -277,10 +277,10 @@ public long read() {
int concurrencyLevel = UNSET_INT;
long maximumSize = UNSET_INT;
long maximumWeight = UNSET_INT;
@Nullable Weigher<? super K, ? super V> weigher;
@CheckForNull Weigher<? super K, ? super V> weigher;

@Nullable Strength keyStrength;
@Nullable Strength valueStrength;
@CheckForNull Strength keyStrength;
@CheckForNull Strength valueStrength;

@SuppressWarnings("GoodTime") // should be a java.time.Duration
long expireAfterWriteNanos = UNSET_INT;
Expand All @@ -291,11 +291,11 @@ public long read() {
@SuppressWarnings("GoodTime") // should be a java.time.Duration
long refreshNanos = UNSET_INT;

@Nullable Equivalence<Object> keyEquivalence;
@Nullable Equivalence<Object> valueEquivalence;
@CheckForNull Equivalence<Object> keyEquivalence;
@CheckForNull Equivalence<Object> valueEquivalence;

@Nullable RemovalListener<? super K, ? super V> removalListener;
@Nullable Ticker ticker;
@CheckForNull RemovalListener<? super K, ? super V> removalListener;
@CheckForNull Ticker ticker;

Supplier<? extends StatsCounter> statsCounterSupplier = NULL_STATS_COUNTER;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ public boolean hasNext() {
}

@Override
@ParametricNullness
public T next() {
if (!valueIterator.hasNext()) {
Entry<K, Collection<V>> mapEntry = keyIterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static <K, V> ImmutableListMultimap<K, V> copyOf(
/** Creates an ImmutableListMultimap from an asMap.entrySet. */
static <K, V> ImmutableListMultimap<K, V> fromMapEntries(
Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries,
@Nullable Comparator<? super V> valueComparator) {
@CheckForNull Comparator<? super V> valueComparator) {
if (mapEntries.isEmpty()) {
return of();
}
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/collect/ImmutableSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ static int chooseTableSize(int setSize) {
*/
private static final class RegularSetBuilderImpl<E> extends SetBuilderImpl<E> {
// null until at least two elements are present
private @Nullable Object @Nullable [] hashTable;
@CheckForNull private @Nullable Object[] hashTable;
private int maxRunBeforeFallback;
private int expandTableThreshold;
private int hashCode;
Expand Down
1 change: 1 addition & 0 deletions guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,7 @@ public interface EntryTransformer<
* @throws NullPointerException if the key or value is null and this transformer does not accept
* null arguments
*/
@ParametricNullness
V2 transformEntry(@ParametricNullness K key, @ParametricNullness V1 value);
}

Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/MoreCollectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collector;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public final class MoreCollectors {
private static final class ToOptionalState {
static final int MAX_EXTRAS = 4;

@Nullable Object element;
@CheckForNull Object element;
List<Object> extras;

ToOptionalState() {
Expand Down

0 comments on commit 049867c

Please sign in to comment.