Skip to content

Commit

Permalink
Add @DoNotCall to the mutator methods on immutable types
Browse files Browse the repository at this point in the history
RELNOTES=Add `@DoNotCall` to the mutator methods on immutable types
PiperOrigin-RevId: 359348257
  • Loading branch information
kluever authored and Google Java Core Libraries committed Feb 24, 2021
1 parent 906cf06 commit 6ae9532
Show file tree
Hide file tree
Showing 34 changed files with 167 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void testBuilder() {
assertMapEquals(map, "five", 5, "four", 4, "one", 1, "three", 3, "two", 2);
}

@SuppressWarnings("DoNotCall")
public void testBuilder_orderEntriesByValueFails() {
ImmutableSortedMap.Builder<String, Integer> builder = ImmutableSortedMap.naturalOrder();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
Expand Down Expand Up @@ -346,7 +347,8 @@ final ImmutableSet<V> createValues() {
@CanIgnoreReturnValue
@Deprecated
@Override
public V forcePut(K key, V value) {
@DoNotCall("Always throws UnsupportedOperationException")
public final V forcePut(K key, V value) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.primitives.Primitives;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.Immutable;
import java.io.Serializable;
import java.util.Map;
Expand Down Expand Up @@ -184,6 +185,7 @@ public <T extends B> T getInstance(Class<T> type) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public <T extends B> T putInstance(Class<T> type, T value) {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.DoNotMock;
import java.io.Serializable;
import java.util.AbstractCollection;
Expand Down Expand Up @@ -231,6 +232,7 @@ int internalArrayEnd() {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean add(E e) {
throw new UnsupportedOperationException();
}
Expand All @@ -244,6 +246,7 @@ public final boolean add(E e) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean remove(Object object) {
throw new UnsupportedOperationException();
}
Expand All @@ -257,6 +260,7 @@ public final boolean remove(Object object) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean addAll(Collection<? extends E> newElements) {
throw new UnsupportedOperationException();
}
Expand All @@ -270,6 +274,7 @@ public final boolean addAll(Collection<? extends E> newElements) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean removeAll(Collection<?> oldElements) {
throw new UnsupportedOperationException();
}
Expand All @@ -283,6 +288,7 @@ public final boolean removeAll(Collection<?> oldElements) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean retainAll(Collection<?> elementsToKeep) {
throw new UnsupportedOperationException();
}
Expand All @@ -295,6 +301,7 @@ public final boolean retainAll(Collection<?> elementsToKeep) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final void clear() {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
Expand Down Expand Up @@ -485,6 +486,7 @@ boolean isPartialView() {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean addAll(int index, Collection<? extends E> newElements) {
throw new UnsupportedOperationException();
}
Expand All @@ -498,6 +500,7 @@ public final boolean addAll(int index, Collection<? extends E> newElements) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final E set(int index, E element) {
throw new UnsupportedOperationException();
}
Expand All @@ -510,6 +513,7 @@ public final E set(int index, E element) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final void add(int index, E element) {
throw new UnsupportedOperationException();
}
Expand All @@ -523,6 +527,7 @@ public final void add(int index, E element) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final E remove(int index) {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import java.io.IOException;
Expand Down Expand Up @@ -351,7 +352,8 @@ private ImmutableListMultimap<V, K> invert() {
@CanIgnoreReturnValue
@Deprecated
@Override
public ImmutableList<V> removeAll(Object key) {
@DoNotCall("Always throws UnsupportedOperationException")
public final ImmutableList<V> removeAll(Object key) {
throw new UnsupportedOperationException();
}

Expand All @@ -364,7 +366,8 @@ public ImmutableList<V> removeAll(Object key) {
@CanIgnoreReturnValue
@Deprecated
@Override
public ImmutableList<V> replaceValues(K key, Iterable<? extends V> values) {
@DoNotCall("Always throws UnsupportedOperationException")
public final ImmutableList<V> replaceValues(K key, Iterable<? extends V> values) {
throw new UnsupportedOperationException();
}

Expand Down
4 changes: 4 additions & 0 deletions android/guava/src/com/google/common/collect/ImmutableMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.DoNotMock;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
Expand Down Expand Up @@ -462,6 +463,7 @@ ImmutableCollection<V> createValues() {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final V put(K k, V v) {
throw new UnsupportedOperationException();
}
Expand All @@ -487,6 +489,7 @@ public final V remove(Object o) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final void putAll(Map<? extends K, ? extends V> map) {
throw new UnsupportedOperationException();
}
Expand All @@ -499,6 +502,7 @@ public final void putAll(Map<? extends K, ? extends V> map) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final void clear() {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.WeakOuter;
import java.io.Serializable;
Expand Down Expand Up @@ -228,6 +229,7 @@ public boolean contains(@NullableDecl Object object) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final int add(E element, int occurrences) {
throw new UnsupportedOperationException();
}
Expand All @@ -241,6 +243,7 @@ public final int add(E element, int occurrences) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final int remove(Object element, int occurrences) {
throw new UnsupportedOperationException();
}
Expand All @@ -254,6 +257,7 @@ public final int remove(Object element, int occurrences) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final int setCount(E element, int count) {
throw new UnsupportedOperationException();
}
Expand All @@ -267,6 +271,7 @@ public final int setCount(E element, int count) {
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public final boolean setCount(E element, int oldCount, int newCount) {
throw new UnsupportedOperationException();
}
Expand Down
16 changes: 11 additions & 5 deletions android/guava/src/com/google/common/collect/ImmutableRangeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.SortedLists.KeyAbsentBehavior;
import com.google.common.collect.SortedLists.KeyPresentBehavior;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.DoNotMock;
import java.io.Serializable;
import java.util.Collections;
Expand Down Expand Up @@ -208,7 +209,8 @@ public Range<K> span() {
*/
@Deprecated
@Override
public void put(Range<K> range, V value) {
@DoNotCall("Always throws UnsupportedOperationException")
public final void put(Range<K> range, V value) {
throw new UnsupportedOperationException();
}

Expand All @@ -220,7 +222,8 @@ public void put(Range<K> range, V value) {
*/
@Deprecated
@Override
public void putCoalescing(Range<K> range, V value) {
@DoNotCall("Always throws UnsupportedOperationException")
public final void putCoalescing(Range<K> range, V value) {
throw new UnsupportedOperationException();
}

Expand All @@ -232,7 +235,8 @@ public void putCoalescing(Range<K> range, V value) {
*/
@Deprecated
@Override
public void putAll(RangeMap<K, V> rangeMap) {
@DoNotCall("Always throws UnsupportedOperationException")
public final void putAll(RangeMap<K, V> rangeMap) {
throw new UnsupportedOperationException();
}

Expand All @@ -244,7 +248,8 @@ public void putAll(RangeMap<K, V> rangeMap) {
*/
@Deprecated
@Override
public void clear() {
@DoNotCall("Always throws UnsupportedOperationException")
public final void clear() {
throw new UnsupportedOperationException();
}

Expand All @@ -256,7 +261,8 @@ public void clear() {
*/
@Deprecated
@Override
public void remove(Range<K> range) {
@DoNotCall("Always throws UnsupportedOperationException")
public final void remove(Range<K> range) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.SortedLists.KeyPresentBehavior;
import com.google.common.primitives.Ints;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.io.Serializable;
import java.util.Collections;
Expand Down Expand Up @@ -205,6 +206,7 @@ public boolean isEmpty() {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public void add(Range<C> range) {
throw new UnsupportedOperationException();
}
Expand All @@ -217,6 +219,7 @@ public void add(Range<C> range) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public void addAll(RangeSet<C> other) {
throw new UnsupportedOperationException();
}
Expand All @@ -229,6 +232,7 @@ public void addAll(RangeSet<C> other) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public void addAll(Iterable<Range<C>> other) {
throw new UnsupportedOperationException();
}
Expand All @@ -241,6 +245,7 @@ public void addAll(Iterable<Range<C>> other) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public void remove(Range<C> range) {
throw new UnsupportedOperationException();
}
Expand All @@ -253,6 +258,7 @@ public void remove(Range<C> range) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public void removeAll(RangeSet<C> other) {
throw new UnsupportedOperationException();
}
Expand All @@ -265,6 +271,7 @@ public void removeAll(RangeSet<C> other) {
*/
@Deprecated
@Override
@DoNotCall("Always throws UnsupportedOperationException")
public void removeAll(Iterable<Range<C>> other) {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import com.google.j2objc.annotations.Weak;
Expand Down Expand Up @@ -397,7 +398,8 @@ private ImmutableSetMultimap<V, K> invert() {
@CanIgnoreReturnValue
@Deprecated
@Override
public ImmutableSet<V> removeAll(Object key) {
@DoNotCall("Always throws UnsupportedOperationException")
public final ImmutableSet<V> removeAll(Object key) {
throw new UnsupportedOperationException();
}

Expand All @@ -410,7 +412,8 @@ public ImmutableSet<V> removeAll(Object key) {
@CanIgnoreReturnValue
@Deprecated
@Override
public ImmutableSet<V> replaceValues(K key, Iterable<? extends V> values) {
@DoNotCall("Always throws UnsupportedOperationException")
public final ImmutableSet<V> replaceValues(K key, Iterable<? extends V> values) {
throw new UnsupportedOperationException();
}

Expand Down
Loading

0 comments on commit 6ae9532

Please sign in to comment.