Annotate some forwarding classes for nullness.
RELNOTES=n/a
PiperOrigin-RevId: 382143120
diff --git a/android/guava/src/com/google/common/collect/ConcurrentHashMultiset.java b/android/guava/src/com/google/common/collect/ConcurrentHashMultiset.java
index 2ed6ae5..4c61b03 100644
--- a/android/guava/src/com/google/common/collect/ConcurrentHashMultiset.java
+++ b/android/guava/src/com/google/common/collect/ConcurrentHashMultiset.java
@@ -170,11 +170,7 @@
}
@Override
- /*
- * Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
- */
- @SuppressWarnings("nullness")
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return snapshot().toArray(array);
}
@@ -579,11 +575,7 @@
}
@Override
- /*
- * Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
- */
- @SuppressWarnings("nullness")
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return snapshot().toArray(array);
}
diff --git a/android/guava/src/com/google/common/collect/DescendingMultiset.java b/android/guava/src/com/google/common/collect/DescendingMultiset.java
index a096384..ec5a1d0 100644
--- a/android/guava/src/com/google/common/collect/DescendingMultiset.java
+++ b/android/guava/src/com/google/common/collect/DescendingMultiset.java
@@ -156,11 +156,7 @@
}
@Override
- /*
- * Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
- */
- @SuppressWarnings("nullness")
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return standardToArray(array);
}
diff --git a/android/guava/src/com/google/common/collect/EvictingQueue.java b/android/guava/src/com/google/common/collect/EvictingQueue.java
index 37a65f3..e5c73b1 100644
--- a/android/guava/src/com/google/common/collect/EvictingQueue.java
+++ b/android/guava/src/com/google/common/collect/EvictingQueue.java
@@ -136,6 +136,21 @@
return delegate().remove(checkNotNull(object));
}
+ @Override
+ public Object[] toArray() {
+ /*
+ * If we could, we'd declare the no-arg `Collection.toArray()` to return "Object[] but elements
+ * have the same nullness as E." Since we can't, we declare it to return nullable elements, and
+ * we can override it in our non-null-guaranteeing subtypes to present a better signature to
+ * their users.
+ *
+ * However, the checker *we* use has this special knowledge about `Collection.toArray()` anyway,
+ * so in our implementation code, we can rely on that. That's why the expression below
+ * type-checks.
+ */
+ return super.toArray();
+ }
+
// TODO(kak): Do we want to checkNotNull each element in containsAll, removeAll, and retainAll?
private static final long serialVersionUID = 0L;
diff --git a/android/guava/src/com/google/common/collect/ForwardingBlockingDeque.java b/android/guava/src/com/google/common/collect/ForwardingBlockingDeque.java
index 7d3895d..49d4bcf 100644
--- a/android/guava/src/com/google/common/collect/ForwardingBlockingDeque.java
+++ b/android/guava/src/com/google/common/collect/ForwardingBlockingDeque.java
@@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;
+import javax.annotation.CheckForNull;
/**
* A {@link BlockingDeque} which forwards all its method calls to another {@code BlockingDeque}.
@@ -46,6 +47,7 @@
*/
@Deprecated
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public abstract class ForwardingBlockingDeque<E> extends ForwardingDeque<E>
implements BlockingDeque<E> {
@@ -91,11 +93,13 @@
}
@Override
+ @CheckForNull
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException {
return delegate().pollFirst(timeout, unit);
}
@Override
+ @CheckForNull
public E pollLast(long timeout, TimeUnit unit) throws InterruptedException {
return delegate().pollLast(timeout, unit);
}
@@ -116,6 +120,7 @@
}
@Override
+ @CheckForNull
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
return delegate().poll(timeout, unit);
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingCollection.java b/android/guava/src/com/google/common/collect/ForwardingCollection.java
index 416ff96..ca1edc1 100644
--- a/android/guava/src/com/google/common/collect/ForwardingCollection.java
+++ b/android/guava/src/com/google/common/collect/ForwardingCollection.java
@@ -21,7 +21,8 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Collection;
import java.util.Iterator;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A collection which forwards all its method calls to another collection. Subclasses should
@@ -46,7 +47,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingCollection<E> extends ForwardingObject implements Collection<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingCollection<E extends @Nullable Object> extends ForwardingObject
+ implements Collection<E> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -77,19 +80,19 @@
}
@Override
- public boolean contains(Object object) {
+ public boolean contains(@CheckForNull Object object) {
return delegate().contains(object);
}
@CanIgnoreReturnValue
@Override
- public boolean add(E element) {
+ public boolean add(@ParametricNullness E element) {
return delegate().add(element);
}
@CanIgnoreReturnValue
@Override
- public boolean remove(Object object) {
+ public boolean remove(@CheckForNull Object object) {
return delegate().remove(object);
}
@@ -116,13 +119,14 @@
}
@Override
- public Object[] toArray() {
+ public @Nullable Object[] toArray() {
return delegate().toArray();
}
@CanIgnoreReturnValue
@Override
- public <T> T[] toArray(T[] array) {
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
+ public <T extends @Nullable Object> T[] toArray(T[] array) {
return delegate().toArray(array);
}
@@ -133,7 +137,7 @@
*
* @since 7.0
*/
- protected boolean standardContains(@NullableDecl Object object) {
+ protected boolean standardContains(@CheckForNull Object object) {
return Iterators.contains(iterator(), object);
}
@@ -165,7 +169,7 @@
*
* @since 7.0
*/
- protected boolean standardRemove(@NullableDecl Object object) {
+ protected boolean standardRemove(@CheckForNull Object object) {
Iterator<E> iterator = iterator();
while (iterator.hasNext()) {
if (Objects.equal(iterator.next(), object)) {
@@ -238,8 +242,8 @@
*
* @since 7.0
*/
- protected Object[] standardToArray() {
- Object[] newArray = new Object[size()];
+ protected @Nullable Object[] standardToArray() {
+ @Nullable Object[] newArray = new @Nullable Object[size()];
return toArray(newArray);
}
@@ -250,7 +254,7 @@
*
* @since 7.0
*/
- protected <T> T[] standardToArray(T[] array) {
+ protected <T extends @Nullable Object> T[] standardToArray(T[] array) {
return ObjectArrays.toArrayImpl(this, array);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingConcurrentMap.java b/android/guava/src/com/google/common/collect/ForwardingConcurrentMap.java
index 0910424..b662b07 100644
--- a/android/guava/src/com/google/common/collect/ForwardingConcurrentMap.java
+++ b/android/guava/src/com/google/common/collect/ForwardingConcurrentMap.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.concurrent.ConcurrentMap;
+import javax.annotation.CheckForNull;
/**
* A concurrent map which forwards all its method calls to another concurrent map. Subclasses should
@@ -36,6 +37,7 @@
* @since 2.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public abstract class ForwardingConcurrentMap<K, V> extends ForwardingMap<K, V>
implements ConcurrentMap<K, V> {
@@ -47,18 +49,20 @@
@CanIgnoreReturnValue
@Override
+ @CheckForNull
public V putIfAbsent(K key, V value) {
return delegate().putIfAbsent(key, value);
}
@CanIgnoreReturnValue
@Override
- public boolean remove(Object key, Object value) {
+ public boolean remove(@CheckForNull Object key, @CheckForNull Object value) {
return delegate().remove(key, value);
}
@CanIgnoreReturnValue
@Override
+ @CheckForNull
public V replace(K key, V value) {
return delegate().replace(key, value);
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingDeque.java b/android/guava/src/com/google/common/collect/ForwardingDeque.java
index 87ac71b..571535c 100644
--- a/android/guava/src/com/google/common/collect/ForwardingDeque.java
+++ b/android/guava/src/com/google/common/collect/ForwardingDeque.java
@@ -20,6 +20,8 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Deque;
import java.util.Iterator;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A deque which forwards all its method calls to another deque. Subclasses should override one or
@@ -39,7 +41,9 @@
* @since 12.0
*/
@GwtIncompatible
-public abstract class ForwardingDeque<E> extends ForwardingQueue<E> implements Deque<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingDeque<E extends @Nullable Object> extends ForwardingQueue<E>
+ implements Deque<E> {
/** Constructor for use by subclasses. */
protected ForwardingDeque() {}
@@ -48,12 +52,12 @@
protected abstract Deque<E> delegate();
@Override
- public void addFirst(E e) {
+ public void addFirst(@ParametricNullness E e) {
delegate().addFirst(e);
}
@Override
- public void addLast(E e) {
+ public void addLast(@ParametricNullness E e) {
delegate().addLast(e);
}
@@ -63,81 +67,90 @@
}
@Override
+ @ParametricNullness
public E getFirst() {
return delegate().getFirst();
}
@Override
+ @ParametricNullness
public E getLast() {
return delegate().getLast();
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
- public boolean offerFirst(E e) {
+ public boolean offerFirst(@ParametricNullness E e) {
return delegate().offerFirst(e);
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
- public boolean offerLast(E e) {
+ public boolean offerLast(@ParametricNullness E e) {
return delegate().offerLast(e);
}
@Override
+ @CheckForNull
public E peekFirst() {
return delegate().peekFirst();
}
@Override
+ @CheckForNull
public E peekLast() {
return delegate().peekLast();
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
+ @CheckForNull
public E pollFirst() {
return delegate().pollFirst();
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
+ @CheckForNull
public E pollLast() {
return delegate().pollLast();
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E pop() {
return delegate().pop();
}
@Override
- public void push(E e) {
+ public void push(@ParametricNullness E e) {
delegate().push(e);
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E removeFirst() {
return delegate().removeFirst();
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E removeLast() {
return delegate().removeLast();
}
@CanIgnoreReturnValue
@Override
- public boolean removeFirstOccurrence(Object o) {
+ public boolean removeFirstOccurrence(@CheckForNull Object o) {
return delegate().removeFirstOccurrence(o);
}
@CanIgnoreReturnValue
@Override
- public boolean removeLastOccurrence(Object o) {
+ public boolean removeLastOccurrence(@CheckForNull Object o) {
return delegate().removeLastOccurrence(o);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingImmutableCollection.java b/android/guava/src/com/google/common/collect/ForwardingImmutableCollection.java
index c0b9c5e..043fe58 100644
--- a/android/guava/src/com/google/common/collect/ForwardingImmutableCollection.java
+++ b/android/guava/src/com/google/common/collect/ForwardingImmutableCollection.java
@@ -24,6 +24,7 @@
* @author Hayward Chan
*/
@GwtCompatible(emulated = true)
+@ElementTypesAreNonnullByDefault
class ForwardingImmutableCollection {
private ForwardingImmutableCollection() {}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingImmutableMap.java b/android/guava/src/com/google/common/collect/ForwardingImmutableMap.java
index a367157..22cc9ff 100644
--- a/android/guava/src/com/google/common/collect/ForwardingImmutableMap.java
+++ b/android/guava/src/com/google/common/collect/ForwardingImmutableMap.java
@@ -24,6 +24,7 @@
* @author Chris Povirk
*/
@GwtCompatible(emulated = true)
+@ElementTypesAreNonnullByDefault
abstract class ForwardingImmutableMap<K, V> {
private ForwardingImmutableMap() {}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingIterator.java b/android/guava/src/com/google/common/collect/ForwardingIterator.java
index 5ecd3d2..1f5a8f1 100644
--- a/android/guava/src/com/google/common/collect/ForwardingIterator.java
+++ b/android/guava/src/com/google/common/collect/ForwardingIterator.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* An iterator which forwards all its method calls to another iterator. Subclasses should override
@@ -36,7 +37,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingIterator<T> extends ForwardingObject implements Iterator<T> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingIterator<T extends @Nullable Object> extends ForwardingObject
+ implements Iterator<T> {
/** Constructor for use by subclasses. */
protected ForwardingIterator() {}
@@ -51,6 +54,7 @@
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public T next() {
return delegate().next();
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingList.java b/android/guava/src/com/google/common/collect/ForwardingList.java
index bfd2083..4b4551e 100644
--- a/android/guava/src/com/google/common/collect/ForwardingList.java
+++ b/android/guava/src/com/google/common/collect/ForwardingList.java
@@ -23,7 +23,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A list which forwards all its method calls to another list. Subclasses should override one or
@@ -51,7 +52,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingList<E> extends ForwardingCollection<E> implements List<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingList<E extends @Nullable Object> extends ForwardingCollection<E>
+ implements List<E> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -61,7 +64,7 @@
protected abstract List<E> delegate();
@Override
- public void add(int index, E element) {
+ public void add(int index, @ParametricNullness E element) {
delegate().add(index, element);
}
@@ -72,17 +75,18 @@
}
@Override
+ @ParametricNullness
public E get(int index) {
return delegate().get(index);
}
@Override
- public int indexOf(Object element) {
+ public int indexOf(@CheckForNull Object element) {
return delegate().indexOf(element);
}
@Override
- public int lastIndexOf(Object element) {
+ public int lastIndexOf(@CheckForNull Object element) {
return delegate().lastIndexOf(element);
}
@@ -98,13 +102,15 @@
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E remove(int index) {
return delegate().remove(index);
}
@CanIgnoreReturnValue
@Override
- public E set(int index, E element) {
+ @ParametricNullness
+ public E set(int index, @ParametricNullness E element) {
return delegate().set(index, element);
}
@@ -114,7 +120,7 @@
}
@Override
- public boolean equals(@NullableDecl Object object) {
+ public boolean equals(@CheckForNull Object object) {
return object == this || delegate().equals(object);
}
@@ -130,7 +136,7 @@
*
* @since 7.0
*/
- protected boolean standardAdd(E element) {
+ protected boolean standardAdd(@ParametricNullness E element) {
add(size(), element);
return true;
}
@@ -153,7 +159,7 @@
*
* @since 7.0
*/
- protected int standardIndexOf(@NullableDecl Object element) {
+ protected int standardIndexOf(@CheckForNull Object element) {
return Lists.indexOfImpl(this, element);
}
@@ -164,7 +170,7 @@
*
* @since 7.0
*/
- protected int standardLastIndexOf(@NullableDecl Object element) {
+ protected int standardLastIndexOf(@CheckForNull Object element) {
return Lists.lastIndexOfImpl(this, element);
}
@@ -222,7 +228,7 @@
* @since 7.0
*/
@Beta
- protected boolean standardEquals(@NullableDecl Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
return Lists.equalsImpl(this, object);
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingListIterator.java b/android/guava/src/com/google/common/collect/ForwardingListIterator.java
index bc2a5ad..a2ac32b 100644
--- a/android/guava/src/com/google/common/collect/ForwardingListIterator.java
+++ b/android/guava/src/com/google/common/collect/ForwardingListIterator.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ListIterator;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A list iterator which forwards all its method calls to another list iterator. Subclasses should
@@ -36,8 +37,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingListIterator<E> extends ForwardingIterator<E>
- implements ListIterator<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingListIterator<E extends @Nullable Object>
+ extends ForwardingIterator<E> implements ListIterator<E> {
/** Constructor for use by subclasses. */
protected ForwardingListIterator() {}
@@ -46,7 +48,7 @@
protected abstract ListIterator<E> delegate();
@Override
- public void add(E element) {
+ public void add(@ParametricNullness E element) {
delegate().add(element);
}
@@ -62,6 +64,7 @@
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E previous() {
return delegate().previous();
}
@@ -72,7 +75,7 @@
}
@Override
- public void set(E element) {
+ public void set(@ParametricNullness E element) {
delegate().set(element);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingMap.java b/android/guava/src/com/google/common/collect/ForwardingMap.java
index 4032bf9..315a4fa 100644
--- a/android/guava/src/com/google/common/collect/ForwardingMap.java
+++ b/android/guava/src/com/google/common/collect/ForwardingMap.java
@@ -24,7 +24,8 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A map which forwards all its method calls to another map. Subclasses should override one or more
@@ -55,7 +56,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingMap<K, V> extends ForwardingObject implements Map<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingMap<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingObject implements Map<K, V> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -76,7 +79,8 @@
@CanIgnoreReturnValue
@Override
- public V remove(Object key) {
+ @CheckForNull
+ public V remove(@CheckForNull Object key) {
return delegate().remove(key);
}
@@ -86,23 +90,25 @@
}
@Override
- public boolean containsKey(@NullableDecl Object key) {
+ public boolean containsKey(@CheckForNull Object key) {
return delegate().containsKey(key);
}
@Override
- public boolean containsValue(@NullableDecl Object value) {
+ public boolean containsValue(@CheckForNull Object value) {
return delegate().containsValue(value);
}
@Override
- public V get(@NullableDecl Object key) {
+ @CheckForNull
+ public V get(@CheckForNull Object key) {
return delegate().get(key);
}
@CanIgnoreReturnValue
@Override
- public V put(K key, V value) {
+ @CheckForNull
+ public V put(@ParametricNullness K key, @ParametricNullness V value) {
return delegate().put(key, value);
}
@@ -127,7 +133,7 @@
}
@Override
- public boolean equals(@NullableDecl Object object) {
+ public boolean equals(@CheckForNull Object object) {
return object == this || delegate().equals(object);
}
@@ -158,7 +164,8 @@
* @since 7.0
*/
@Beta
- protected V standardRemove(@NullableDecl Object key) {
+ @CheckForNull
+ protected V standardRemove(@CheckForNull Object key) {
Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
while (entryIterator.hasNext()) {
Entry<K, V> entry = entryIterator.next();
@@ -207,7 +214,7 @@
* @since 7.0
*/
@Beta
- protected boolean standardContainsKey(@NullableDecl Object key) {
+ protected boolean standardContainsKey(@CheckForNull Object key) {
return Maps.containsKeyImpl(this, key);
}
@@ -235,7 +242,7 @@
*
* @since 7.0
*/
- protected boolean standardContainsValue(@NullableDecl Object value) {
+ protected boolean standardContainsValue(@CheckForNull Object value) {
return Maps.containsValueImpl(this, value);
}
@@ -277,7 +284,7 @@
*
* @since 7.0
*/
- protected boolean standardEquals(@NullableDecl Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
return Maps.equalsImpl(this, object);
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingMapEntry.java b/android/guava/src/com/google/common/collect/ForwardingMapEntry.java
index 198b94b..6816ccb 100644
--- a/android/guava/src/com/google/common/collect/ForwardingMapEntry.java
+++ b/android/guava/src/com/google/common/collect/ForwardingMapEntry.java
@@ -21,7 +21,8 @@
import com.google.common.base.Objects;
import java.util.Map;
import java.util.Map.Entry;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A map entry which forwards all its method calls to another map entry. Subclasses should override
@@ -47,7 +48,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingMapEntry<K, V> extends ForwardingObject implements Map.Entry<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingMapEntry<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingObject implements Map.Entry<K, V> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -57,22 +60,25 @@
protected abstract Entry<K, V> delegate();
@Override
+ @ParametricNullness
public K getKey() {
return delegate().getKey();
}
@Override
+ @ParametricNullness
public V getValue() {
return delegate().getValue();
}
@Override
- public V setValue(V value) {
+ @ParametricNullness
+ public V setValue(@ParametricNullness V value) {
return delegate().setValue(value);
}
@Override
- public boolean equals(@NullableDecl Object object) {
+ public boolean equals(@CheckForNull Object object) {
return delegate().equals(object);
}
@@ -88,7 +94,7 @@
*
* @since 7.0
*/
- protected boolean standardEquals(@NullableDecl Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
if (object instanceof Entry) {
Entry<?, ?> that = (Entry<?, ?>) object;
return Objects.equal(this.getKey(), that.getKey())
diff --git a/android/guava/src/com/google/common/collect/ForwardingNavigableMap.java b/android/guava/src/com/google/common/collect/ForwardingNavigableMap.java
index c8d0fd5..e0e1c39 100644
--- a/android/guava/src/com/google/common/collect/ForwardingNavigableMap.java
+++ b/android/guava/src/com/google/common/collect/ForwardingNavigableMap.java
@@ -16,7 +16,6 @@
package com.google.common.collect;
-import static com.google.common.collect.CollectPreconditions.checkRemove;
import static com.google.common.collect.Maps.keyOrNull;
import com.google.common.annotations.Beta;
@@ -26,6 +25,8 @@
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.SortedMap;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A navigable map which forwards all its method calls to another navigable map. Subclasses should
@@ -54,8 +55,9 @@
* @since 12.0
*/
@GwtIncompatible
-public abstract class ForwardingNavigableMap<K, V> extends ForwardingSortedMap<K, V>
- implements NavigableMap<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingNavigableMap<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingSortedMap<K, V> implements NavigableMap<K, V> {
/** Constructor for use by subclasses. */
protected ForwardingNavigableMap() {}
@@ -64,7 +66,8 @@
protected abstract NavigableMap<K, V> delegate();
@Override
- public Entry<K, V> lowerEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> lowerEntry(@ParametricNullness K key) {
return delegate().lowerEntry(key);
}
@@ -73,12 +76,14 @@
* #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override {@code
* lowerEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardLowerEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardLowerEntry(@ParametricNullness K key) {
return headMap(key, false).lastEntry();
}
@Override
- public K lowerKey(K key) {
+ @CheckForNull
+ public K lowerKey(@ParametricNullness K key) {
return delegate().lowerKey(key);
}
@@ -87,12 +92,14 @@
* {@link #lowerEntry}, you may wish to override {@code lowerKey} to forward to this
* implementation.
*/
- protected K standardLowerKey(K key) {
+ @CheckForNull
+ protected K standardLowerKey(@ParametricNullness K key) {
return keyOrNull(lowerEntry(key));
}
@Override
- public Entry<K, V> floorEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> floorEntry(@ParametricNullness K key) {
return delegate().floorEntry(key);
}
@@ -101,12 +108,14 @@
* #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override {@code
* floorEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardFloorEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardFloorEntry(@ParametricNullness K key) {
return headMap(key, true).lastEntry();
}
@Override
- public K floorKey(K key) {
+ @CheckForNull
+ public K floorKey(@ParametricNullness K key) {
return delegate().floorKey(key);
}
@@ -115,12 +124,14 @@
* {@code floorEntry}, you may wish to override {@code floorKey} to forward to this
* implementation.
*/
- protected K standardFloorKey(K key) {
+ @CheckForNull
+ protected K standardFloorKey(@ParametricNullness K key) {
return keyOrNull(floorEntry(key));
}
@Override
- public Entry<K, V> ceilingEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> ceilingEntry(@ParametricNullness K key) {
return delegate().ceilingEntry(key);
}
@@ -129,12 +140,14 @@
* #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override {@code
* ceilingEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardCeilingEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardCeilingEntry(@ParametricNullness K key) {
return tailMap(key, true).firstEntry();
}
@Override
- public K ceilingKey(K key) {
+ @CheckForNull
+ public K ceilingKey(@ParametricNullness K key) {
return delegate().ceilingKey(key);
}
@@ -143,12 +156,14 @@
* {@code ceilingEntry}, you may wish to override {@code ceilingKey} to forward to this
* implementation.
*/
- protected K standardCeilingKey(K key) {
+ @CheckForNull
+ protected K standardCeilingKey(@ParametricNullness K key) {
return keyOrNull(ceilingEntry(key));
}
@Override
- public Entry<K, V> higherEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> higherEntry(@ParametricNullness K key) {
return delegate().higherEntry(key);
}
@@ -157,12 +172,14 @@
* #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override {@code
* higherEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardHigherEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardHigherEntry(@ParametricNullness K key) {
return tailMap(key, false).firstEntry();
}
@Override
- public K higherKey(K key) {
+ @CheckForNull
+ public K higherKey(@ParametricNullness K key) {
return delegate().higherKey(key);
}
@@ -171,11 +188,13 @@
* {@code higherEntry}, you may wish to override {@code higherKey} to forward to this
* implementation.
*/
- protected K standardHigherKey(K key) {
+ @CheckForNull
+ protected K standardHigherKey(@ParametricNullness K key) {
return keyOrNull(higherEntry(key));
}
@Override
+ @CheckForNull
public Entry<K, V> firstEntry() {
return delegate().firstEntry();
}
@@ -185,6 +204,7 @@
* #entrySet}. If you override {@code entrySet}, you may wish to override {@code firstEntry} to
* forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardFirstEntry() {
return Iterables.getFirst(entrySet(), null);
}
@@ -204,6 +224,7 @@
}
@Override
+ @CheckForNull
public Entry<K, V> lastEntry() {
return delegate().lastEntry();
}
@@ -213,6 +234,7 @@
* #entrySet} of {@link #descendingMap}. If you override {@code descendingMap}, you may wish to
* override {@code lastEntry} to forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardLastEntry() {
return Iterables.getFirst(descendingMap().entrySet(), null);
}
@@ -231,6 +253,7 @@
}
@Override
+ @CheckForNull
public Entry<K, V> pollFirstEntry() {
return delegate().pollFirstEntry();
}
@@ -240,11 +263,13 @@
* entrySet}. If you override {@code entrySet}, you may wish to override {@code pollFirstEntry} to
* forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardPollFirstEntry() {
return Iterators.pollNext(entrySet().iterator());
}
@Override
+ @CheckForNull
public Entry<K, V> pollLastEntry() {
return delegate().pollLastEntry();
}
@@ -254,6 +279,7 @@
* entrySet} of {@code descendingMap}. If you override {@code descendingMap}, you may wish to
* override {@code pollFirstEntry} to forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardPollLastEntry() {
return Iterators.pollNext(descendingMap().entrySet().iterator());
}
@@ -287,8 +313,8 @@
@Override
protected Iterator<Entry<K, V>> entryIterator() {
return new Iterator<Entry<K, V>>() {
- private Entry<K, V> toRemove = null;
- private Entry<K, V> nextOrNull = forward().lastEntry();
+ @CheckForNull private Entry<K, V> toRemove = null;
+ @CheckForNull private Entry<K, V> nextOrNull = forward().lastEntry();
@Override
public boolean hasNext() {
@@ -297,7 +323,7 @@
@Override
public java.util.Map.Entry<K, V> next() {
- if (!hasNext()) {
+ if (nextOrNull == null) {
throw new NoSuchElementException();
}
try {
@@ -310,7 +336,9 @@
@Override
public void remove() {
- checkRemove(toRemove != null);
+ if (toRemove == null) {
+ throw new IllegalStateException("no calls to next() since the last call to remove()");
+ }
forward().remove(toRemove.getKey());
toRemove = null;
}
@@ -362,22 +390,27 @@
* wish to override {@code subMap} to forward to this implementation.
*/
@Override
- protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) {
+ protected SortedMap<K, V> standardSubMap(
+ @ParametricNullness K fromKey, @ParametricNullness K toKey) {
return subMap(fromKey, true, toKey, false);
}
@Override
- public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ public NavigableMap<K, V> subMap(
+ @ParametricNullness K fromKey,
+ boolean fromInclusive,
+ @ParametricNullness K toKey,
+ boolean toInclusive) {
return delegate().subMap(fromKey, fromInclusive, toKey, toInclusive);
}
@Override
- public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
+ public NavigableMap<K, V> headMap(@ParametricNullness K toKey, boolean inclusive) {
return delegate().headMap(toKey, inclusive);
}
@Override
- public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
+ public NavigableMap<K, V> tailMap(@ParametricNullness K fromKey, boolean inclusive) {
return delegate().tailMap(fromKey, inclusive);
}
@@ -386,7 +419,7 @@
* boolean)}. If you override {@code headMap(K, boolean)}, you may wish to override {@code
* headMap} to forward to this implementation.
*/
- protected SortedMap<K, V> standardHeadMap(K toKey) {
+ protected SortedMap<K, V> standardHeadMap(@ParametricNullness K toKey) {
return headMap(toKey, false);
}
@@ -395,7 +428,7 @@
* boolean)}. If you override {@code tailMap(K, boolean)}, you may wish to override {@code
* tailMap} to forward to this implementation.
*/
- protected SortedMap<K, V> standardTailMap(K fromKey) {
+ protected SortedMap<K, V> standardTailMap(@ParametricNullness K fromKey) {
return tailMap(fromKey, true);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingNavigableSet.java b/android/guava/src/com/google/common/collect/ForwardingNavigableSet.java
index 827698e..6822aa8 100644
--- a/android/guava/src/com/google/common/collect/ForwardingNavigableSet.java
+++ b/android/guava/src/com/google/common/collect/ForwardingNavigableSet.java
@@ -21,6 +21,8 @@
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A navigable set which forwards all its method calls to another navigable set. Subclasses should
@@ -49,8 +51,9 @@
* @since 12.0
*/
@GwtIncompatible
-public abstract class ForwardingNavigableSet<E> extends ForwardingSortedSet<E>
- implements NavigableSet<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingNavigableSet<E extends @Nullable Object>
+ extends ForwardingSortedSet<E> implements NavigableSet<E> {
/** Constructor for use by subclasses. */
protected ForwardingNavigableSet() {}
@@ -59,7 +62,8 @@
protected abstract NavigableSet<E> delegate();
@Override
- public E lower(E e) {
+ @CheckForNull
+ public E lower(@ParametricNullness E e) {
return delegate().lower(e);
}
@@ -68,12 +72,14 @@
* {@link #headSet(Object, boolean)}. If you override {@link #headSet(Object, boolean)}, you may
* wish to override {@link #lower} to forward to this implementation.
*/
- protected E standardLower(E e) {
+ @CheckForNull
+ protected E standardLower(@ParametricNullness E e) {
return Iterators.getNext(headSet(e, false).descendingIterator(), null);
}
@Override
- public E floor(E e) {
+ @CheckForNull
+ public E floor(@ParametricNullness E e) {
return delegate().floor(e);
}
@@ -82,12 +88,14 @@
* {@link #headSet(Object, boolean)}. If you override {@link #headSet(Object, boolean)}, you may
* wish to override {@link #floor} to forward to this implementation.
*/
- protected E standardFloor(E e) {
+ @CheckForNull
+ protected E standardFloor(@ParametricNullness E e) {
return Iterators.getNext(headSet(e, true).descendingIterator(), null);
}
@Override
- public E ceiling(E e) {
+ @CheckForNull
+ public E ceiling(@ParametricNullness E e) {
return delegate().ceiling(e);
}
@@ -96,12 +104,14 @@
* #tailSet(Object, boolean)}. If you override {@link #tailSet(Object, boolean)}, you may wish to
* override {@link #ceiling} to forward to this implementation.
*/
- protected E standardCeiling(E e) {
+ @CheckForNull
+ protected E standardCeiling(@ParametricNullness E e) {
return Iterators.getNext(tailSet(e, true).iterator(), null);
}
@Override
- public E higher(E e) {
+ @CheckForNull
+ public E higher(@ParametricNullness E e) {
return delegate().higher(e);
}
@@ -110,11 +120,13 @@
* #tailSet(Object, boolean)}. If you override {@link #tailSet(Object, boolean)}, you may wish to
* override {@link #higher} to forward to this implementation.
*/
- protected E standardHigher(E e) {
+ @CheckForNull
+ protected E standardHigher(@ParametricNullness E e) {
return Iterators.getNext(tailSet(e, false).iterator(), null);
}
@Override
+ @CheckForNull
public E pollFirst() {
return delegate().pollFirst();
}
@@ -124,11 +136,13 @@
* override {@link #iterator} you may wish to override {@link #pollFirst} to forward to this
* implementation.
*/
+ @CheckForNull
protected E standardPollFirst() {
return Iterators.pollNext(iterator());
}
@Override
+ @CheckForNull
public E pollLast() {
return delegate().pollLast();
}
@@ -138,14 +152,17 @@
* If you override {@link #descendingIterator} you may wish to override {@link #pollLast} to
* forward to this implementation.
*/
+ @CheckForNull
protected E standardPollLast() {
return Iterators.pollNext(descendingIterator());
}
+ @ParametricNullness
protected E standardFirst() {
return iterator().next();
}
+ @ParametricNullness
protected E standardLast() {
return descendingIterator().next();
}
@@ -179,7 +196,10 @@
@Override
public NavigableSet<E> subSet(
- E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ @ParametricNullness E fromElement,
+ boolean fromInclusive,
+ @ParametricNullness E toElement,
+ boolean toInclusive) {
return delegate().subSet(fromElement, fromInclusive, toElement, toInclusive);
}
@@ -190,7 +210,10 @@
*/
@Beta
protected NavigableSet<E> standardSubSet(
- E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ @ParametricNullness E fromElement,
+ boolean fromInclusive,
+ @ParametricNullness E toElement,
+ boolean toInclusive) {
return tailSet(fromElement, fromInclusive).headSet(toElement, toInclusive);
}
@@ -201,12 +224,13 @@
* implementation.
*/
@Override
- protected SortedSet<E> standardSubSet(E fromElement, E toElement) {
+ protected SortedSet<E> standardSubSet(
+ @ParametricNullness E fromElement, @ParametricNullness E toElement) {
return subSet(fromElement, true, toElement, false);
}
@Override
- public NavigableSet<E> headSet(E toElement, boolean inclusive) {
+ public NavigableSet<E> headSet(@ParametricNullness E toElement, boolean inclusive) {
return delegate().headSet(toElement, inclusive);
}
@@ -215,12 +239,12 @@
* boolean)} method. If you override {@link #headSet(Object, boolean)}, you may wish to override
* {@link #headSet(Object)} to forward to this implementation.
*/
- protected SortedSet<E> standardHeadSet(E toElement) {
+ protected SortedSet<E> standardHeadSet(@ParametricNullness E toElement) {
return headSet(toElement, false);
}
@Override
- public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
+ public NavigableSet<E> tailSet(@ParametricNullness E fromElement, boolean inclusive) {
return delegate().tailSet(fromElement, inclusive);
}
@@ -229,7 +253,7 @@
* boolean)} method. If you override {@link #tailSet(Object, boolean)}, you may wish to override
* {@link #tailSet(Object)} to forward to this implementation.
*/
- protected SortedSet<E> standardTailSet(E fromElement) {
+ protected SortedSet<E> standardTailSet(@ParametricNullness E fromElement) {
return tailSet(fromElement, true);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingObject.java b/android/guava/src/com/google/common/collect/ForwardingObject.java
index 712b14f..64af908 100644
--- a/android/guava/src/com/google/common/collect/ForwardingObject.java
+++ b/android/guava/src/com/google/common/collect/ForwardingObject.java
@@ -44,6 +44,7 @@
* @since 2.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public abstract class ForwardingObject {
/** Constructor for use by subclasses. */
diff --git a/android/guava/src/com/google/common/collect/ForwardingQueue.java b/android/guava/src/com/google/common/collect/ForwardingQueue.java
index f77e560..43c2eaa 100644
--- a/android/guava/src/com/google/common/collect/ForwardingQueue.java
+++ b/android/guava/src/com/google/common/collect/ForwardingQueue.java
@@ -20,6 +20,8 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.NoSuchElementException;
import java.util.Queue;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A queue which forwards all its method calls to another queue. Subclasses should override one or
@@ -44,7 +46,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingQueue<E> extends ForwardingCollection<E> implements Queue<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingQueue<E extends @Nullable Object> extends ForwardingCollection<E>
+ implements Queue<E> {
/** Constructor for use by subclasses. */
protected ForwardingQueue() {}
@@ -54,28 +58,32 @@
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
- public boolean offer(E o) {
+ public boolean offer(@ParametricNullness E o) {
return delegate().offer(o);
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
+ @CheckForNull
public E poll() {
return delegate().poll();
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E remove() {
return delegate().remove();
}
@Override
+ @CheckForNull
public E peek() {
return delegate().peek();
}
@Override
+ @ParametricNullness
public E element() {
return delegate().element();
}
@@ -86,7 +94,7 @@
*
* @since 7.0
*/
- protected boolean standardOffer(E e) {
+ protected boolean standardOffer(@ParametricNullness E e) {
try {
return add(e);
} catch (IllegalStateException caught) {
@@ -100,6 +108,7 @@
*
* @since 7.0
*/
+ @CheckForNull
protected E standardPeek() {
try {
return element();
@@ -114,6 +123,7 @@
*
* @since 7.0
*/
+ @CheckForNull
protected E standardPoll() {
try {
return remove();
diff --git a/android/guava/src/com/google/common/collect/ForwardingSet.java b/android/guava/src/com/google/common/collect/ForwardingSet.java
index 73b1413..bc27272 100644
--- a/android/guava/src/com/google/common/collect/ForwardingSet.java
+++ b/android/guava/src/com/google/common/collect/ForwardingSet.java
@@ -21,7 +21,8 @@
import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Set;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A set which forwards all its method calls to another set. Subclasses should override one or more
@@ -46,7 +47,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingSet<E> extends ForwardingCollection<E> implements Set<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingSet<E extends @Nullable Object> extends ForwardingCollection<E>
+ implements Set<E> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -56,7 +59,7 @@
protected abstract Set<E> delegate();
@Override
- public boolean equals(@NullableDecl Object object) {
+ public boolean equals(@CheckForNull Object object) {
return object == this || delegate().equals(object);
}
@@ -84,7 +87,7 @@
*
* @since 7.0
*/
- protected boolean standardEquals(@NullableDecl Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
return Sets.equalsImpl(this, object);
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingSortedMap.java b/android/guava/src/com/google/common/collect/ForwardingSortedMap.java
index 4866fb9..e0882ab 100644
--- a/android/guava/src/com/google/common/collect/ForwardingSortedMap.java
+++ b/android/guava/src/com/google/common/collect/ForwardingSortedMap.java
@@ -23,7 +23,8 @@
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.SortedMap;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A sorted map which forwards all its method calls to another sorted map. Subclasses should
@@ -51,8 +52,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingSortedMap<K, V> extends ForwardingMap<K, V>
- implements SortedMap<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingSortedMap<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingMap<K, V> implements SortedMap<K, V> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -62,32 +64,35 @@
protected abstract SortedMap<K, V> delegate();
@Override
+ @CheckForNull
public Comparator<? super K> comparator() {
return delegate().comparator();
}
@Override
+ @ParametricNullness
public K firstKey() {
return delegate().firstKey();
}
@Override
- public SortedMap<K, V> headMap(K toKey) {
+ public SortedMap<K, V> headMap(@ParametricNullness K toKey) {
return delegate().headMap(toKey);
}
@Override
+ @ParametricNullness
public K lastKey() {
return delegate().lastKey();
}
@Override
- public SortedMap<K, V> subMap(K fromKey, K toKey) {
+ public SortedMap<K, V> subMap(@ParametricNullness K fromKey, @ParametricNullness K toKey) {
return delegate().subMap(fromKey, toKey);
}
@Override
- public SortedMap<K, V> tailMap(K fromKey) {
+ public SortedMap<K, V> tailMap(@ParametricNullness K fromKey) {
return delegate().tailMap(fromKey);
}
@@ -106,14 +111,14 @@
}
}
- // unsafe, but worst case is a CCE is thrown, which callers will be expecting
- @SuppressWarnings("unchecked")
- private int unsafeCompare(Object k1, Object k2) {
- Comparator<? super K> comparator = comparator();
+ // unsafe, but worst case is a CCE or NPE is thrown, which callers will be expecting
+ @SuppressWarnings({"unchecked", "nullness"})
+ static int unsafeCompare(
+ @CheckForNull Comparator<?> comparator, @CheckForNull Object o1, @CheckForNull Object o2) {
if (comparator == null) {
- return ((Comparable<Object>) k1).compareTo(k2);
+ return ((Comparable<@Nullable Object>) o1).compareTo(o2);
} else {
- return ((Comparator<Object>) comparator).compare(k1, k2);
+ return ((Comparator<@Nullable Object>) comparator).compare(o1, o2);
}
}
@@ -126,13 +131,13 @@
*/
@Override
@Beta
- protected boolean standardContainsKey(@NullableDecl Object key) {
+ protected boolean standardContainsKey(@CheckForNull Object key) {
try {
- // any CCE will be caught
- @SuppressWarnings("unchecked")
- SortedMap<Object, V> self = (SortedMap<Object, V>) this;
+ // any CCE or NPE will be caught
+ @SuppressWarnings({"unchecked", "nullness"})
+ SortedMap<@Nullable Object, V> self = (SortedMap<@Nullable Object, V>) this;
Object ceilingKey = self.tailMap(key).firstKey();
- return unsafeCompare(ceilingKey, key) == 0;
+ return unsafeCompare(comparator(), ceilingKey, key) == 0;
} catch (ClassCastException | NoSuchElementException | NullPointerException e) {
return false;
}
@@ -147,7 +152,7 @@
*/
@Beta
protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) {
- checkArgument(unsafeCompare(fromKey, toKey) <= 0, "fromKey must be <= toKey");
+ checkArgument(unsafeCompare(comparator(), fromKey, toKey) <= 0, "fromKey must be <= toKey");
return tailMap(fromKey).headMap(toKey);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingSortedSet.java b/android/guava/src/com/google/common/collect/ForwardingSortedSet.java
index 9879944..32625af 100644
--- a/android/guava/src/com/google/common/collect/ForwardingSortedSet.java
+++ b/android/guava/src/com/google/common/collect/ForwardingSortedSet.java
@@ -16,13 +16,16 @@
package com.google.common.collect;
+import static com.google.common.collect.ForwardingSortedMap.unsafeCompare;
+
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.SortedSet;
-import org.checkerframework.checker.nullness.compatqual.NullableDecl;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A sorted set which forwards all its method calls to another sorted set. Subclasses should
@@ -52,7 +55,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingSortedSet<E> extends ForwardingSet<E> implements SortedSet<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingSortedSet<E extends @Nullable Object> extends ForwardingSet<E>
+ implements SortedSet<E> {
/** Constructor for use by subclasses. */
protected ForwardingSortedSet() {}
@@ -61,44 +66,38 @@
protected abstract SortedSet<E> delegate();
@Override
+ @CheckForNull
public Comparator<? super E> comparator() {
return delegate().comparator();
}
@Override
+ @ParametricNullness
public E first() {
return delegate().first();
}
@Override
- public SortedSet<E> headSet(E toElement) {
+ public SortedSet<E> headSet(@ParametricNullness E toElement) {
return delegate().headSet(toElement);
}
@Override
+ @ParametricNullness
public E last() {
return delegate().last();
}
@Override
- public SortedSet<E> subSet(E fromElement, E toElement) {
+ public SortedSet<E> subSet(@ParametricNullness E fromElement, @ParametricNullness E toElement) {
return delegate().subSet(fromElement, toElement);
}
@Override
- public SortedSet<E> tailSet(E fromElement) {
+ public SortedSet<E> tailSet(@ParametricNullness E fromElement) {
return delegate().tailSet(fromElement);
}
- // unsafe, but worst case is a CCE is thrown, which callers will be expecting
- @SuppressWarnings("unchecked")
- private int unsafeCompare(@NullableDecl Object o1, @NullableDecl Object o2) {
- Comparator<? super E> comparator = comparator();
- return (comparator == null)
- ? ((Comparable<Object>) o1).compareTo(o2)
- : ((Comparator<Object>) comparator).compare(o1, o2);
- }
-
/**
* A sensible definition of {@link #contains} in terms of the {@code first()} method of {@link
* #tailSet}. If you override {@link #tailSet}, you may wish to override {@link #contains} to
@@ -108,13 +107,13 @@
*/
@Override
@Beta
- protected boolean standardContains(@NullableDecl Object object) {
+ protected boolean standardContains(@CheckForNull Object object) {
try {
- // any ClassCastExceptions are caught
- @SuppressWarnings("unchecked")
- SortedSet<Object> self = (SortedSet<Object>) this;
+ // any ClassCastExceptions and NullPointerExceptions are caught
+ @SuppressWarnings({"unchecked", "nullness"})
+ SortedSet<@Nullable Object> self = (SortedSet<@Nullable Object>) this;
Object ceiling = self.tailSet(object).first();
- return unsafeCompare(ceiling, object) == 0;
+ return unsafeCompare(comparator(), ceiling, object) == 0;
} catch (ClassCastException | NoSuchElementException | NullPointerException e) {
return false;
}
@@ -129,15 +128,15 @@
*/
@Override
@Beta
- protected boolean standardRemove(@NullableDecl Object object) {
+ protected boolean standardRemove(@CheckForNull Object object) {
try {
- // any ClassCastExceptions are caught
- @SuppressWarnings("unchecked")
- SortedSet<Object> self = (SortedSet<Object>) this;
- Iterator<Object> iterator = self.tailSet(object).iterator();
+ // any ClassCastExceptions and NullPointerExceptions are caught
+ @SuppressWarnings({"unchecked", "nullness"})
+ SortedSet<@Nullable Object> self = (SortedSet<@Nullable Object>) this;
+ Iterator<?> iterator = self.tailSet(object).iterator();
if (iterator.hasNext()) {
Object ceiling = iterator.next();
- if (unsafeCompare(ceiling, object) == 0) {
+ if (unsafeCompare(comparator(), ceiling, object) == 0) {
iterator.remove();
return true;
}
@@ -156,7 +155,8 @@
* @since 7.0
*/
@Beta
- protected SortedSet<E> standardSubSet(E fromElement, E toElement) {
+ protected SortedSet<E> standardSubSet(
+ @ParametricNullness E fromElement, @ParametricNullness E toElement) {
return tailSet(fromElement).headSet(toElement);
}
}
diff --git a/android/guava/src/com/google/common/collect/ForwardingTable.java b/android/guava/src/com/google/common/collect/ForwardingTable.java
index ea80383..4fcb858 100644
--- a/android/guava/src/com/google/common/collect/ForwardingTable.java
+++ b/android/guava/src/com/google/common/collect/ForwardingTable.java
@@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A table which forwards all its method calls to another table. Subclasses should override one or
@@ -32,7 +33,10 @@
* @since 7.0
*/
@GwtCompatible
-public abstract class ForwardingTable<R, C, V> extends ForwardingObject implements Table<R, C, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingTable<
+ R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingObject implements Table<R, C, V> {
/** Constructor for use by subclasses. */
protected ForwardingTable() {}
@@ -50,7 +54,7 @@
}
@Override
- public Map<R, V> column(C columnKey) {
+ public Map<R, V> column(@ParametricNullness C columnKey) {
return delegate().column(columnKey);
}
@@ -65,28 +69,28 @@
}
@Override
- public boolean contains(Object rowKey, Object columnKey) {
+ public boolean contains(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
return delegate().contains(rowKey, columnKey);
}
@Override
- public boolean containsColumn(Object columnKey) {
+ public boolean containsColumn(@CheckForNull Object columnKey) {
return delegate().containsColumn(columnKey);
}
@Override
- public boolean containsRow(Object rowKey) {
+ public boolean containsRow(@CheckForNull Object rowKey) {
return delegate().containsRow(rowKey);
}
@Override
- public boolean containsValue(Object value) {
+ public boolean containsValue(@CheckForNull Object value) {
return delegate().containsValue(value);
}
@Override
@CheckForNull
- public V get(Object rowKey, Object columnKey) {
+ public V get(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
return delegate().get(rowKey, columnKey);
}
@@ -98,7 +102,8 @@
@CanIgnoreReturnValue
@Override
@CheckForNull
- public V put(R rowKey, C columnKey, V value) {
+ public V put(
+ @ParametricNullness R rowKey, @ParametricNullness C columnKey, @ParametricNullness V value) {
return delegate().put(rowKey, columnKey, value);
}
@@ -110,12 +115,12 @@
@CanIgnoreReturnValue
@Override
@CheckForNull
- public V remove(Object rowKey, Object columnKey) {
+ public V remove(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
return delegate().remove(rowKey, columnKey);
}
@Override
- public Map<C, V> row(R rowKey) {
+ public Map<C, V> row(@ParametricNullness R rowKey) {
return delegate().row(rowKey);
}
@@ -140,7 +145,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@CheckForNull Object obj) {
return (obj == this) || delegate().equals(obj);
}
diff --git a/android/guava/src/com/google/common/collect/ImmutableCollection.java b/android/guava/src/com/google/common/collect/ImmutableCollection.java
index 183418c..b5ac2a1 100644
--- a/android/guava/src/com/google/common/collect/ImmutableCollection.java
+++ b/android/guava/src/com/google/common/collect/ImmutableCollection.java
@@ -186,8 +186,7 @@
/*
* This suppression is here for two reasons:
*
- * 1. Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
+ * 1. b/192354773 in our checker affects toArray declarations.
*
* 2. `other[size] = null` is unsound. We could "fix" this by requiring callers to pass in an
* array with a nullable element type. But probably they usually want an array with a non-nullable
diff --git a/guava/src/com/google/common/collect/ConcurrentHashMultiset.java b/guava/src/com/google/common/collect/ConcurrentHashMultiset.java
index 2ed6ae5..4c61b03 100644
--- a/guava/src/com/google/common/collect/ConcurrentHashMultiset.java
+++ b/guava/src/com/google/common/collect/ConcurrentHashMultiset.java
@@ -170,11 +170,7 @@
}
@Override
- /*
- * Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
- */
- @SuppressWarnings("nullness")
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return snapshot().toArray(array);
}
@@ -579,11 +575,7 @@
}
@Override
- /*
- * Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
- */
- @SuppressWarnings("nullness")
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return snapshot().toArray(array);
}
diff --git a/guava/src/com/google/common/collect/DescendingMultiset.java b/guava/src/com/google/common/collect/DescendingMultiset.java
index a096384..ec5a1d0 100644
--- a/guava/src/com/google/common/collect/DescendingMultiset.java
+++ b/guava/src/com/google/common/collect/DescendingMultiset.java
@@ -156,11 +156,7 @@
}
@Override
- /*
- * Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
- */
- @SuppressWarnings("nullness")
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
public <T extends @Nullable Object> T[] toArray(T[] array) {
return standardToArray(array);
}
diff --git a/guava/src/com/google/common/collect/EvictingQueue.java b/guava/src/com/google/common/collect/EvictingQueue.java
index 37a65f3..e5c73b1 100644
--- a/guava/src/com/google/common/collect/EvictingQueue.java
+++ b/guava/src/com/google/common/collect/EvictingQueue.java
@@ -136,6 +136,21 @@
return delegate().remove(checkNotNull(object));
}
+ @Override
+ public Object[] toArray() {
+ /*
+ * If we could, we'd declare the no-arg `Collection.toArray()` to return "Object[] but elements
+ * have the same nullness as E." Since we can't, we declare it to return nullable elements, and
+ * we can override it in our non-null-guaranteeing subtypes to present a better signature to
+ * their users.
+ *
+ * However, the checker *we* use has this special knowledge about `Collection.toArray()` anyway,
+ * so in our implementation code, we can rely on that. That's why the expression below
+ * type-checks.
+ */
+ return super.toArray();
+ }
+
// TODO(kak): Do we want to checkNotNull each element in containsAll, removeAll, and retainAll?
private static final long serialVersionUID = 0L;
diff --git a/guava/src/com/google/common/collect/ForwardingBlockingDeque.java b/guava/src/com/google/common/collect/ForwardingBlockingDeque.java
index 7d3895d..49d4bcf 100644
--- a/guava/src/com/google/common/collect/ForwardingBlockingDeque.java
+++ b/guava/src/com/google/common/collect/ForwardingBlockingDeque.java
@@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;
+import javax.annotation.CheckForNull;
/**
* A {@link BlockingDeque} which forwards all its method calls to another {@code BlockingDeque}.
@@ -46,6 +47,7 @@
*/
@Deprecated
@GwtIncompatible
+@ElementTypesAreNonnullByDefault
public abstract class ForwardingBlockingDeque<E> extends ForwardingDeque<E>
implements BlockingDeque<E> {
@@ -91,11 +93,13 @@
}
@Override
+ @CheckForNull
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException {
return delegate().pollFirst(timeout, unit);
}
@Override
+ @CheckForNull
public E pollLast(long timeout, TimeUnit unit) throws InterruptedException {
return delegate().pollLast(timeout, unit);
}
@@ -116,6 +120,7 @@
}
@Override
+ @CheckForNull
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
return delegate().poll(timeout, unit);
}
diff --git a/guava/src/com/google/common/collect/ForwardingCollection.java b/guava/src/com/google/common/collect/ForwardingCollection.java
index 66219c8..ca1edc1 100644
--- a/guava/src/com/google/common/collect/ForwardingCollection.java
+++ b/guava/src/com/google/common/collect/ForwardingCollection.java
@@ -21,6 +21,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Collection;
import java.util.Iterator;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -46,7 +47,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingCollection<E> extends ForwardingObject implements Collection<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingCollection<E extends @Nullable Object> extends ForwardingObject
+ implements Collection<E> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -77,19 +80,19 @@
}
@Override
- public boolean contains(Object object) {
+ public boolean contains(@CheckForNull Object object) {
return delegate().contains(object);
}
@CanIgnoreReturnValue
@Override
- public boolean add(E element) {
+ public boolean add(@ParametricNullness E element) {
return delegate().add(element);
}
@CanIgnoreReturnValue
@Override
- public boolean remove(Object object) {
+ public boolean remove(@CheckForNull Object object) {
return delegate().remove(object);
}
@@ -116,13 +119,14 @@
}
@Override
- public Object[] toArray() {
+ public @Nullable Object[] toArray() {
return delegate().toArray();
}
@CanIgnoreReturnValue
@Override
- public <T> T[] toArray(T[] array) {
+ @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
+ public <T extends @Nullable Object> T[] toArray(T[] array) {
return delegate().toArray(array);
}
@@ -133,7 +137,7 @@
*
* @since 7.0
*/
- protected boolean standardContains(@Nullable Object object) {
+ protected boolean standardContains(@CheckForNull Object object) {
return Iterators.contains(iterator(), object);
}
@@ -165,7 +169,7 @@
*
* @since 7.0
*/
- protected boolean standardRemove(@Nullable Object object) {
+ protected boolean standardRemove(@CheckForNull Object object) {
Iterator<E> iterator = iterator();
while (iterator.hasNext()) {
if (Objects.equal(iterator.next(), object)) {
@@ -238,8 +242,8 @@
*
* @since 7.0
*/
- protected Object[] standardToArray() {
- Object[] newArray = new Object[size()];
+ protected @Nullable Object[] standardToArray() {
+ @Nullable Object[] newArray = new @Nullable Object[size()];
return toArray(newArray);
}
@@ -250,7 +254,7 @@
*
* @since 7.0
*/
- protected <T> T[] standardToArray(T[] array) {
+ protected <T extends @Nullable Object> T[] standardToArray(T[] array) {
return ObjectArrays.toArrayImpl(this, array);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingConcurrentMap.java b/guava/src/com/google/common/collect/ForwardingConcurrentMap.java
index 0910424..b662b07 100644
--- a/guava/src/com/google/common/collect/ForwardingConcurrentMap.java
+++ b/guava/src/com/google/common/collect/ForwardingConcurrentMap.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.concurrent.ConcurrentMap;
+import javax.annotation.CheckForNull;
/**
* A concurrent map which forwards all its method calls to another concurrent map. Subclasses should
@@ -36,6 +37,7 @@
* @since 2.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public abstract class ForwardingConcurrentMap<K, V> extends ForwardingMap<K, V>
implements ConcurrentMap<K, V> {
@@ -47,18 +49,20 @@
@CanIgnoreReturnValue
@Override
+ @CheckForNull
public V putIfAbsent(K key, V value) {
return delegate().putIfAbsent(key, value);
}
@CanIgnoreReturnValue
@Override
- public boolean remove(Object key, Object value) {
+ public boolean remove(@CheckForNull Object key, @CheckForNull Object value) {
return delegate().remove(key, value);
}
@CanIgnoreReturnValue
@Override
+ @CheckForNull
public V replace(K key, V value) {
return delegate().replace(key, value);
}
diff --git a/guava/src/com/google/common/collect/ForwardingDeque.java b/guava/src/com/google/common/collect/ForwardingDeque.java
index 87ac71b..571535c 100644
--- a/guava/src/com/google/common/collect/ForwardingDeque.java
+++ b/guava/src/com/google/common/collect/ForwardingDeque.java
@@ -20,6 +20,8 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Deque;
import java.util.Iterator;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A deque which forwards all its method calls to another deque. Subclasses should override one or
@@ -39,7 +41,9 @@
* @since 12.0
*/
@GwtIncompatible
-public abstract class ForwardingDeque<E> extends ForwardingQueue<E> implements Deque<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingDeque<E extends @Nullable Object> extends ForwardingQueue<E>
+ implements Deque<E> {
/** Constructor for use by subclasses. */
protected ForwardingDeque() {}
@@ -48,12 +52,12 @@
protected abstract Deque<E> delegate();
@Override
- public void addFirst(E e) {
+ public void addFirst(@ParametricNullness E e) {
delegate().addFirst(e);
}
@Override
- public void addLast(E e) {
+ public void addLast(@ParametricNullness E e) {
delegate().addLast(e);
}
@@ -63,81 +67,90 @@
}
@Override
+ @ParametricNullness
public E getFirst() {
return delegate().getFirst();
}
@Override
+ @ParametricNullness
public E getLast() {
return delegate().getLast();
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
- public boolean offerFirst(E e) {
+ public boolean offerFirst(@ParametricNullness E e) {
return delegate().offerFirst(e);
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
- public boolean offerLast(E e) {
+ public boolean offerLast(@ParametricNullness E e) {
return delegate().offerLast(e);
}
@Override
+ @CheckForNull
public E peekFirst() {
return delegate().peekFirst();
}
@Override
+ @CheckForNull
public E peekLast() {
return delegate().peekLast();
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
+ @CheckForNull
public E pollFirst() {
return delegate().pollFirst();
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
+ @CheckForNull
public E pollLast() {
return delegate().pollLast();
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E pop() {
return delegate().pop();
}
@Override
- public void push(E e) {
+ public void push(@ParametricNullness E e) {
delegate().push(e);
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E removeFirst() {
return delegate().removeFirst();
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E removeLast() {
return delegate().removeLast();
}
@CanIgnoreReturnValue
@Override
- public boolean removeFirstOccurrence(Object o) {
+ public boolean removeFirstOccurrence(@CheckForNull Object o) {
return delegate().removeFirstOccurrence(o);
}
@CanIgnoreReturnValue
@Override
- public boolean removeLastOccurrence(Object o) {
+ public boolean removeLastOccurrence(@CheckForNull Object o) {
return delegate().removeLastOccurrence(o);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingImmutableCollection.java b/guava/src/com/google/common/collect/ForwardingImmutableCollection.java
index c0b9c5e..043fe58 100644
--- a/guava/src/com/google/common/collect/ForwardingImmutableCollection.java
+++ b/guava/src/com/google/common/collect/ForwardingImmutableCollection.java
@@ -24,6 +24,7 @@
* @author Hayward Chan
*/
@GwtCompatible(emulated = true)
+@ElementTypesAreNonnullByDefault
class ForwardingImmutableCollection {
private ForwardingImmutableCollection() {}
}
diff --git a/guava/src/com/google/common/collect/ForwardingImmutableMap.java b/guava/src/com/google/common/collect/ForwardingImmutableMap.java
index a367157..22cc9ff 100644
--- a/guava/src/com/google/common/collect/ForwardingImmutableMap.java
+++ b/guava/src/com/google/common/collect/ForwardingImmutableMap.java
@@ -24,6 +24,7 @@
* @author Chris Povirk
*/
@GwtCompatible(emulated = true)
+@ElementTypesAreNonnullByDefault
abstract class ForwardingImmutableMap<K, V> {
private ForwardingImmutableMap() {}
}
diff --git a/guava/src/com/google/common/collect/ForwardingIterator.java b/guava/src/com/google/common/collect/ForwardingIterator.java
index 5ecd3d2..1f5a8f1 100644
--- a/guava/src/com/google/common/collect/ForwardingIterator.java
+++ b/guava/src/com/google/common/collect/ForwardingIterator.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* An iterator which forwards all its method calls to another iterator. Subclasses should override
@@ -36,7 +37,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingIterator<T> extends ForwardingObject implements Iterator<T> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingIterator<T extends @Nullable Object> extends ForwardingObject
+ implements Iterator<T> {
/** Constructor for use by subclasses. */
protected ForwardingIterator() {}
@@ -51,6 +54,7 @@
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public T next() {
return delegate().next();
}
diff --git a/guava/src/com/google/common/collect/ForwardingList.java b/guava/src/com/google/common/collect/ForwardingList.java
index d2ba154..4b4551e 100644
--- a/guava/src/com/google/common/collect/ForwardingList.java
+++ b/guava/src/com/google/common/collect/ForwardingList.java
@@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -51,7 +52,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingList<E> extends ForwardingCollection<E> implements List<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingList<E extends @Nullable Object> extends ForwardingCollection<E>
+ implements List<E> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -61,7 +64,7 @@
protected abstract List<E> delegate();
@Override
- public void add(int index, E element) {
+ public void add(int index, @ParametricNullness E element) {
delegate().add(index, element);
}
@@ -72,17 +75,18 @@
}
@Override
+ @ParametricNullness
public E get(int index) {
return delegate().get(index);
}
@Override
- public int indexOf(Object element) {
+ public int indexOf(@CheckForNull Object element) {
return delegate().indexOf(element);
}
@Override
- public int lastIndexOf(Object element) {
+ public int lastIndexOf(@CheckForNull Object element) {
return delegate().lastIndexOf(element);
}
@@ -98,13 +102,15 @@
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E remove(int index) {
return delegate().remove(index);
}
@CanIgnoreReturnValue
@Override
- public E set(int index, E element) {
+ @ParametricNullness
+ public E set(int index, @ParametricNullness E element) {
return delegate().set(index, element);
}
@@ -114,7 +120,7 @@
}
@Override
- public boolean equals(@Nullable Object object) {
+ public boolean equals(@CheckForNull Object object) {
return object == this || delegate().equals(object);
}
@@ -130,7 +136,7 @@
*
* @since 7.0
*/
- protected boolean standardAdd(E element) {
+ protected boolean standardAdd(@ParametricNullness E element) {
add(size(), element);
return true;
}
@@ -153,7 +159,7 @@
*
* @since 7.0
*/
- protected int standardIndexOf(@Nullable Object element) {
+ protected int standardIndexOf(@CheckForNull Object element) {
return Lists.indexOfImpl(this, element);
}
@@ -164,7 +170,7 @@
*
* @since 7.0
*/
- protected int standardLastIndexOf(@Nullable Object element) {
+ protected int standardLastIndexOf(@CheckForNull Object element) {
return Lists.lastIndexOfImpl(this, element);
}
@@ -222,7 +228,7 @@
* @since 7.0
*/
@Beta
- protected boolean standardEquals(@Nullable Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
return Lists.equalsImpl(this, object);
}
diff --git a/guava/src/com/google/common/collect/ForwardingListIterator.java b/guava/src/com/google/common/collect/ForwardingListIterator.java
index bc2a5ad..a2ac32b 100644
--- a/guava/src/com/google/common/collect/ForwardingListIterator.java
+++ b/guava/src/com/google/common/collect/ForwardingListIterator.java
@@ -19,6 +19,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ListIterator;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A list iterator which forwards all its method calls to another list iterator. Subclasses should
@@ -36,8 +37,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingListIterator<E> extends ForwardingIterator<E>
- implements ListIterator<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingListIterator<E extends @Nullable Object>
+ extends ForwardingIterator<E> implements ListIterator<E> {
/** Constructor for use by subclasses. */
protected ForwardingListIterator() {}
@@ -46,7 +48,7 @@
protected abstract ListIterator<E> delegate();
@Override
- public void add(E element) {
+ public void add(@ParametricNullness E element) {
delegate().add(element);
}
@@ -62,6 +64,7 @@
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E previous() {
return delegate().previous();
}
@@ -72,7 +75,7 @@
}
@Override
- public void set(E element) {
+ public void set(@ParametricNullness E element) {
delegate().set(element);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingMap.java b/guava/src/com/google/common/collect/ForwardingMap.java
index 3a6aa52..315a4fa 100644
--- a/guava/src/com/google/common/collect/ForwardingMap.java
+++ b/guava/src/com/google/common/collect/ForwardingMap.java
@@ -24,6 +24,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -55,7 +56,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingMap<K, V> extends ForwardingObject implements Map<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingMap<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingObject implements Map<K, V> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -76,7 +79,8 @@
@CanIgnoreReturnValue
@Override
- public V remove(Object key) {
+ @CheckForNull
+ public V remove(@CheckForNull Object key) {
return delegate().remove(key);
}
@@ -86,23 +90,25 @@
}
@Override
- public boolean containsKey(@Nullable Object key) {
+ public boolean containsKey(@CheckForNull Object key) {
return delegate().containsKey(key);
}
@Override
- public boolean containsValue(@Nullable Object value) {
+ public boolean containsValue(@CheckForNull Object value) {
return delegate().containsValue(value);
}
@Override
- public V get(@Nullable Object key) {
+ @CheckForNull
+ public V get(@CheckForNull Object key) {
return delegate().get(key);
}
@CanIgnoreReturnValue
@Override
- public V put(K key, V value) {
+ @CheckForNull
+ public V put(@ParametricNullness K key, @ParametricNullness V value) {
return delegate().put(key, value);
}
@@ -127,7 +133,7 @@
}
@Override
- public boolean equals(@Nullable Object object) {
+ public boolean equals(@CheckForNull Object object) {
return object == this || delegate().equals(object);
}
@@ -158,7 +164,8 @@
* @since 7.0
*/
@Beta
- protected V standardRemove(@Nullable Object key) {
+ @CheckForNull
+ protected V standardRemove(@CheckForNull Object key) {
Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
while (entryIterator.hasNext()) {
Entry<K, V> entry = entryIterator.next();
@@ -207,7 +214,7 @@
* @since 7.0
*/
@Beta
- protected boolean standardContainsKey(@Nullable Object key) {
+ protected boolean standardContainsKey(@CheckForNull Object key) {
return Maps.containsKeyImpl(this, key);
}
@@ -235,7 +242,7 @@
*
* @since 7.0
*/
- protected boolean standardContainsValue(@Nullable Object value) {
+ protected boolean standardContainsValue(@CheckForNull Object value) {
return Maps.containsValueImpl(this, value);
}
@@ -277,7 +284,7 @@
*
* @since 7.0
*/
- protected boolean standardEquals(@Nullable Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
return Maps.equalsImpl(this, object);
}
diff --git a/guava/src/com/google/common/collect/ForwardingMapEntry.java b/guava/src/com/google/common/collect/ForwardingMapEntry.java
index c775986..6816ccb 100644
--- a/guava/src/com/google/common/collect/ForwardingMapEntry.java
+++ b/guava/src/com/google/common/collect/ForwardingMapEntry.java
@@ -21,6 +21,7 @@
import com.google.common.base.Objects;
import java.util.Map;
import java.util.Map.Entry;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -47,7 +48,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingMapEntry<K, V> extends ForwardingObject implements Map.Entry<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingMapEntry<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingObject implements Map.Entry<K, V> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -57,22 +60,25 @@
protected abstract Entry<K, V> delegate();
@Override
+ @ParametricNullness
public K getKey() {
return delegate().getKey();
}
@Override
+ @ParametricNullness
public V getValue() {
return delegate().getValue();
}
@Override
- public V setValue(V value) {
+ @ParametricNullness
+ public V setValue(@ParametricNullness V value) {
return delegate().setValue(value);
}
@Override
- public boolean equals(@Nullable Object object) {
+ public boolean equals(@CheckForNull Object object) {
return delegate().equals(object);
}
@@ -88,7 +94,7 @@
*
* @since 7.0
*/
- protected boolean standardEquals(@Nullable Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
if (object instanceof Entry) {
Entry<?, ?> that = (Entry<?, ?>) object;
return Objects.equal(this.getKey(), that.getKey())
diff --git a/guava/src/com/google/common/collect/ForwardingNavigableMap.java b/guava/src/com/google/common/collect/ForwardingNavigableMap.java
index 5f23d05..c32468d 100644
--- a/guava/src/com/google/common/collect/ForwardingNavigableMap.java
+++ b/guava/src/com/google/common/collect/ForwardingNavigableMap.java
@@ -16,7 +16,6 @@
package com.google.common.collect;
-import static com.google.common.collect.CollectPreconditions.checkRemove;
import static com.google.common.collect.Maps.keyOrNull;
import com.google.common.annotations.Beta;
@@ -27,6 +26,8 @@
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.function.BiFunction;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A navigable map which forwards all its method calls to another navigable map. Subclasses should
@@ -55,8 +56,9 @@
* @since 12.0
*/
@GwtIncompatible
-public abstract class ForwardingNavigableMap<K, V> extends ForwardingSortedMap<K, V>
- implements NavigableMap<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingNavigableMap<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingSortedMap<K, V> implements NavigableMap<K, V> {
/** Constructor for use by subclasses. */
protected ForwardingNavigableMap() {}
@@ -65,7 +67,8 @@
protected abstract NavigableMap<K, V> delegate();
@Override
- public Entry<K, V> lowerEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> lowerEntry(@ParametricNullness K key) {
return delegate().lowerEntry(key);
}
@@ -74,12 +77,14 @@
* #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override {@code
* lowerEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardLowerEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardLowerEntry(@ParametricNullness K key) {
return headMap(key, false).lastEntry();
}
@Override
- public K lowerKey(K key) {
+ @CheckForNull
+ public K lowerKey(@ParametricNullness K key) {
return delegate().lowerKey(key);
}
@@ -88,12 +93,14 @@
* {@link #lowerEntry}, you may wish to override {@code lowerKey} to forward to this
* implementation.
*/
- protected K standardLowerKey(K key) {
+ @CheckForNull
+ protected K standardLowerKey(@ParametricNullness K key) {
return keyOrNull(lowerEntry(key));
}
@Override
- public Entry<K, V> floorEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> floorEntry(@ParametricNullness K key) {
return delegate().floorEntry(key);
}
@@ -102,12 +109,14 @@
* #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override {@code
* floorEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardFloorEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardFloorEntry(@ParametricNullness K key) {
return headMap(key, true).lastEntry();
}
@Override
- public K floorKey(K key) {
+ @CheckForNull
+ public K floorKey(@ParametricNullness K key) {
return delegate().floorKey(key);
}
@@ -116,12 +125,14 @@
* {@code floorEntry}, you may wish to override {@code floorKey} to forward to this
* implementation.
*/
- protected K standardFloorKey(K key) {
+ @CheckForNull
+ protected K standardFloorKey(@ParametricNullness K key) {
return keyOrNull(floorEntry(key));
}
@Override
- public Entry<K, V> ceilingEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> ceilingEntry(@ParametricNullness K key) {
return delegate().ceilingEntry(key);
}
@@ -130,12 +141,14 @@
* #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override {@code
* ceilingEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardCeilingEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardCeilingEntry(@ParametricNullness K key) {
return tailMap(key, true).firstEntry();
}
@Override
- public K ceilingKey(K key) {
+ @CheckForNull
+ public K ceilingKey(@ParametricNullness K key) {
return delegate().ceilingKey(key);
}
@@ -144,12 +157,14 @@
* {@code ceilingEntry}, you may wish to override {@code ceilingKey} to forward to this
* implementation.
*/
- protected K standardCeilingKey(K key) {
+ @CheckForNull
+ protected K standardCeilingKey(@ParametricNullness K key) {
return keyOrNull(ceilingEntry(key));
}
@Override
- public Entry<K, V> higherEntry(K key) {
+ @CheckForNull
+ public Entry<K, V> higherEntry(@ParametricNullness K key) {
return delegate().higherEntry(key);
}
@@ -158,12 +173,14 @@
* #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override {@code
* higherEntry} to forward to this implementation.
*/
- protected Entry<K, V> standardHigherEntry(K key) {
+ @CheckForNull
+ protected Entry<K, V> standardHigherEntry(@ParametricNullness K key) {
return tailMap(key, false).firstEntry();
}
@Override
- public K higherKey(K key) {
+ @CheckForNull
+ public K higherKey(@ParametricNullness K key) {
return delegate().higherKey(key);
}
@@ -172,11 +189,13 @@
* {@code higherEntry}, you may wish to override {@code higherKey} to forward to this
* implementation.
*/
- protected K standardHigherKey(K key) {
+ @CheckForNull
+ protected K standardHigherKey(@ParametricNullness K key) {
return keyOrNull(higherEntry(key));
}
@Override
+ @CheckForNull
public Entry<K, V> firstEntry() {
return delegate().firstEntry();
}
@@ -186,6 +205,7 @@
* #entrySet}. If you override {@code entrySet}, you may wish to override {@code firstEntry} to
* forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardFirstEntry() {
return Iterables.getFirst(entrySet(), null);
}
@@ -205,6 +225,7 @@
}
@Override
+ @CheckForNull
public Entry<K, V> lastEntry() {
return delegate().lastEntry();
}
@@ -214,6 +235,7 @@
* #entrySet} of {@link #descendingMap}. If you override {@code descendingMap}, you may wish to
* override {@code lastEntry} to forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardLastEntry() {
return Iterables.getFirst(descendingMap().entrySet(), null);
}
@@ -232,6 +254,7 @@
}
@Override
+ @CheckForNull
public Entry<K, V> pollFirstEntry() {
return delegate().pollFirstEntry();
}
@@ -241,11 +264,13 @@
* entrySet}. If you override {@code entrySet}, you may wish to override {@code pollFirstEntry} to
* forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardPollFirstEntry() {
return Iterators.pollNext(entrySet().iterator());
}
@Override
+ @CheckForNull
public Entry<K, V> pollLastEntry() {
return delegate().pollLastEntry();
}
@@ -255,6 +280,7 @@
* entrySet} of {@code descendingMap}. If you override {@code descendingMap}, you may wish to
* override {@code pollFirstEntry} to forward to this implementation.
*/
+ @CheckForNull
protected Entry<K, V> standardPollLastEntry() {
return Iterators.pollNext(descendingMap().entrySet().iterator());
}
@@ -293,8 +319,8 @@
@Override
protected Iterator<Entry<K, V>> entryIterator() {
return new Iterator<Entry<K, V>>() {
- private Entry<K, V> toRemove = null;
- private Entry<K, V> nextOrNull = forward().lastEntry();
+ @CheckForNull private Entry<K, V> toRemove = null;
+ @CheckForNull private Entry<K, V> nextOrNull = forward().lastEntry();
@Override
public boolean hasNext() {
@@ -303,7 +329,7 @@
@Override
public java.util.Map.Entry<K, V> next() {
- if (!hasNext()) {
+ if (nextOrNull == null) {
throw new NoSuchElementException();
}
try {
@@ -316,7 +342,9 @@
@Override
public void remove() {
- checkRemove(toRemove != null);
+ if (toRemove == null) {
+ throw new IllegalStateException("no calls to next() since the last call to remove()");
+ }
forward().remove(toRemove.getKey());
toRemove = null;
}
@@ -368,22 +396,27 @@
* wish to override {@code subMap} to forward to this implementation.
*/
@Override
- protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) {
+ protected SortedMap<K, V> standardSubMap(
+ @ParametricNullness K fromKey, @ParametricNullness K toKey) {
return subMap(fromKey, true, toKey, false);
}
@Override
- public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ public NavigableMap<K, V> subMap(
+ @ParametricNullness K fromKey,
+ boolean fromInclusive,
+ @ParametricNullness K toKey,
+ boolean toInclusive) {
return delegate().subMap(fromKey, fromInclusive, toKey, toInclusive);
}
@Override
- public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
+ public NavigableMap<K, V> headMap(@ParametricNullness K toKey, boolean inclusive) {
return delegate().headMap(toKey, inclusive);
}
@Override
- public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
+ public NavigableMap<K, V> tailMap(@ParametricNullness K fromKey, boolean inclusive) {
return delegate().tailMap(fromKey, inclusive);
}
@@ -392,7 +425,7 @@
* boolean)}. If you override {@code headMap(K, boolean)}, you may wish to override {@code
* headMap} to forward to this implementation.
*/
- protected SortedMap<K, V> standardHeadMap(K toKey) {
+ protected SortedMap<K, V> standardHeadMap(@ParametricNullness K toKey) {
return headMap(toKey, false);
}
@@ -401,7 +434,7 @@
* boolean)}. If you override {@code tailMap(K, boolean)}, you may wish to override {@code
* tailMap} to forward to this implementation.
*/
- protected SortedMap<K, V> standardTailMap(K fromKey) {
+ protected SortedMap<K, V> standardTailMap(@ParametricNullness K fromKey) {
return tailMap(fromKey, true);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingNavigableSet.java b/guava/src/com/google/common/collect/ForwardingNavigableSet.java
index 827698e..6822aa8 100644
--- a/guava/src/com/google/common/collect/ForwardingNavigableSet.java
+++ b/guava/src/com/google/common/collect/ForwardingNavigableSet.java
@@ -21,6 +21,8 @@
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A navigable set which forwards all its method calls to another navigable set. Subclasses should
@@ -49,8 +51,9 @@
* @since 12.0
*/
@GwtIncompatible
-public abstract class ForwardingNavigableSet<E> extends ForwardingSortedSet<E>
- implements NavigableSet<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingNavigableSet<E extends @Nullable Object>
+ extends ForwardingSortedSet<E> implements NavigableSet<E> {
/** Constructor for use by subclasses. */
protected ForwardingNavigableSet() {}
@@ -59,7 +62,8 @@
protected abstract NavigableSet<E> delegate();
@Override
- public E lower(E e) {
+ @CheckForNull
+ public E lower(@ParametricNullness E e) {
return delegate().lower(e);
}
@@ -68,12 +72,14 @@
* {@link #headSet(Object, boolean)}. If you override {@link #headSet(Object, boolean)}, you may
* wish to override {@link #lower} to forward to this implementation.
*/
- protected E standardLower(E e) {
+ @CheckForNull
+ protected E standardLower(@ParametricNullness E e) {
return Iterators.getNext(headSet(e, false).descendingIterator(), null);
}
@Override
- public E floor(E e) {
+ @CheckForNull
+ public E floor(@ParametricNullness E e) {
return delegate().floor(e);
}
@@ -82,12 +88,14 @@
* {@link #headSet(Object, boolean)}. If you override {@link #headSet(Object, boolean)}, you may
* wish to override {@link #floor} to forward to this implementation.
*/
- protected E standardFloor(E e) {
+ @CheckForNull
+ protected E standardFloor(@ParametricNullness E e) {
return Iterators.getNext(headSet(e, true).descendingIterator(), null);
}
@Override
- public E ceiling(E e) {
+ @CheckForNull
+ public E ceiling(@ParametricNullness E e) {
return delegate().ceiling(e);
}
@@ -96,12 +104,14 @@
* #tailSet(Object, boolean)}. If you override {@link #tailSet(Object, boolean)}, you may wish to
* override {@link #ceiling} to forward to this implementation.
*/
- protected E standardCeiling(E e) {
+ @CheckForNull
+ protected E standardCeiling(@ParametricNullness E e) {
return Iterators.getNext(tailSet(e, true).iterator(), null);
}
@Override
- public E higher(E e) {
+ @CheckForNull
+ public E higher(@ParametricNullness E e) {
return delegate().higher(e);
}
@@ -110,11 +120,13 @@
* #tailSet(Object, boolean)}. If you override {@link #tailSet(Object, boolean)}, you may wish to
* override {@link #higher} to forward to this implementation.
*/
- protected E standardHigher(E e) {
+ @CheckForNull
+ protected E standardHigher(@ParametricNullness E e) {
return Iterators.getNext(tailSet(e, false).iterator(), null);
}
@Override
+ @CheckForNull
public E pollFirst() {
return delegate().pollFirst();
}
@@ -124,11 +136,13 @@
* override {@link #iterator} you may wish to override {@link #pollFirst} to forward to this
* implementation.
*/
+ @CheckForNull
protected E standardPollFirst() {
return Iterators.pollNext(iterator());
}
@Override
+ @CheckForNull
public E pollLast() {
return delegate().pollLast();
}
@@ -138,14 +152,17 @@
* If you override {@link #descendingIterator} you may wish to override {@link #pollLast} to
* forward to this implementation.
*/
+ @CheckForNull
protected E standardPollLast() {
return Iterators.pollNext(descendingIterator());
}
+ @ParametricNullness
protected E standardFirst() {
return iterator().next();
}
+ @ParametricNullness
protected E standardLast() {
return descendingIterator().next();
}
@@ -179,7 +196,10 @@
@Override
public NavigableSet<E> subSet(
- E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ @ParametricNullness E fromElement,
+ boolean fromInclusive,
+ @ParametricNullness E toElement,
+ boolean toInclusive) {
return delegate().subSet(fromElement, fromInclusive, toElement, toInclusive);
}
@@ -190,7 +210,10 @@
*/
@Beta
protected NavigableSet<E> standardSubSet(
- E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ @ParametricNullness E fromElement,
+ boolean fromInclusive,
+ @ParametricNullness E toElement,
+ boolean toInclusive) {
return tailSet(fromElement, fromInclusive).headSet(toElement, toInclusive);
}
@@ -201,12 +224,13 @@
* implementation.
*/
@Override
- protected SortedSet<E> standardSubSet(E fromElement, E toElement) {
+ protected SortedSet<E> standardSubSet(
+ @ParametricNullness E fromElement, @ParametricNullness E toElement) {
return subSet(fromElement, true, toElement, false);
}
@Override
- public NavigableSet<E> headSet(E toElement, boolean inclusive) {
+ public NavigableSet<E> headSet(@ParametricNullness E toElement, boolean inclusive) {
return delegate().headSet(toElement, inclusive);
}
@@ -215,12 +239,12 @@
* boolean)} method. If you override {@link #headSet(Object, boolean)}, you may wish to override
* {@link #headSet(Object)} to forward to this implementation.
*/
- protected SortedSet<E> standardHeadSet(E toElement) {
+ protected SortedSet<E> standardHeadSet(@ParametricNullness E toElement) {
return headSet(toElement, false);
}
@Override
- public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
+ public NavigableSet<E> tailSet(@ParametricNullness E fromElement, boolean inclusive) {
return delegate().tailSet(fromElement, inclusive);
}
@@ -229,7 +253,7 @@
* boolean)} method. If you override {@link #tailSet(Object, boolean)}, you may wish to override
* {@link #tailSet(Object)} to forward to this implementation.
*/
- protected SortedSet<E> standardTailSet(E fromElement) {
+ protected SortedSet<E> standardTailSet(@ParametricNullness E fromElement) {
return tailSet(fromElement, true);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingObject.java b/guava/src/com/google/common/collect/ForwardingObject.java
index 712b14f..64af908 100644
--- a/guava/src/com/google/common/collect/ForwardingObject.java
+++ b/guava/src/com/google/common/collect/ForwardingObject.java
@@ -44,6 +44,7 @@
* @since 2.0
*/
@GwtCompatible
+@ElementTypesAreNonnullByDefault
public abstract class ForwardingObject {
/** Constructor for use by subclasses. */
diff --git a/guava/src/com/google/common/collect/ForwardingQueue.java b/guava/src/com/google/common/collect/ForwardingQueue.java
index f77e560..43c2eaa 100644
--- a/guava/src/com/google/common/collect/ForwardingQueue.java
+++ b/guava/src/com/google/common/collect/ForwardingQueue.java
@@ -20,6 +20,8 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.NoSuchElementException;
import java.util.Queue;
+import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A queue which forwards all its method calls to another queue. Subclasses should override one or
@@ -44,7 +46,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingQueue<E> extends ForwardingCollection<E> implements Queue<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingQueue<E extends @Nullable Object> extends ForwardingCollection<E>
+ implements Queue<E> {
/** Constructor for use by subclasses. */
protected ForwardingQueue() {}
@@ -54,28 +58,32 @@
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
- public boolean offer(E o) {
+ public boolean offer(@ParametricNullness E o) {
return delegate().offer(o);
}
@CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
@Override
+ @CheckForNull
public E poll() {
return delegate().poll();
}
@CanIgnoreReturnValue
@Override
+ @ParametricNullness
public E remove() {
return delegate().remove();
}
@Override
+ @CheckForNull
public E peek() {
return delegate().peek();
}
@Override
+ @ParametricNullness
public E element() {
return delegate().element();
}
@@ -86,7 +94,7 @@
*
* @since 7.0
*/
- protected boolean standardOffer(E e) {
+ protected boolean standardOffer(@ParametricNullness E e) {
try {
return add(e);
} catch (IllegalStateException caught) {
@@ -100,6 +108,7 @@
*
* @since 7.0
*/
+ @CheckForNull
protected E standardPeek() {
try {
return element();
@@ -114,6 +123,7 @@
*
* @since 7.0
*/
+ @CheckForNull
protected E standardPoll() {
try {
return remove();
diff --git a/guava/src/com/google/common/collect/ForwardingSet.java b/guava/src/com/google/common/collect/ForwardingSet.java
index ff21924..bc27272 100644
--- a/guava/src/com/google/common/collect/ForwardingSet.java
+++ b/guava/src/com/google/common/collect/ForwardingSet.java
@@ -21,6 +21,7 @@
import com.google.common.annotations.GwtCompatible;
import java.util.Collection;
import java.util.Set;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -46,7 +47,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingSet<E> extends ForwardingCollection<E> implements Set<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingSet<E extends @Nullable Object> extends ForwardingCollection<E>
+ implements Set<E> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -56,7 +59,7 @@
protected abstract Set<E> delegate();
@Override
- public boolean equals(@Nullable Object object) {
+ public boolean equals(@CheckForNull Object object) {
return object == this || delegate().equals(object);
}
@@ -84,7 +87,7 @@
*
* @since 7.0
*/
- protected boolean standardEquals(@Nullable Object object) {
+ protected boolean standardEquals(@CheckForNull Object object) {
return Sets.equalsImpl(this, object);
}
diff --git a/guava/src/com/google/common/collect/ForwardingSortedMap.java b/guava/src/com/google/common/collect/ForwardingSortedMap.java
index 8539adf..e0882ab 100644
--- a/guava/src/com/google/common/collect/ForwardingSortedMap.java
+++ b/guava/src/com/google/common/collect/ForwardingSortedMap.java
@@ -23,6 +23,7 @@
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.SortedMap;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -51,8 +52,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingSortedMap<K, V> extends ForwardingMap<K, V>
- implements SortedMap<K, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingSortedMap<K extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingMap<K, V> implements SortedMap<K, V> {
// TODO(lowasser): identify places where thread safety is actually lost
/** Constructor for use by subclasses. */
@@ -62,32 +64,35 @@
protected abstract SortedMap<K, V> delegate();
@Override
+ @CheckForNull
public Comparator<? super K> comparator() {
return delegate().comparator();
}
@Override
+ @ParametricNullness
public K firstKey() {
return delegate().firstKey();
}
@Override
- public SortedMap<K, V> headMap(K toKey) {
+ public SortedMap<K, V> headMap(@ParametricNullness K toKey) {
return delegate().headMap(toKey);
}
@Override
+ @ParametricNullness
public K lastKey() {
return delegate().lastKey();
}
@Override
- public SortedMap<K, V> subMap(K fromKey, K toKey) {
+ public SortedMap<K, V> subMap(@ParametricNullness K fromKey, @ParametricNullness K toKey) {
return delegate().subMap(fromKey, toKey);
}
@Override
- public SortedMap<K, V> tailMap(K fromKey) {
+ public SortedMap<K, V> tailMap(@ParametricNullness K fromKey) {
return delegate().tailMap(fromKey);
}
@@ -106,14 +111,14 @@
}
}
- // unsafe, but worst case is a CCE is thrown, which callers will be expecting
- @SuppressWarnings("unchecked")
- private int unsafeCompare(Object k1, Object k2) {
- Comparator<? super K> comparator = comparator();
+ // unsafe, but worst case is a CCE or NPE is thrown, which callers will be expecting
+ @SuppressWarnings({"unchecked", "nullness"})
+ static int unsafeCompare(
+ @CheckForNull Comparator<?> comparator, @CheckForNull Object o1, @CheckForNull Object o2) {
if (comparator == null) {
- return ((Comparable<Object>) k1).compareTo(k2);
+ return ((Comparable<@Nullable Object>) o1).compareTo(o2);
} else {
- return ((Comparator<Object>) comparator).compare(k1, k2);
+ return ((Comparator<@Nullable Object>) comparator).compare(o1, o2);
}
}
@@ -126,13 +131,13 @@
*/
@Override
@Beta
- protected boolean standardContainsKey(@Nullable Object key) {
+ protected boolean standardContainsKey(@CheckForNull Object key) {
try {
- // any CCE will be caught
- @SuppressWarnings("unchecked")
- SortedMap<Object, V> self = (SortedMap<Object, V>) this;
+ // any CCE or NPE will be caught
+ @SuppressWarnings({"unchecked", "nullness"})
+ SortedMap<@Nullable Object, V> self = (SortedMap<@Nullable Object, V>) this;
Object ceilingKey = self.tailMap(key).firstKey();
- return unsafeCompare(ceilingKey, key) == 0;
+ return unsafeCompare(comparator(), ceilingKey, key) == 0;
} catch (ClassCastException | NoSuchElementException | NullPointerException e) {
return false;
}
@@ -147,7 +152,7 @@
*/
@Beta
protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) {
- checkArgument(unsafeCompare(fromKey, toKey) <= 0, "fromKey must be <= toKey");
+ checkArgument(unsafeCompare(comparator(), fromKey, toKey) <= 0, "fromKey must be <= toKey");
return tailMap(fromKey).headMap(toKey);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingSortedSet.java b/guava/src/com/google/common/collect/ForwardingSortedSet.java
index 4c4ddf4..32625af 100644
--- a/guava/src/com/google/common/collect/ForwardingSortedSet.java
+++ b/guava/src/com/google/common/collect/ForwardingSortedSet.java
@@ -16,12 +16,15 @@
package com.google.common.collect;
+import static com.google.common.collect.ForwardingSortedMap.unsafeCompare;
+
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.SortedSet;
+import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
@@ -52,7 +55,9 @@
* @since 2.0
*/
@GwtCompatible
-public abstract class ForwardingSortedSet<E> extends ForwardingSet<E> implements SortedSet<E> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingSortedSet<E extends @Nullable Object> extends ForwardingSet<E>
+ implements SortedSet<E> {
/** Constructor for use by subclasses. */
protected ForwardingSortedSet() {}
@@ -61,44 +66,38 @@
protected abstract SortedSet<E> delegate();
@Override
+ @CheckForNull
public Comparator<? super E> comparator() {
return delegate().comparator();
}
@Override
+ @ParametricNullness
public E first() {
return delegate().first();
}
@Override
- public SortedSet<E> headSet(E toElement) {
+ public SortedSet<E> headSet(@ParametricNullness E toElement) {
return delegate().headSet(toElement);
}
@Override
+ @ParametricNullness
public E last() {
return delegate().last();
}
@Override
- public SortedSet<E> subSet(E fromElement, E toElement) {
+ public SortedSet<E> subSet(@ParametricNullness E fromElement, @ParametricNullness E toElement) {
return delegate().subSet(fromElement, toElement);
}
@Override
- public SortedSet<E> tailSet(E fromElement) {
+ public SortedSet<E> tailSet(@ParametricNullness E fromElement) {
return delegate().tailSet(fromElement);
}
- // unsafe, but worst case is a CCE is thrown, which callers will be expecting
- @SuppressWarnings("unchecked")
- private int unsafeCompare(@Nullable Object o1, @Nullable Object o2) {
- Comparator<? super E> comparator = comparator();
- return (comparator == null)
- ? ((Comparable<Object>) o1).compareTo(o2)
- : ((Comparator<Object>) comparator).compare(o1, o2);
- }
-
/**
* A sensible definition of {@link #contains} in terms of the {@code first()} method of {@link
* #tailSet}. If you override {@link #tailSet}, you may wish to override {@link #contains} to
@@ -108,13 +107,13 @@
*/
@Override
@Beta
- protected boolean standardContains(@Nullable Object object) {
+ protected boolean standardContains(@CheckForNull Object object) {
try {
- // any ClassCastExceptions are caught
- @SuppressWarnings("unchecked")
- SortedSet<Object> self = (SortedSet<Object>) this;
+ // any ClassCastExceptions and NullPointerExceptions are caught
+ @SuppressWarnings({"unchecked", "nullness"})
+ SortedSet<@Nullable Object> self = (SortedSet<@Nullable Object>) this;
Object ceiling = self.tailSet(object).first();
- return unsafeCompare(ceiling, object) == 0;
+ return unsafeCompare(comparator(), ceiling, object) == 0;
} catch (ClassCastException | NoSuchElementException | NullPointerException e) {
return false;
}
@@ -129,15 +128,15 @@
*/
@Override
@Beta
- protected boolean standardRemove(@Nullable Object object) {
+ protected boolean standardRemove(@CheckForNull Object object) {
try {
- // any ClassCastExceptions are caught
- @SuppressWarnings("unchecked")
- SortedSet<Object> self = (SortedSet<Object>) this;
- Iterator<Object> iterator = self.tailSet(object).iterator();
+ // any ClassCastExceptions and NullPointerExceptions are caught
+ @SuppressWarnings({"unchecked", "nullness"})
+ SortedSet<@Nullable Object> self = (SortedSet<@Nullable Object>) this;
+ Iterator<?> iterator = self.tailSet(object).iterator();
if (iterator.hasNext()) {
Object ceiling = iterator.next();
- if (unsafeCompare(ceiling, object) == 0) {
+ if (unsafeCompare(comparator(), ceiling, object) == 0) {
iterator.remove();
return true;
}
@@ -156,7 +155,8 @@
* @since 7.0
*/
@Beta
- protected SortedSet<E> standardSubSet(E fromElement, E toElement) {
+ protected SortedSet<E> standardSubSet(
+ @ParametricNullness E fromElement, @ParametricNullness E toElement) {
return tailSet(fromElement).headSet(toElement);
}
}
diff --git a/guava/src/com/google/common/collect/ForwardingTable.java b/guava/src/com/google/common/collect/ForwardingTable.java
index ea80383..4fcb858 100644
--- a/guava/src/com/google/common/collect/ForwardingTable.java
+++ b/guava/src/com/google/common/collect/ForwardingTable.java
@@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A table which forwards all its method calls to another table. Subclasses should override one or
@@ -32,7 +33,10 @@
* @since 7.0
*/
@GwtCompatible
-public abstract class ForwardingTable<R, C, V> extends ForwardingObject implements Table<R, C, V> {
+@ElementTypesAreNonnullByDefault
+public abstract class ForwardingTable<
+ R extends @Nullable Object, C extends @Nullable Object, V extends @Nullable Object>
+ extends ForwardingObject implements Table<R, C, V> {
/** Constructor for use by subclasses. */
protected ForwardingTable() {}
@@ -50,7 +54,7 @@
}
@Override
- public Map<R, V> column(C columnKey) {
+ public Map<R, V> column(@ParametricNullness C columnKey) {
return delegate().column(columnKey);
}
@@ -65,28 +69,28 @@
}
@Override
- public boolean contains(Object rowKey, Object columnKey) {
+ public boolean contains(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
return delegate().contains(rowKey, columnKey);
}
@Override
- public boolean containsColumn(Object columnKey) {
+ public boolean containsColumn(@CheckForNull Object columnKey) {
return delegate().containsColumn(columnKey);
}
@Override
- public boolean containsRow(Object rowKey) {
+ public boolean containsRow(@CheckForNull Object rowKey) {
return delegate().containsRow(rowKey);
}
@Override
- public boolean containsValue(Object value) {
+ public boolean containsValue(@CheckForNull Object value) {
return delegate().containsValue(value);
}
@Override
@CheckForNull
- public V get(Object rowKey, Object columnKey) {
+ public V get(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
return delegate().get(rowKey, columnKey);
}
@@ -98,7 +102,8 @@
@CanIgnoreReturnValue
@Override
@CheckForNull
- public V put(R rowKey, C columnKey, V value) {
+ public V put(
+ @ParametricNullness R rowKey, @ParametricNullness C columnKey, @ParametricNullness V value) {
return delegate().put(rowKey, columnKey, value);
}
@@ -110,12 +115,12 @@
@CanIgnoreReturnValue
@Override
@CheckForNull
- public V remove(Object rowKey, Object columnKey) {
+ public V remove(@CheckForNull Object rowKey, @CheckForNull Object columnKey) {
return delegate().remove(rowKey, columnKey);
}
@Override
- public Map<C, V> row(R rowKey) {
+ public Map<C, V> row(@ParametricNullness R rowKey) {
return delegate().row(rowKey);
}
@@ -140,7 +145,7 @@
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@CheckForNull Object obj) {
return (obj == this) || delegate().equals(obj);
}
diff --git a/guava/src/com/google/common/collect/ImmutableCollection.java b/guava/src/com/google/common/collect/ImmutableCollection.java
index 557131d..80fefb6 100644
--- a/guava/src/com/google/common/collect/ImmutableCollection.java
+++ b/guava/src/com/google/common/collect/ImmutableCollection.java
@@ -198,8 +198,7 @@
/*
* This suppression is here for two reasons:
*
- * 1. Our checker says "found: T[]; required: T[]." That sounds bogus. I discuss a possible reason
- * for this error in https://github.com/jspecify/checker-framework/issues/10.
+ * 1. b/192354773 in our checker affects toArray declarations.
*
* 2. `other[size] = null` is unsound. We could "fix" this by requiring callers to pass in an
* array with a nullable element type. But probably they usually want an array with a non-nullable