diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 05a82df71c1d5..57d44cb35f572 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,6 +51,7 @@ jobs: run: make jdk build_jdk21u: + if: endsWith(github.repository, '/jdk') needs: - check_generated_ci runs-on: ubuntu-latest diff --git a/.github/workflows/ci.yml.m4 b/.github/workflows/ci.yml.m4 index 4b09b9583f000..50210af2491f2 100644 --- a/.github/workflows/ci.yml.m4 +++ b/.github/workflows/ci.yml.m4 @@ -55,6 +55,7 @@ jobs: run: make jdk build_jdk21u: + if: endsWith(github.repository, '/jdk') needs: - check_generated_ci runs-on: ubuntu-latest diff --git a/src/java.base/share/classes/java/io/BufferedReader.java b/src/java.base/share/classes/java/io/BufferedReader.java index 5e68906a069cc..168f571e45c35 100644 --- a/src/java.base/share/classes/java/io/BufferedReader.java +++ b/src/java.base/share/classes/java/io/BufferedReader.java @@ -40,6 +40,7 @@ import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.Iterator; import java.util.NoSuchElementException; @@ -510,8 +511,8 @@ private long implSkip(long n) throws IOException { * * @throws IOException If an I/O error occurs */ - @EnsuresNonNullIf(expression={"readLine()"}, result=true) @Pure + @EnsuresNonNullIf(expression={"readLine()"}, result=true) public boolean ready(@GuardSatisfied BufferedReader this) throws IOException { Object lock = this.lock; if (lock instanceof InternalLock locker) { @@ -709,6 +710,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public String next(/*@NonEmpty Iterator this*/) { if (nextLine != null || hasNext()) { String line = nextLine; diff --git a/src/java.base/share/classes/java/lang/CharSequence.java b/src/java.base/share/classes/java/lang/CharSequence.java index 6c9ddd0ea61cd..3469c83c9bd9e 100644 --- a/src/java.base/share/classes/java/lang/CharSequence.java +++ b/src/java.base/share/classes/java/lang/CharSequence.java @@ -36,6 +36,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.NoSuchElementException; import java.util.Objects; @@ -175,6 +176,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int nextInt(@NonEmpty CharIterator this) { if (hasNext()) { return charAt(cur++); @@ -252,6 +254,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int nextInt(@NonEmpty CodePointIterator this) { final int length = length(); diff --git a/src/java.base/share/classes/java/lang/Class.java b/src/java.base/share/classes/java/lang/Class.java index 7e7cabd891789..4353f31e9d743 100644 --- a/src/java.base/share/classes/java/lang/Class.java +++ b/src/java.base/share/classes/java/lang/Class.java @@ -840,8 +840,8 @@ public Void run() { * {@code false} otherwise. * @since 1.1 */ - @EnsuresNonNullIf(expression={"getComponentType()"}, result=true) @Pure + @EnsuresNonNullIf(expression={"getComponentType()"}, result=true) @IntrinsicCandidate public native boolean isArray(@GuardSatisfied Class this); diff --git a/src/java.base/share/classes/java/lang/String.java b/src/java.base/share/classes/java/lang/String.java index 985accbb2669a..0a6d2f8f404aa 100644 --- a/src/java.base/share/classes/java/lang/String.java +++ b/src/java.base/share/classes/java/lang/String.java @@ -1945,8 +1945,8 @@ public void getBytes(@IndexOrHigh({"this"}) int srcBegin, @IndexOrHigh({"this"}) * @see #compareTo(String) * @see #equalsIgnoreCase(String) */ - @EnsuresNonNullIf(expression={"#1"}, result=true) @Pure + @EnsuresNonNullIf(expression={"#1"}, result=true) @StaticallyExecutable public boolean equals(@GuardSatisfied @Nullable Object anObject) { if (this == anObject) { @@ -2084,8 +2084,8 @@ public boolean contentEquals(@GuardSatisfied CharSequence cs) { * @see #equals(Object) * @see #codePoints() */ - @EnsuresNonNullIf(expression={"#1"}, result=true) @Pure + @EnsuresNonNullIf(expression={"#1"}, result=true) @StaticallyExecutable public boolean equalsIgnoreCase(@Nullable String anotherString) { return (this == anotherString) ? true diff --git a/src/java.base/share/classes/java/lang/invoke/AbstractConstantGroup.java b/src/java.base/share/classes/java/lang/invoke/AbstractConstantGroup.java index a6883d734afaf..075fe0f562d23 100644 --- a/src/java.base/share/classes/java/lang/invoke/AbstractConstantGroup.java +++ b/src/java.base/share/classes/java/lang/invoke/AbstractConstantGroup.java @@ -29,6 +29,7 @@ import org.checkerframework.checker.nonempty.qual.NonEmpty; import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.*; import jdk.internal.vm.annotation.Stable; @@ -101,6 +102,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Object next(@NonEmpty AsIterator this) { int i = bumpIndex(); if (resolving) diff --git a/src/java.base/share/classes/java/net/URL.java b/src/java.base/share/classes/java/net/URL.java index 39cbdd607c10f..9aaf4a1b7df71 100644 --- a/src/java.base/share/classes/java/net/URL.java +++ b/src/java.base/share/classes/java/net/URL.java @@ -35,6 +35,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.File; import java.io.IOException; @@ -1513,6 +1514,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public URLStreamHandlerProvider next(/*@NonEmpty Iterator this*/) { if (!getNext()) throw new NoSuchElementException(); diff --git a/src/java.base/share/classes/java/nio/charset/Charset.java b/src/java.base/share/classes/java/nio/charset/Charset.java index 5d59179d95ace..346e27c985c86 100644 --- a/src/java.base/share/classes/java/nio/charset/Charset.java +++ b/src/java.base/share/classes/java/nio/charset/Charset.java @@ -29,6 +29,7 @@ import org.checkerframework.checker.nonempty.qual.NonEmpty; import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import jdk.internal.misc.ThreadTracker; import jdk.internal.misc.VM; @@ -371,6 +372,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public CharsetProvider next(/*@NonEmpty Iterator this*/) { if (!getNext()) throw new NoSuchElementException(); diff --git a/src/java.base/share/classes/java/nio/file/FileTreeIterator.java b/src/java.base/share/classes/java/nio/file/FileTreeIterator.java index b3df235085f69..fff8cbec47500 100644 --- a/src/java.base/share/classes/java/nio/file/FileTreeIterator.java +++ b/src/java.base/share/classes/java/nio/file/FileTreeIterator.java @@ -29,6 +29,7 @@ import org.checkerframework.checker.nonempty.qual.NonEmpty; import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.Closeable; import java.io.IOException; @@ -113,6 +114,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Event next(@NonEmpty FileTreeIterator this) { if (!walker.isOpen()) throw new IllegalStateException(); diff --git a/src/java.base/share/classes/java/nio/file/Files.java b/src/java.base/share/classes/java/nio/file/Files.java index d6aba35bdaa3f..1a5099e646bd5 100644 --- a/src/java.base/share/classes/java/nio/file/Files.java +++ b/src/java.base/share/classes/java/nio/file/Files.java @@ -30,12 +30,13 @@ import org.checkerframework.checker.mustcall.qual.MustCall; import org.checkerframework.checker.nonempty.qual.EnsuresNonEmptyIf; import org.checkerframework.checker.nonempty.qual.NonEmpty; +import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.signedness.qual.PolySigned; import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; -import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -3866,6 +3867,7 @@ public boolean hasNext() { } } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Override public Path next(/*@NonEmpty Iterator this*/) { try { diff --git a/src/java.base/share/classes/java/nio/file/Path.java b/src/java.base/share/classes/java/nio/file/Path.java index f190d043b54b5..d1ab91c4bec5b 100644 --- a/src/java.base/share/classes/java/nio/file/Path.java +++ b/src/java.base/share/classes/java/nio/file/Path.java @@ -32,6 +32,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.File; import java.io.IOException; @@ -963,6 +964,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Path next(/*@NonEmpty Iterator this*/) { if (i < getNameCount()) { Path result = getName(i); diff --git a/src/java.base/share/classes/java/security/Provider.java b/src/java.base/share/classes/java/security/Provider.java index 22ebc08a5c8a4..9cba2a016e10a 100644 --- a/src/java.base/share/classes/java/security/Provider.java +++ b/src/java.base/share/classes/java/security/Provider.java @@ -28,6 +28,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.PolyNull; import org.checkerframework.checker.signedness.qual.UnknownSignedness; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import jdk.internal.event.SecurityProviderServiceEvent; @@ -635,6 +636,7 @@ public synchronized Object replace(Object key, Object value) { * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void replaceAll(BiFunction function) { check("putProviderProperty." + name); @@ -663,6 +665,7 @@ public synchronized void replaceAll(BiFunction remappingFunction) { check("putProviderProperty." + name); @@ -693,6 +696,7 @@ public synchronized void replaceAll(BiFunction mappingFunction) { check("putProviderProperty." + name); @@ -722,6 +726,7 @@ public synchronized void replaceAll(BiFunction remappingFunction) { @@ -755,6 +760,7 @@ public synchronized void replaceAll(BiFunction remappingFunction) { diff --git a/src/java.base/share/classes/java/util/AbstractCollection.java b/src/java.base/share/classes/java/util/AbstractCollection.java index 7b1300ad87e46..d38628827e01a 100644 --- a/src/java.base/share/classes/java/util/AbstractCollection.java +++ b/src/java.base/share/classes/java/util/AbstractCollection.java @@ -39,6 +39,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import jdk.internal.util.ArraysSupport; @@ -275,6 +276,8 @@ private static T[] finishToArray(T[] r, Iterator it) { * @throws IllegalArgumentException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied AbstractCollection this, E e) { throw new UnsupportedOperationException(); } @@ -296,6 +299,8 @@ public boolean add(@GuardSatisfied AbstractCollection this, E e) { * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @CanShrink AbstractCollection this, @GuardSatisfied @UnknownSignedness Object o) { Iterator it = iterator(); if (o==null) { @@ -359,6 +364,8 @@ public boolean containsAll(@GuardSatisfied AbstractCollection this, @GuardSat * * @see #add(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied AbstractCollection this, Collection c) { boolean modified = false; for (E e : c) @@ -389,6 +396,8 @@ public boolean addAll(@GuardSatisfied AbstractCollection this, Collection this, Collection c) { Objects.requireNonNull(c); boolean modified = false; @@ -424,6 +433,8 @@ public boolean removeAll(@GuardSatisfied @CanShrink AbstractCollection this, * @see #remove(Object) * @see #contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(@GuardSatisfied @CanShrink AbstractCollection this, Collection c) { Objects.requireNonNull(c); boolean modified = false; @@ -453,6 +464,8 @@ public boolean retainAll(@GuardSatisfied @CanShrink AbstractCollection this, * * @throws UnsupportedOperationException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink AbstractCollection this) { Iterator it = iterator(); while (it.hasNext()) { diff --git a/src/java.base/share/classes/java/util/AbstractList.java b/src/java.base/share/classes/java/util/AbstractList.java index 4040d013945ec..7997e85e864b4 100644 --- a/src/java.base/share/classes/java/util/AbstractList.java +++ b/src/java.base/share/classes/java/util/AbstractList.java @@ -42,6 +42,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; @@ -131,6 +132,8 @@ protected AbstractList() { * prevents it from being added to this list */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied AbstractList this, E e) { add(size(), e); return true; @@ -157,6 +160,8 @@ public boolean add(@GuardSatisfied AbstractList this, E e) { * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(@GuardSatisfied AbstractList this, @IndexFor({"this"}) int index, E element) { throw new UnsupportedOperationException(); } @@ -174,6 +179,8 @@ public E set(@GuardSatisfied AbstractList this, @IndexFor({"this"}) int index * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(@GuardSatisfied AbstractList this, @IndexOrHigh({"this"}) int index, E element) { throw new UnsupportedOperationException(); } @@ -188,6 +195,8 @@ public void add(@GuardSatisfied AbstractList this, @IndexOrHigh({"this"}) int * @throws UnsupportedOperationException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @CanShrink AbstractList this, @IndexFor({"this"}) int index) { throw new UnsupportedOperationException(); } @@ -266,6 +275,8 @@ public E remove(@GuardSatisfied @CanShrink AbstractList this, @IndexFor({"thi * @throws UnsupportedOperationException if the {@code clear} operation * is not supported by this list */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink AbstractList this) { removeRange(0, size()); } @@ -290,6 +301,8 @@ public void clear(@GuardSatisfied @CanShrink AbstractList this) { * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied AbstractList this, @IndexOrHigh({"this"}) int index, Collection c) { rangeCheckForAdd(index); boolean modified = false; @@ -395,6 +408,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { checkForComodification(); try { @@ -409,6 +423,8 @@ public E next(@NonEmpty Itr this) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastRet < 0) throw new IllegalStateException(); @@ -436,6 +452,7 @@ private class ListItr extends Itr implements ListIterator { cursor = index; } + @Pure public boolean hasPrevious() { return cursor != 0; } @@ -453,14 +470,18 @@ public E previous() { } } + @Pure public int nextIndex() { return cursor; } + @Pure public int previousIndex() { return cursor-1; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); @@ -474,6 +495,8 @@ public void set(E e) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { checkForComodification(); @@ -625,6 +648,8 @@ public int hashCode(@GuardSatisfied AbstractList this) { * @param fromIndex index of first element to be removed * @param toIndex index after last element to be removed */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") protected void removeRange(@GuardSatisfied @CanShrink AbstractList this, @IndexOrHigh({"this"}) int fromIndex, @IndexOrHigh({"this"}) int toIndex) { ListIterator it = listIterator(fromIndex); for (int i=0, n=toIndex-fromIndex; i E get(List list, int i) { try { return list.get(i); @@ -808,12 +834,15 @@ protected SubList(SubList parent, int fromIndex, int toIndex) { this.modCount = root.modCount; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { Objects.checkIndex(index, size); checkForComodification(); return root.set(offset + index, element); } + @Pure public E get(int index) { Objects.checkIndex(index, size); checkForComodification(); @@ -826,6 +855,8 @@ public int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { rangeCheckForAdd(index); checkForComodification(); @@ -833,6 +864,8 @@ public void add(int index, E element) { updateSizeAndModCount(1); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { Objects.checkIndex(index, size); checkForComodification(); @@ -847,10 +880,14 @@ protected void removeRange(int fromIndex, int toIndex) { updateSizeAndModCount(fromIndex - toIndex); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { return addAll(size, c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { rangeCheckForAdd(index); int cSize = c.size(); @@ -862,6 +899,7 @@ public boolean addAll(int index, Collection c) { return true; } + @SideEffectFree public Iterator iterator() { return listIterator(); } @@ -881,6 +919,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(/*@NonEmpty ListIterator this*/) { if (hasNext()) return i.next(); @@ -911,15 +950,21 @@ public int previousIndex() { return i.previousIndex() - offset; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { i.remove(); updateSizeAndModCount(-1); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { i.set(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { i.add(e); updateSizeAndModCount(1); @@ -927,6 +972,7 @@ public void add(E e) { }; } + @SideEffectFree public List subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); @@ -976,6 +1022,7 @@ private static class RandomAccessSubList super(parent, fromIndex, toIndex); } + @SideEffectFree public List subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new RandomAccessSubList<>(this, fromIndex, toIndex); diff --git a/src/java.base/share/classes/java/util/AbstractMap.java b/src/java.base/share/classes/java/util/AbstractMap.java index 3bcd121323d56..79f7049ca7047 100644 --- a/src/java.base/share/classes/java/util/AbstractMap.java +++ b/src/java.base/share/classes/java/util/AbstractMap.java @@ -40,6 +40,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.stream.Stream; import java.util.function.Consumer; @@ -167,8 +168,8 @@ public boolean containsValue(@GuardSatisfied AbstractMap this, @GuardSatis * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public boolean containsKey(@GuardSatisfied AbstractMap this, @GuardSatisfied @UnknownSignedness Object key) { Iterator> i = entrySet().iterator(); if (key==null) { @@ -237,6 +238,8 @@ public boolean containsKey(@GuardSatisfied AbstractMap this, @GuardSatisfi */ @ReleasesNoLocks @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(@GuardSatisfied AbstractMap this, K key, V value) { throw new UnsupportedOperationException(); } @@ -263,6 +266,8 @@ public boolean containsKey(@GuardSatisfied AbstractMap this, @GuardSatisfi * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V remove(@GuardSatisfied AbstractMap this, @GuardSatisfied @UnknownSignedness Object key) { Iterator> i = entrySet().iterator(); Entry correctEntry = null; @@ -308,6 +313,8 @@ public boolean containsKey(@GuardSatisfied AbstractMap this, @GuardSatisfi * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(@GuardSatisfied AbstractMap this, Map m) { for (Map.Entry e : m.entrySet()) put(e.getKey(), e.getValue()); @@ -325,6 +332,8 @@ public void putAll(@GuardSatisfied AbstractMap this, Map this) { entrySet().clear(); } @@ -380,6 +389,7 @@ public void clear(@GuardSatisfied AbstractMap this) { Set ks = keySet; if (ks == null) { ks = new AbstractSet() { + @SideEffectFree public Iterator iterator() { return new Iterator() { private Iterator> i = entrySet().iterator(); @@ -391,10 +401,13 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(/*@NonEmpty Iterator this*/) { return i.next().getKey(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { i.remove(); } @@ -412,10 +425,13 @@ public boolean isEmpty() { return AbstractMap.this.isEmpty(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { AbstractMap.this.clear(); } + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object k) { return AbstractMap.this.containsKey(k); @@ -447,6 +463,7 @@ public Collection values(@GuardSatisfied AbstractMap this) { Collection vals = values; if (vals == null) { vals = new AbstractCollection() { + @SideEffectFree public Iterator iterator() { return new Iterator() { private Iterator> i = entrySet().iterator(); @@ -458,10 +475,13 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(/*@NonEmpty Iterator this*/) { return i.next().getValue(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { i.remove(); } @@ -479,10 +499,13 @@ public boolean isEmpty() { return AbstractMap.this.isEmpty(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { AbstractMap.this.clear(); } + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object v) { return AbstractMap.this.containsValue(v); @@ -615,6 +638,7 @@ public String toString(@GuardSatisfied AbstractMap this) { * * @return a shallow copy of this map */ + @SideEffectFree protected Object clone() throws CloneNotSupportedException { AbstractMap result = (AbstractMap)super.clone(); result.keySet = null; @@ -718,6 +742,8 @@ public V getValue(AbstractMap.@GuardSatisfied SimpleEntry this) { * @param value new value to be stored in this entry * @return the old value corresponding to the entry */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V setValue(AbstractMap.@GuardSatisfied SimpleEntry this, V value) { V oldValue = this.value; this.value = value; @@ -961,25 +987,47 @@ public String toString(AbstractMap.@GuardSatisfied SimpleImmutableEntry th UnsupportedOperationException uoe() { return new UnsupportedOperationException(); } abstract Collection view(); + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E t) { throw uoe(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { throw uoe(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { view().clear(); } + @Pure public boolean contains(Object o) { return view().contains(o); } + @Pure public boolean containsAll(Collection c) { return view().containsAll(c); } public void forEach(Consumer c) { view().forEach(c); } + @Pure public boolean isEmpty() { return view().isEmpty(); } + @SideEffectFree public Iterator iterator() { return view().iterator(); } public Stream parallelStream() { return view().parallelStream(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(Object o) { return view().remove(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return view().removeAll(c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { return view().removeIf(filter); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { return view().retainAll(c); } + @Pure public int size() { return view().size(); } + @SideEffectFree public Spliterator spliterator() { return view().spliterator(); } public Stream stream() { return view().stream(); } + @SideEffectFree public Object[] toArray() { return view().toArray(); } public T[] toArray(IntFunction generator) { return view().toArray(generator); } public T[] toArray(T[] a) { return view().toArray(a); } + @SideEffectFree public String toString() { return view().toString(); } } } diff --git a/src/java.base/share/classes/java/util/AbstractQueue.java b/src/java.base/share/classes/java/util/AbstractQueue.java index 0bd11fc96d0f9..c349250b238ae 100644 --- a/src/java.base/share/classes/java/util/AbstractQueue.java +++ b/src/java.base/share/classes/java/util/AbstractQueue.java @@ -39,7 +39,9 @@ import org.checkerframework.checker.lock.qual.GuardSatisfied; import org.checkerframework.checker.nonempty.qual.EnsuresNonEmpty; import org.checkerframework.checker.nonempty.qual.NonEmpty; +import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * This class provides skeletal implementations of some {@link Queue} @@ -99,6 +101,8 @@ protected AbstractQueue() { * prevents it from being added to this queue */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied AbstractQueue this, E e) { if (offer(e)) return true; @@ -117,6 +121,8 @@ public boolean add(@GuardSatisfied AbstractQueue this, E e) { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @NonEmpty @CanShrink AbstractQueue this) { E x = poll(); if (x != null) @@ -136,6 +142,7 @@ public E remove(@GuardSatisfied @NonEmpty @CanShrink AbstractQueue this) { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ + @Pure public E element(@GuardSatisfied @NonEmpty AbstractQueue this) { E x = peek(); if (x != null) @@ -151,6 +158,8 @@ public E element(@GuardSatisfied @NonEmpty AbstractQueue this) { *

This implementation repeatedly invokes {@link #poll poll} until it * returns {@code null}. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink AbstractQueue this) { while (poll() != null) ; @@ -185,6 +194,8 @@ public void clear(@GuardSatisfied @CanShrink AbstractQueue this) { * this time due to insertion restrictions * @see #add(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied AbstractQueue this, Collection c) { if (c == null) throw new NullPointerException(); diff --git a/src/java.base/share/classes/java/util/AbstractSequentialList.java b/src/java.base/share/classes/java/util/AbstractSequentialList.java index 9097f49066f51..a8932d5d6673d 100644 --- a/src/java.base/share/classes/java/util/AbstractSequentialList.java +++ b/src/java.base/share/classes/java/util/AbstractSequentialList.java @@ -33,6 +33,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * This class provides a skeletal implementation of the {@code List} @@ -124,6 +125,8 @@ public E get(@GuardSatisfied AbstractSequentialList this, int index) { * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(@GuardSatisfied AbstractSequentialList this, int index, E element) { try { ListIterator e = listIterator(index); @@ -155,6 +158,8 @@ public E set(@GuardSatisfied AbstractSequentialList this, int index, E elemen * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(@GuardSatisfied AbstractSequentialList this, int index, E element) { try { listIterator(index).add(element); @@ -180,6 +185,8 @@ public void add(@GuardSatisfied AbstractSequentialList this, int index, E ele * @throws UnsupportedOperationException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @CanShrink AbstractSequentialList this, int index) { try { ListIterator e = listIterator(index); @@ -223,6 +230,8 @@ public E remove(@GuardSatisfied @CanShrink AbstractSequentialList this, int i * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied AbstractSequentialList this, int index, Collection c) { try { boolean modified = false; diff --git a/src/java.base/share/classes/java/util/AbstractSet.java b/src/java.base/share/classes/java/util/AbstractSet.java index a18723fbaca86..19e90776f4b1a 100644 --- a/src/java.base/share/classes/java/util/AbstractSet.java +++ b/src/java.base/share/classes/java/util/AbstractSet.java @@ -31,6 +31,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * This class provides a skeletal implementation of the {@code Set} @@ -174,6 +175,8 @@ public int hashCode(@GuardSatisfied AbstractSet this) { * @see #remove(Object) * @see #contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@GuardSatisfied AbstractSet this, Collection c) { Objects.requireNonNull(c); boolean modified = false; diff --git a/src/java.base/share/classes/java/util/ArrayDeque.java b/src/java.base/share/classes/java/util/ArrayDeque.java index 83d23ebdf6dd6..56490f9810600 100644 --- a/src/java.base/share/classes/java/util/ArrayDeque.java +++ b/src/java.base/share/classes/java/util/ArrayDeque.java @@ -52,6 +52,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.Serializable; import java.util.function.Consumer; @@ -301,6 +302,8 @@ static final E nonNullElementAt(@PolyNull @PolySigned Object[] es, int i) { * @param e the element to add * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(@GuardSatisfied ArrayDeque this, E e) { if (e == null) throw new NullPointerException(); @@ -318,6 +321,8 @@ public void addFirst(@GuardSatisfied ArrayDeque this, E e) { * @param e the element to add * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(@GuardSatisfied ArrayDeque this, E e) { if (e == null) throw new NullPointerException(); @@ -337,6 +342,8 @@ public void addLast(@GuardSatisfied ArrayDeque this, E e) { * @throws NullPointerException if the specified collection or any * of its elements are null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { final int s, needed; if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0) @@ -356,6 +363,8 @@ private void copyElements(Collection c) { * @return {@code true} (as specified by {@link Deque#offerFirst}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerFirst(E e) { addFirst(e); return true; @@ -368,6 +377,8 @@ public boolean offerFirst(E e) { * @return {@code true} (as specified by {@link Deque#offerLast}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerLast(E e) { addLast(e); return true; @@ -376,6 +387,8 @@ public boolean offerLast(E e) { /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { E e = pollFirst(); if (e == null) @@ -386,6 +399,8 @@ public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { E e = pollLast(); if (e == null) @@ -393,6 +408,8 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { return e; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst(@GuardSatisfied @CanShrink ArrayDeque this) { final Object[] es; final int h; @@ -404,6 +421,8 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { return e; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast(@GuardSatisfied @CanShrink ArrayDeque this) { final Object[] es; final int t; @@ -416,6 +435,7 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E getFirst(@GuardSatisfied @NonEmpty ArrayDeque this) { E e = elementAt(elements, head); if (e == null) @@ -426,6 +446,7 @@ public E getFirst(@GuardSatisfied @NonEmpty ArrayDeque this) { /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E getLast(@GuardSatisfied @NonEmpty ArrayDeque this) { final Object[] es = elements; E e = elementAt(es, dec(tail, es.length)); @@ -457,6 +478,8 @@ public E getLast(@GuardSatisfied @NonEmpty ArrayDeque this) { * @param o element to be removed from this deque, if present * @return {@code true} if the deque contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeFirstOccurrence(@GuardSatisfied @CanShrink ArrayDeque this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o != null) { final Object[] es = elements; @@ -485,6 +508,8 @@ public boolean removeFirstOccurrence(@GuardSatisfied @CanShrink ArrayDeque th * @param o element to be removed from this deque, if present * @return {@code true} if the deque contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeLastOccurrence(@GuardSatisfied @CanShrink ArrayDeque this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o != null) { final Object[] es = elements; @@ -513,6 +538,8 @@ public boolean removeLastOccurrence(@GuardSatisfied @CanShrink ArrayDeque thi * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied ArrayDeque this, E e) { addLast(e); return true; @@ -527,6 +554,8 @@ public boolean add(@GuardSatisfied ArrayDeque this, E e) { * @return {@code true} (as specified by {@link Queue#offer}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(@GuardSatisfied ArrayDeque this, E e) { return offerLast(e); } @@ -542,6 +571,8 @@ public boolean offer(@GuardSatisfied ArrayDeque this, E e) { * @return the head of the queue represented by this deque * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { return removeFirst(); } @@ -556,6 +587,8 @@ public E remove(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { * @return the head of the queue represented by this deque, or * {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink ArrayDeque this) { return pollFirst(); } @@ -570,6 +603,7 @@ public E remove(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { * @return the head of the queue represented by this deque * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E element(@GuardSatisfied @NonEmpty ArrayDeque this) { return getFirst(); } @@ -599,6 +633,8 @@ public E element(@GuardSatisfied @NonEmpty ArrayDeque this) { * @param e the element to push * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void push(@GuardSatisfied ArrayDeque this, E e) { addFirst(e); } @@ -613,6 +649,8 @@ public void push(@GuardSatisfied ArrayDeque this, E e) { * of the stack represented by this deque) * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pop(@GuardSatisfied @NonEmpty @CanShrink ArrayDeque this) { return removeFirst(); } @@ -725,6 +763,7 @@ public final boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty DeqIterator this) { if (remaining <= 0) throw new NoSuchElementException(); @@ -740,6 +779,8 @@ void postDelete(boolean leftShifted) { cursor = dec(cursor, elements.length); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void remove() { if (lastRet < 0) throw new IllegalStateException(); @@ -773,6 +814,8 @@ public void forEachRemaining(Consumer action) { private class DescendingIterator extends DeqIterator { DescendingIterator() { cursor = dec(tail, elements.length); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final E next(@NonEmpty DescendingIterator this) { if (remaining <= 0) throw new NoSuchElementException(); @@ -825,6 +868,7 @@ public final void forEachRemaining(Consumer action) { * @return a {@code Spliterator} over the elements in this deque * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return new DeqSpliterator(); } @@ -928,6 +972,8 @@ public void forEach(Consumer action) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink ArrayDeque this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -936,6 +982,8 @@ public boolean removeIf(@CanShrink ArrayDeque this, Predicate filt /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@CanShrink ArrayDeque this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -944,6 +992,8 @@ public boolean removeAll(@CanShrink ArrayDeque this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); @@ -1060,6 +1110,8 @@ public boolean contains(@GuardSatisfied ArrayDeque this, @GuardSatisfied @Nul * @param o element to be removed from this deque, if present * @return {@code true} if this deque contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @CanShrink ArrayDeque this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { return removeFirstOccurrence(o); } @@ -1068,6 +1120,8 @@ public boolean remove(@GuardSatisfied @CanShrink ArrayDeque this, @GuardSatis * Removes all of the elements from this deque. * The deque will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink ArrayDeque this) { circularClear(elements, head, tail); head = tail = 0; diff --git a/src/java.base/share/classes/java/util/ArrayList.java b/src/java.base/share/classes/java/util/ArrayList.java index 11a972708dab6..1ec69c77e7020 100644 --- a/src/java.base/share/classes/java/util/ArrayList.java +++ b/src/java.base/share/classes/java/util/ArrayList.java @@ -45,6 +45,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; import java.util.function.Predicate; @@ -448,6 +449,7 @@ E elementData(@NonNegative int index) { } @SuppressWarnings("unchecked") + @Pure static E elementAt(Object[] es, int index) { return (E) es[index]; } @@ -471,6 +473,7 @@ public E get(@GuardSatisfied ArrayList this, @NonNegative int index) { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure public E getFirst() { if (size == 0) { throw new NoSuchElementException(); @@ -485,6 +488,7 @@ public E getFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure public E getLast() { int last = size - 1; if (last < 0) { @@ -504,6 +508,7 @@ public E getLast() { * @throws IndexOutOfBoundsException {@inheritDoc} */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(@GuardSatisfied ArrayList this, @NonNegative int index, E element) { Objects.checkIndex(index, size); E oldValue = elementData(index); @@ -532,6 +537,8 @@ private void add(E e, Object[] elementData, int s) { */ // @SideEffectsOnly("this") @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied ArrayList this, E e) { modCount++; add(e, elementData, size); @@ -548,6 +555,7 @@ public boolean add(@GuardSatisfied ArrayList this, E e) { * @throws IndexOutOfBoundsException {@inheritDoc} */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(@GuardSatisfied ArrayList this, @NonNegative int index, E element) { rangeCheckForAdd(index); modCount++; @@ -567,6 +575,8 @@ public void add(@GuardSatisfied ArrayList this, @NonNegative int index, E ele * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E element) { add(0, element); } @@ -576,6 +586,8 @@ public void addFirst(E element) { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E element) { add(element); } @@ -589,6 +601,8 @@ public void addLast(E element) { * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @CanShrink ArrayList this, @NonNegative int index) { Objects.checkIndex(index, size); final Object[] es = elementData; @@ -605,6 +619,8 @@ public E remove(@GuardSatisfied @CanShrink ArrayList this, @NonNegative int i * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { if (size == 0) { throw new NoSuchElementException(); @@ -622,6 +638,8 @@ public E removeFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { int last = size - 1; if (last < 0) { @@ -703,6 +721,7 @@ private void checkForComodification(final int expectedModCount) { /** * {@inheritDoc} */ + @Pure public int hashCode() { int expectedModCount = modCount; int hash = hashCodeRange(0, size); @@ -736,6 +755,8 @@ int hashCodeRange(int from, int to) { * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @CanShrink ArrayList this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { final Object[] es = elementData; final int size = this.size; @@ -772,6 +793,8 @@ private void fastRemove(Object[] es, int i) { * Removes all of the elements from this list. The list will * be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink ArrayList this) { modCount++; final Object[] es = elementData; @@ -793,6 +816,7 @@ public void clear(@GuardSatisfied @CanShrink ArrayList this) { * @throws NullPointerException if the specified collection is null */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied ArrayList this, Collection c) { Object[] a = c.toArray(); modCount++; @@ -824,6 +848,7 @@ public boolean addAll(@GuardSatisfied ArrayList this, Collection * @throws NullPointerException if the specified collection is null */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied ArrayList this, @NonNegative int index, Collection c) { rangeCheckForAdd(index); @@ -915,6 +940,8 @@ private static String outOfBoundsMsg(int fromIndex, int toIndex) { * or if the specified collection is null * @see Collection#contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@CanShrink ArrayList this, Collection c) { return batchRemove(c, false, 0, size); } @@ -935,6 +962,8 @@ public boolean removeAll(@CanShrink ArrayList this, Collection this, Collection c) { return batchRemove(c, true, 0, size); } @@ -1095,6 +1124,7 @@ public boolean hasNext() { @SuppressWarnings("unchecked") // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { checkForComodification(); int i = cursor; @@ -1107,6 +1137,8 @@ public E next(@NonEmpty Itr this) { return (E) elementData[lastRet = i]; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastRet < 0) throw new IllegalStateException(); @@ -1156,19 +1188,28 @@ private class ListItr extends Itr implements ListIterator { cursor = index; } + @Pure public boolean hasPrevious() { return cursor != 0; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure public int nextIndex() { return cursor; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure public int previousIndex() { return cursor - 1; } @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E previous() { checkForComodification(); int i = cursor - 1; @@ -1181,6 +1222,8 @@ public E previous() { return (E) elementData[lastRet = i]; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); @@ -1193,6 +1236,8 @@ public void set(E e) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { checkForComodification(); @@ -1237,6 +1282,9 @@ public void add(E e) { * @throws IndexOutOfBoundsException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @SideEffectFree public @PolyGrowShrink List subList(@GuardSatisfied @PolyGrowShrink ArrayList this, @NonNegative int fromIndex, @NonNegative int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); @@ -1270,6 +1318,8 @@ private SubList(SubList parent, int fromIndex, int toIndex) { this.modCount = parent.modCount; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(@NonNegative int index, E element) { Objects.checkIndex(index, size); checkForComodification(); @@ -1278,6 +1328,7 @@ public E set(@NonNegative int index, E element) { return oldValue; } + @Pure public E get(@NonNegative int index) { Objects.checkIndex(index, size); checkForComodification(); @@ -1290,6 +1341,8 @@ public E get(@NonNegative int index) { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(@NonNegative int index, E element) { rangeCheckForAdd(index); checkForComodification(); @@ -1297,6 +1350,8 @@ public void add(@NonNegative int index, E element) { updateSizeAndModCount(1); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@NonNegative int index) { Objects.checkIndex(index, size); checkForComodification(); @@ -1305,16 +1360,22 @@ public E remove(@NonNegative int index) { return result; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") protected void removeRange(int fromIndex, int toIndex) { checkForComodification(); root.removeRange(offset + fromIndex, offset + toIndex); updateSizeAndModCount(fromIndex - toIndex); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { return addAll(this.size, c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@NonNegative int index, Collection c) { rangeCheckForAdd(index); int cSize = c.size(); @@ -1326,14 +1387,19 @@ public boolean addAll(@NonNegative int index, Collection c) { return true; } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { root.replaceAllRange(operator, offset, offset + size); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return batchRemove(c, false); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { return batchRemove(c, true); } @@ -1348,6 +1414,8 @@ private boolean batchRemove(Collection c, boolean complement) { return modified; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { checkForComodification(); int oldSize = root.size; @@ -1357,6 +1425,7 @@ public boolean removeIf(Predicate filter) { return modified; } + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(SubList<@PolyNull @PolySigned E> this) { checkForComodification(); return Arrays.copyOfRange(root.elementData, offset, offset + size); @@ -1374,6 +1443,7 @@ public boolean removeIf(Predicate filter) { return a; } + @Pure public boolean equals(@Nullable Object o) { if (o == this) { return true; @@ -1388,24 +1458,28 @@ public boolean equals(@Nullable Object o) { return equal; } + @Pure public int hashCode() { int hash = root.hashCodeRange(offset, offset + size); checkForComodification(); return hash; } + @Pure public int indexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { int index = root.indexOfRange(o, offset, offset + size); checkForComodification(); return index >= 0 ? index - offset : -1; } + @Pure public int lastIndexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { int index = root.lastIndexOfRange(o, offset, offset + size); checkForComodification(); return index >= 0 ? index - offset : -1; } + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@Nullable @UnknownSignedness Object o) { return indexOf(o) >= 0; @@ -1445,6 +1519,7 @@ public E next(/*@NonEmpty ListIterator this*/) { return (E) elementData[offset + (lastRet = i)]; } + @Pure public boolean hasPrevious() { return cursor != 0; } @@ -1479,10 +1554,12 @@ public void forEachRemaining(Consumer action) { } } + @Pure public int nextIndex() { return cursor; } + @Pure public int previousIndex() { return cursor - 1; } @@ -1535,6 +1612,7 @@ final void checkForComodification() { }; } + @SideEffectFree public List subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); @@ -1642,6 +1720,7 @@ public int characteristics() { * @throws NullPointerException {@inheritDoc} */ @Override + @DoesNotUnrefineReceiver("modifiability") public void forEach(Consumer action) { Objects.requireNonNull(action); final int expectedModCount = modCount; @@ -1797,6 +1876,8 @@ private static boolean isClear(long[] bits, int i) { * @throws NullPointerException {@inheritDoc} */ @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink ArrayList this, Predicate filter) { return removeIf(filter, 0, size); } @@ -1840,6 +1921,7 @@ boolean removeIf(@CanShrink ArrayList this, Predicate filter, int @SuppressWarnings({"unchecked"}) @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { replaceAllRange(operator, 0, size); // TODO(8203662): remove increment of modCount from ... @@ -1858,6 +1940,8 @@ private void replaceAllRange(UnaryOperator operator, int i, int end) { @Override @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, size, c); diff --git a/src/java.base/share/classes/java/util/Arrays.java b/src/java.base/share/classes/java/util/Arrays.java index 71e85da99f5ef..37f0a44ea8a8a 100644 --- a/src/java.base/share/classes/java/util/Arrays.java +++ b/src/java.base/share/classes/java/util/Arrays.java @@ -47,6 +47,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import jdk.internal.util.ArraysSupport; import jdk.internal.vm.annotation.ForceInline; @@ -4402,6 +4403,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty ArrayItr this) { int i = cursor; if (i >= a.length) { diff --git a/src/java.base/share/classes/java/util/Collection.java b/src/java.base/share/classes/java/util/Collection.java index 01d2cd4416b1a..79c8e8a7bc811 100644 --- a/src/java.base/share/classes/java/util/Collection.java +++ b/src/java.base/share/classes/java/util/Collection.java @@ -41,6 +41,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.IntFunction; import java.util.function.Predicate; @@ -410,7 +411,6 @@ public interface Collection extends Iterable { * runtime component type} of the specified array * @throws NullPointerException if the specified array is null */ - @SideEffectFree @Nullable T [] toArray(@PolyNull T[] a); /** @@ -488,6 +488,8 @@ default T[] toArray(IntFunction generator) { * time due to insertion restrictions */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean add(@GuardSatisfied Collection this, E e); /** @@ -510,6 +512,8 @@ default T[] toArray(IntFunction generator) { * @throws UnsupportedOperationException if the {@code remove} operation * is not supported by this collection */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean remove(@GuardSatisfied @CanShrink Collection this, @UnknownSignedness Object o); @@ -562,6 +566,8 @@ default T[] toArray(IntFunction generator) { * this time due to insertion restrictions * @see #add(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean addAll(@GuardSatisfied Collection this, Collection c); /** @@ -587,6 +593,8 @@ default T[] toArray(IntFunction generator) { * @see #remove(Object) * @see #contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean removeAll(@GuardSatisfied @CanShrink Collection this, Collection c); /** @@ -611,6 +619,8 @@ default T[] toArray(IntFunction generator) { * supported. * @since 1.8 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default boolean removeIf(@CanShrink Collection this, Predicate filter) { Objects.requireNonNull(filter); boolean removed = false; @@ -646,6 +656,8 @@ default boolean removeIf(@CanShrink Collection this, Predicate fil * @see #remove(Object) * @see #contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean retainAll(@GuardSatisfied @CanShrink Collection this, Collection c); /** @@ -655,6 +667,8 @@ default boolean removeIf(@CanShrink Collection this, Predicate fil * @throws UnsupportedOperationException if the {@code clear} operation * is not supported by this collection */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void clear(@GuardSatisfied @CanShrink Collection this); @@ -785,6 +799,7 @@ default Spliterator spliterator() { * @return a sequential {@code Stream} over the elements in this collection * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default @PolyNonEmpty Stream stream(@PolyNonEmpty Collection this) { return StreamSupport.stream(spliterator(), false); } @@ -806,6 +821,7 @@ default Spliterator spliterator() { * collection * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default Stream parallelStream() { return StreamSupport.stream(spliterator(), true); } diff --git a/src/java.base/share/classes/java/util/Collections.java b/src/java.base/share/classes/java/util/Collections.java index 21eb1f6299f87..18a66f0ef8e61 100644 --- a/src/java.base/share/classes/java/util/Collections.java +++ b/src/java.base/share/classes/java/util/Collections.java @@ -48,6 +48,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.io.ObjectInputStream; @@ -290,6 +291,7 @@ else if (cmp > 0) * Gets the ith element from the given list by repositioning the specified * list listIterator. */ + // @SideEffectsOnly("#1") private static T get(ListIterator i, int index) { T obj; int pos = i.nextIndex(); @@ -1109,7 +1111,6 @@ static class UnmodifiableCollection implements Collection, Serializable { public boolean contains(@UnknownSignedness Object o) {return c.contains(o);} @SideEffectFree public @PolyNull @PolySigned Object[] toArray(Collections.UnmodifiableCollection<@PolyNull @PolySigned E> this) {return c.toArray();} - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) {return c.toArray(a);} public T[] toArray(IntFunction f) {return c.toArray(f);} public String toString() {return c.toString();} @@ -1123,11 +1124,15 @@ static class UnmodifiableCollection implements Collection, Serializable { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasNext() {return i.hasNext();} // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(/*@NonEmpty Iterator this*/) {return i.next();} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public void forEachRemaining(Consumer action) { // Use backing collection version i.forEachRemaining(action); @@ -1135,10 +1140,14 @@ public void forEachRemaining(Consumer action) { }; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmpty("this") public boolean add(E e) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { throw new UnsupportedOperationException(); } @@ -1147,25 +1156,36 @@ public boolean remove(@UnknownSignedness Object o) { public boolean containsAll(Collection coll) { return c.containsAll(coll); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection coll) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection coll) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection coll) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { throw new UnsupportedOperationException(); } // Override default methods in Collection @Override + @DoesNotUnrefineReceiver("modifiability") public void forEach(Consumer action) { c.forEach(action); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { throw new UnsupportedOperationException(); } @@ -1177,11 +1197,15 @@ public Spliterator spliterator() { } @SuppressWarnings("unchecked") @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Stream stream() { return (Stream)c.stream(); } @SuppressWarnings("unchecked") @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Stream parallelStream() { return (Stream)c.parallelStream(); } @@ -1239,30 +1263,42 @@ private SequencedCollection sc() { // Even though this wrapper class is serializable, the reversed view is effectively // not serializable because it points to the reversed collection view, which usually isn't // serializable. + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public SequencedCollection reversed() { return new UnmodifiableSequencedCollection<>(sc().reversed()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { throw new UnsupportedOperationException(); } + @Pure public E getFirst() { return sc().getFirst(); } + @Pure public E getLast() { return sc().getLast(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { throw new UnsupportedOperationException(); } @@ -1301,7 +1337,9 @@ static class UnmodifiableSet extends UnmodifiableCollection private static final long serialVersionUID = -9215047833775013803L; UnmodifiableSet(Set s) {super(s);} + @Pure public boolean equals(Object o) {return o == this || c.equals(o);} + @Pure public int hashCode() {return c.hashCode();} } @@ -1339,7 +1377,9 @@ static class UnmodifiableSequencedSet extends UnmodifiableSequencedCollection private static final long serialVersionUID = -2153469532349793522L; UnmodifiableSequencedSet(SequencedSet s) {super(s);} + @Pure public boolean equals(Object o) {return o == this || c.equals(o);} + @Pure public int hashCode() {return c.hashCode();} @SuppressWarnings("unchecked") @@ -1350,6 +1390,8 @@ private SequencedSet ss() { // Even though this wrapper class is serializable, the reversed view is effectively // not serializable because it points to the reversed set view, which usually isn't // serializable. + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public SequencedSet reversed() { return new UnmodifiableSequencedSet<>(ss().reversed()); } @@ -1393,14 +1435,21 @@ static class UnmodifiableSortedSet UnmodifiableSortedSet(SortedSet s) {super(s); ss = s;} + @Pure public Comparator comparator() {return ss.comparator();} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public SortedSet subSet(E fromElement, E toElement) { return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public SortedSet headSet(E toElement) { return new UnmodifiableSortedSet<>(ss.headSet(toElement)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public SortedSet tailSet(E fromElement) { return new UnmodifiableSortedSet<>(ss.tailSet(fromElement)); } @@ -1482,23 +1531,36 @@ public EmptyNavigableSet() { public E floor(E e) { return ns.floor(e); } public E ceiling(E e) { return ns.ceiling(e); } public E higher(E e) { return ns.higher(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pollFirst() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pollLast() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public NavigableSet descendingSet() { return new UnmodifiableNavigableSet<>(ns.descendingSet()); } + @SideEffectFree public Iterator descendingIterator() { return descendingSet().iterator(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return new UnmodifiableNavigableSet<>( ns.subSet(fromElement, fromInclusive, toElement, toInclusive)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public NavigableSet headSet(E toElement, boolean inclusive) { return new UnmodifiableNavigableSet<>( ns.headSet(toElement, inclusive)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public NavigableSet tailSet(E fromElement, boolean inclusive) { return new UnmodifiableNavigableSet<>( ns.tailSet(fromElement, inclusive)); @@ -1548,36 +1610,54 @@ static class UnmodifiableList extends UnmodifiableCollection this.list = list; } + @Pure public boolean equals(Object o) {return o == this || list.equals(o);} + @Pure public int hashCode() {return list.hashCode();} + @Pure public E get(int index) {return list.get(index);} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { throw new UnsupportedOperationException(); } + @Pure public int indexOf(Object o) {return list.indexOf(o);} + @Pure public int lastIndexOf(Object o) {return list.lastIndexOf(o);} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { throw new UnsupportedOperationException(); } public @PolyGrowShrink @PolyNonEmpty ListIterator listIterator(@PolyGrowShrink @PolyNonEmpty UnmodifiableList this) {return listIterator(0);} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public ListIterator listIterator(final int index) { return new ListIterator<>() { private final ListIterator i @@ -1587,20 +1667,29 @@ public ListIterator listIterator(final int index) { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasNext() {return i.hasNext();} // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(/*@NonEmpty ListIterator this*/) {return i.next();} @Pure public boolean hasPrevious() {return i.hasPrevious();} // @SideEffectsOnly("this") public E previous() {return i.previous();} + @Pure public int nextIndex() {return i.nextIndex();} + @Pure public int previousIndex() {return i.previousIndex();} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { throw new UnsupportedOperationException(); } @@ -1717,18 +1806,27 @@ private static class UnmodifiableMap implements Map, Serializable { public boolean containsKey(@UnknownSignedness Object key) {return m.containsKey(key);} @Pure public boolean containsValue(@UnknownSignedness Object val) {return m.containsValue(val);} + @Pure public V get(Object key) {return m.get(key);} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) public V put(K key, V value) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V remove(Object key) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(Map m) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { throw new UnsupportedOperationException(); } @@ -1756,7 +1854,9 @@ public Collection values() { return values; } + @Pure public boolean equals(Object o) {return o == this || m.equals(o);} + @Pure public int hashCode() {return m.hashCode();} public String toString() {return m.toString();} @@ -1775,10 +1875,13 @@ public void forEach(BiConsumer action) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) @Override public V putIfAbsent(K key, V value) { @@ -1786,38 +1889,48 @@ public V putIfAbsent(K key, V value) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { throw new UnsupportedOperationException(); @@ -1931,9 +2044,12 @@ public boolean hasNext() { return i.hasNext(); } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(/*@NonEmpty Iterator> this*/) { return new UnmodifiableEntry<>(i.next()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { throw new UnsupportedOperationException(); } @@ -1977,6 +2093,7 @@ public Object[] toArray() { * that the equality-candidate is Map.Entry and calls its * setValue method. */ + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object o) { if (!(o instanceof Map.Entry)) @@ -1998,6 +2115,7 @@ public boolean containsAll(Collection coll) } return true; } + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -2020,12 +2138,16 @@ private static class UnmodifiableEntry implements Map.Entry { UnmodifiableEntry(Map.Entry e) {this.e = Objects.requireNonNull(e);} + @Pure public K getKey() {return e.getKey();} + @Pure public V getValue() {return e.getValue();} public V setValue(V value) { throw new UnsupportedOperationException(); } + @Pure public int hashCode() {return e.hashCode();} + @Pure public boolean equals(Object o) { if (this == o) return true; @@ -2087,18 +2209,26 @@ public SequencedMap reversed() { return new UnmodifiableSequencedMap<>(sm().reversed()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollFirstEntry() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollLastEntry() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putFirst(K k, V v) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putLast(K k, V v) { throw new UnsupportedOperationException(); } @@ -2144,6 +2274,7 @@ static class UnmodifiableSortedMap private final SortedMap sm; UnmodifiableSortedMap(SortedMap m) {super(m); sm = m; } + @Pure public Comparator comparator() { return sm.comparator(); } @SideEffectFree public SortedMap subMap(K fromKey, K toKey) @@ -2287,8 +2418,12 @@ public Entry lastEntry() { : null; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollFirstEntry() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollLastEntry() { throw new UnsupportedOperationException(); } @SideEffectFree @@ -2398,7 +2533,6 @@ public boolean contains(@UnknownSignedness Object o) { public @PolyNull @PolySigned Object[] toArray(Collections.SynchronizedCollection<@PolyNull @PolySigned E> this) { synchronized (mutex) {return c.toArray();} } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { synchronized (mutex) {return c.toArray(a);} } @@ -2411,10 +2545,14 @@ public Iterator iterator() { return c.iterator(); // Must be manually synched by user! } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmpty("this") public boolean add(E e) { synchronized (mutex) {return c.add(e);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { synchronized (mutex) {return c.remove(o);} } @@ -2423,15 +2561,23 @@ public boolean remove(@UnknownSignedness Object o) { public boolean containsAll(Collection coll) { synchronized (mutex) {return c.containsAll(coll);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection coll) { synchronized (mutex) {return c.addAll(coll);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection coll) { synchronized (mutex) {return c.removeAll(coll);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection coll) { synchronized (mutex) {return c.retainAll(coll);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { synchronized (mutex) {c.clear();} } @@ -2444,6 +2590,8 @@ public void forEach(Consumer consumer) { synchronized (mutex) {c.forEach(consumer);} } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { synchronized (mutex) {return c.removeIf(filter);} } @@ -2517,11 +2665,13 @@ static class SynchronizedSet super(s, mutex); } + @Pure public boolean equals(Object o) { if (this == o) return true; synchronized (mutex) {return c.equals(o);} } + @Pure public int hashCode() { synchronized (mutex) {return c.hashCode();} } @@ -2592,6 +2742,7 @@ static class SynchronizedSortedSet ss = s; } + @Pure public Comparator comparator() { synchronized (mutex) {return ss.comparator();} } @@ -2692,7 +2843,11 @@ static class SynchronizedNavigableSet public E floor(E e) { synchronized (mutex) {return ns.floor(e);} } public E ceiling(E e) { synchronized (mutex) {return ns.ceiling(e);} } public E higher(E e) { synchronized (mutex) {return ns.higher(e);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pollFirst() { synchronized (mutex) {return ns.pollFirst();} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pollLast() { synchronized (mutex) {return ns.pollLast();} } public NavigableSet descendingSet() { @@ -2799,35 +2954,48 @@ static class SynchronizedList this.list = list; } + @Pure public boolean equals(Object o) { if (this == o) return true; synchronized (mutex) {return list.equals(o);} } + @Pure public int hashCode() { synchronized (mutex) {return list.hashCode();} } + @Pure public E get(int index) { synchronized (mutex) {return list.get(index);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { synchronized (mutex) {return list.set(index, element);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { synchronized (mutex) {list.add(index, element);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { synchronized (mutex) {return list.remove(index);} } + @Pure public int indexOf(Object o) { synchronized (mutex) {return list.indexOf(o);} } + @Pure public int lastIndexOf(Object o) { synchronized (mutex) {return list.lastIndexOf(o);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { synchronized (mutex) {return list.addAll(index, c);} } @@ -2848,10 +3016,13 @@ public List subList(int fromIndex, int toIndex) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { synchronized (mutex) {list.replaceAll(operator);} } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { synchronized (mutex) {list.sort(c);} } @@ -2988,20 +3159,29 @@ public boolean containsKey(@UnknownSignedness Object key) { public boolean containsValue(@UnknownSignedness Object value) { synchronized (mutex) {return m.containsValue(value);} } + @Pure public V get(Object key) { synchronized (mutex) {return m.get(key);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) public V put(K key, V value) { synchronized (mutex) {return m.put(key, value);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V remove(Object key) { synchronized (mutex) {return m.remove(key);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(Map map) { synchronized (mutex) {m.putAll(map);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { synchronized (mutex) {m.clear();} } @@ -3035,11 +3215,13 @@ public Collection values() { } } + @Pure public boolean equals(Object o) { if (this == o) return true; synchronized (mutex) {return m.equals(o);} } + @Pure public int hashCode() { synchronized (mutex) {return m.hashCode();} } @@ -3058,42 +3240,55 @@ public void forEach(BiConsumer action) { synchronized (mutex) {m.forEach(action);} } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { synchronized (mutex) {m.replaceAll(function);} } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) @Override public V putIfAbsent(K key, V value) { synchronized (mutex) {return m.putIfAbsent(key, value);} } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value) { synchronized (mutex) {return m.remove(key, value);} } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { synchronized (mutex) {return m.replace(key, oldValue, newValue);} } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { synchronized (mutex) {return m.replace(key, value);} } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { synchronized (mutex) {return m.computeIfAbsent(key, mappingFunction);} } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { synchronized (mutex) {return m.computeIfPresent(key, remappingFunction);} } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { synchronized (mutex) {return m.compute(key, remappingFunction);} } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { synchronized (mutex) {return m.merge(key, value, remappingFunction);} @@ -3176,6 +3371,7 @@ static class SynchronizedSortedMap sm = m; } + @Pure public Comparator comparator() { synchronized (mutex) {return sm.comparator();} } @@ -3303,8 +3499,12 @@ public Entry firstEntry() { synchronized (mutex) { return nm.firstEntry(); } } public Entry lastEntry() { synchronized (mutex) { return nm.lastEntry(); } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollFirstEntry() { synchronized (mutex) { return nm.pollFirstEntry(); } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollLastEntry() { synchronized (mutex) { return nm.pollLastEntry(); } } @@ -3492,20 +3692,27 @@ private String badElementMsg(Object o) { public boolean contains(@UnknownSignedness Object o) { return c.contains(o); } @SideEffectFree public @PolyNull @PolySigned Object[] toArray(Collections.CheckedCollection<@PolyNull @PolySigned E> this) { return c.toArray(); } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { return c.toArray(a); } public T[] toArray(IntFunction f) { return c.toArray(f); } public String toString() { return c.toString(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { return c.remove(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { c.clear(); } @Pure public boolean containsAll(Collection coll) { return c.containsAll(coll); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection coll) { return c.removeAll(coll); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection coll) { return c.retainAll(coll); } @@ -3520,7 +3727,10 @@ public Iterator iterator() { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasNext() { return it.hasNext(); } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(/*@NonEmpty Iterator this*/) { return it.next(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { it.remove(); } public void forEachRemaining(Consumer action) { it.forEachRemaining(action); @@ -3528,6 +3738,8 @@ public void forEachRemaining(Consumer action) { }; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmpty("this") public boolean add(E e) { return c.add(typeCheck(e)); } @@ -3562,6 +3774,8 @@ Collection checkedCopyOf(Collection coll) { return (Collection) Arrays.asList(a); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection coll) { // Doing things this way insulates us from concurrent changes // in the contents of coll and provides all-or-nothing @@ -3574,6 +3788,8 @@ public boolean addAll(Collection coll) { @Override public void forEach(Consumer action) {c.forEach(action);} @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { return c.removeIf(filter); } @@ -3634,6 +3850,7 @@ static class CheckedQueue this.queue = queue; } + @Pure public E element() {return queue.element();} @Pure public boolean equals(Object o) {return o == this || c.equals(o);} @@ -3641,8 +3858,14 @@ static class CheckedQueue public int hashCode() {return c.hashCode();} @Pure public E peek() {return queue.peek();} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll() {return queue.poll();} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove() {return queue.remove();} + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) {return queue.offer(typeCheck(e));} } @@ -3688,7 +3911,9 @@ static class CheckedSet extends CheckedCollection CheckedSet(Set s, Class elementType) { super(s, elementType); } + @Pure public boolean equals(Object o) { return o == this || c.equals(o); } + @Pure public int hashCode() { return c.hashCode(); } } @@ -3742,6 +3967,7 @@ static class CheckedSortedSet extends CheckedSet ss = s; } + @Pure public Comparator comparator() { return ss.comparator(); } public E first() { return ss.first(); } public E last() { return ss.last(); } @@ -3891,21 +4117,34 @@ static class CheckedList this.list = list; } + @Pure public boolean equals(Object o) { return o == this || list.equals(o); } + @Pure public int hashCode() { return list.hashCode(); } + @Pure public E get(int index) { return list.get(index); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { return list.remove(index); } + @Pure public int indexOf(Object o) { return list.indexOf(o); } + @Pure public int lastIndexOf(Object o) { return list.lastIndexOf(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { return list.set(index, typeCheck(element)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { list.add(index, typeCheck(element)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { return list.addAll(index, checkedCopyOf(c)); } @@ -3919,17 +4158,27 @@ public ListIterator listIterator(final int index) { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasNext() { return i.hasNext(); } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(/*@NonEmpty ListIterator this*/) { return i.next(); } + @Pure public boolean hasPrevious() { return i.hasPrevious(); } public E previous() { return i.previous(); } + @Pure public int nextIndex() { return i.nextIndex(); } + @Pure public int previousIndex() { return i.previousIndex(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { i.remove(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { i.set(typeCheck(e)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { i.add(typeCheck(e)); } @@ -3954,12 +4203,15 @@ public List subList(int fromIndex, int toIndex) { * already been replaced. */ @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { Objects.requireNonNull(operator); list.replaceAll(e -> typeCheck(operator.apply(e))); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { list.sort(c); } @@ -4087,21 +4339,32 @@ private String badValueMsg(Object value) { public boolean containsKey(@UnknownSignedness Object key) { return m.containsKey(key); } @Pure public boolean containsValue(@UnknownSignedness Object v) { return m.containsValue(v); } + @Pure public V get(Object key) { return m.get(key); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V remove(Object key) { return m.remove(key); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { m.clear(); } public Set keySet() { return m.keySet(); } public Collection values() { return m.values(); } + @Pure public boolean equals(Object o) { return o == this || m.equals(o); } + @Pure public int hashCode() { return m.hashCode(); } public String toString() { return m.toString(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) public V put(K key, V value) { typeCheck(key, value); return m.put(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @SuppressWarnings("unchecked") public void putAll(Map t) { // Satisfy the following goals: @@ -4139,10 +4402,13 @@ public void forEach(BiConsumer action) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { m.replaceAll(typeCheck(function)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) @Override public V putIfAbsent(K key, V value) { @@ -4151,23 +4417,30 @@ public V putIfAbsent(K key, V value) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value) { return m.remove(key, value); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { typeCheck(key, newValue); return m.replace(key, oldValue, newValue); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { typeCheck(key, value); return m.replace(key, value); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { Objects.requireNonNull(mappingFunction); @@ -4179,6 +4452,7 @@ public V replace(K key, V value) { } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { return m.computeIfPresent(key, typeCheck(remappingFunction)); @@ -4191,6 +4465,7 @@ public V replace(K key, V value) { } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -4223,14 +4498,22 @@ static class CheckedEntrySet implements Set> { @Pure @EnsuresNonEmptyIf(result = false, expression = "this") public boolean isEmpty() { return s.isEmpty(); } + @SideEffectFree public String toString() { return s.toString(); } + @Pure public int hashCode() { return s.hashCode(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { s.clear(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmpty("this") public boolean add(Map.Entry e) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection> coll) { throw new UnsupportedOperationException(); } @@ -4243,8 +4526,11 @@ public Iterator> iterator() { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasNext() { return i.hasNext(); } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { i.remove(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(/*@NonEmpty Iterator> this*/) { return checkedEntry(i.next(), valueType); } @@ -4320,6 +4606,8 @@ public boolean containsAll(Collection c) { return true; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { if (!(o instanceof Map.Entry)) return false; @@ -4327,9 +4615,13 @@ public boolean remove(@UnknownSignedness Object o) { <>((Map.Entry)o)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return batchRemove(c, false); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { return batchRemove(c, true); } @@ -4346,6 +4638,7 @@ private boolean batchRemove(Collection c, boolean complement) { return modified; } + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -4375,8 +4668,11 @@ private static class CheckedEntry implements Map.Entry { this.valueType = Objects.requireNonNull(valueType); } + @Pure public K getKey() { return e.getKey(); } + @Pure public V getValue() { return e.getValue(); } + @Pure public int hashCode() { return e.hashCode(); } public String toString() { return e.toString(); } @@ -4391,6 +4687,7 @@ private String badValueMsg(Object value) { " value into map with value type " + valueType; } + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -4463,6 +4760,7 @@ static class CheckedSortedMap extends CheckedMap sm = m; } + @Pure public Comparator comparator() { return sm.comparator(); } public K firstKey() { return sm.firstKey(); } public K lastKey() { return sm.lastKey(); } @@ -4542,6 +4840,7 @@ static class CheckedNavigableMap extends CheckedSortedMap nm = m; } + @Pure public Comparator comparator() { return nm.comparator(); } public K firstKey() { return nm.firstKey(); } public K lastKey() { return nm.lastKey(); } @@ -4596,6 +4895,8 @@ public Entry lastEntry() { : null; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollFirstEntry() { Entry entry = nm.pollFirstEntry(); return (null == entry) @@ -4603,6 +4904,8 @@ public Entry pollFirstEntry() { : new CheckedMap.CheckedEntrySet.CheckedEntry<>(entry, valueType); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollLastEntry() { Entry entry = nm.pollLastEntry(); return (null == entry) @@ -4699,7 +5002,10 @@ private static class EmptyIterator implements Iterator { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasNext() { return false; } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty EmptyIterator this) { throw new NoSuchElementException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove(@NonEmpty EmptyIterator this) { throw new IllegalStateException(); } @Override public void forEachRemaining(Consumer action) { @@ -4746,11 +5052,18 @@ private static class EmptyListIterator static final EmptyListIterator EMPTY_ITERATOR = new EmptyListIterator<>(); + @Pure public boolean hasPrevious() { return false; } public E previous() { throw new NoSuchElementException(); } + @Pure public int nextIndex() { return 0; } + @Pure public int previousIndex() { return -1; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { throw new IllegalStateException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { throw new UnsupportedOperationException(); } } @@ -4781,6 +5094,7 @@ private static class EmptyEnumeration implements Enumeration { static final EmptyEnumeration EMPTY_ENUMERATION = new EmptyEnumeration<>(); + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasMoreElements() { return false; } public E nextElement(@NonEmpty EmptyEnumeration this) { throw new NoSuchElementException(); } @@ -4849,7 +5163,6 @@ public void clear() {} @SideEffectFree public @PolyNull @PolySigned Object[] toArray(Collections.EmptySet<@PolyNull @PolySigned E> this) { return new Object[0]; } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { if (a.length > 0) a[0] = null; @@ -4877,6 +5190,7 @@ private Object readResolve() { } @Override + @Pure public int hashCode() { return 0; } @@ -4993,21 +5307,23 @@ public void clear() {} @SideEffectFree public @PolyNull @PolySigned Object[] toArray(Collections.EmptyList<@PolyNull @PolySigned E> this) { return new Object[0]; } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { if (a.length > 0) a[0] = null; return a; } + @Pure public E get(int index) { throw new IndexOutOfBoundsException("Index: "+index); } + @Pure public boolean equals(Object o) { return (o instanceof List) && ((List)o).isEmpty(); } + @Pure public int hashCode() { return 1; } @Override @@ -5138,16 +5454,19 @@ public void clear() {} public boolean containsKey(@UnknownSignedness Object key) {return false;} @Pure public boolean containsValue(@UnknownSignedness Object value) {return false;} + @Pure public V get(Object key) {return null;} public Set keySet() {return emptySet();} public Collection values() {return emptySet();} @SideEffectFree public Set> entrySet() {return emptySet();} + @Pure public boolean equals(Object o) { return (o instanceof Map) && ((Map)o).isEmpty(); } + @Pure public int hashCode() {return 0;} // Override default methods in Map @@ -5167,6 +5486,8 @@ public void replaceAll(BiFunction function) { Objects.requireNonNull(function); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) @Override public V putIfAbsent(K key, V value) { @@ -5174,39 +5495,49 @@ public V putIfAbsent(K key, V value) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { throw new UnsupportedOperationException(); @@ -5242,6 +5573,7 @@ public boolean hasNext() { return hasNext; } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(/*@NonEmpty Iterator this*/) { if (hasNext) { hasNext = false; @@ -5249,6 +5581,8 @@ public E next(/*@NonEmpty Iterator this*/) { } throw new NoSuchElementException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { throw new UnsupportedOperationException(); } @@ -5347,10 +5681,13 @@ public Spliterator spliterator() { return singletonSpliterator(element); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { throw new UnsupportedOperationException(); } @Override + @Pure public int hashCode() { return Objects.hashCode(element); } @@ -5398,6 +5735,7 @@ public Iterator iterator() { @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object obj) {return eq(obj, element);} + @Pure public E get(int index) { if (index != 0) throw new IndexOutOfBoundsException("Index: "+index+", Size: 1"); @@ -5410,10 +5748,13 @@ public void forEach(Consumer action) { action.accept(element); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { throw new UnsupportedOperationException(); } @@ -5426,6 +5767,7 @@ public Spliterator spliterator() { return singletonSpliterator(element); } @Override + @Pure public int hashCode() { return 31 + Objects.hashCode(element); } @@ -5476,6 +5818,7 @@ private static class SingletonMap public boolean containsKey(@UnknownSignedness Object key) {return eq(key, k);} @Pure public boolean containsValue(@UnknownSignedness Object value) {return eq(value, v);} + @Pure public V get(Object key) {return (eq(key, k) ? v : null);} private transient Set keySet; @@ -5515,10 +5858,13 @@ public void forEach(BiConsumer action) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresKeyFor(value={"#1"}, map={"this"}) @Override public V putIfAbsent(K key, V value) { @@ -5526,45 +5872,56 @@ public V putIfAbsent(K key, V value) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { throw new UnsupportedOperationException(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { throw new UnsupportedOperationException(); } @Override + @Pure public int hashCode() { return Objects.hashCode(k) ^ Objects.hashCode(v); } @@ -5626,14 +5983,17 @@ public boolean contains(@UnknownSignedness Object obj) { return n != 0 && eq(obj, element); } + @Pure public int indexOf(Object o) { return contains(o) ? 0 : -1; } + @Pure public int lastIndexOf(Object o) { return contains(o) ? n - 1 : -1; } + @Pure public E get(int index) { Objects.checkIndex(index, n); return element; @@ -5686,6 +6046,7 @@ public List subList(int fromIndex, int toIndex) { } @Override + @Pure public int hashCode() { if (n == 0) return 1; // hashCode of n repeating elements is 31^n + elementHash * Sum(31^k, k = 0..n-1) @@ -5705,6 +6066,7 @@ public int hashCode() { } @Override + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -5881,12 +6243,14 @@ public int compare(T t1, T t2) { return cmp.compare(t2, t1); } + @Pure public boolean equals(Object o) { return (o == this) || (o instanceof ReverseComparator2 that && cmp.equals(that.cmp)); } + @Pure public int hashCode() { return cmp.hashCode() ^ Integer.MIN_VALUE; } @@ -5916,6 +6280,7 @@ public static Enumeration enumeration(final Collection c) { return new Enumeration<>() { private final Iterator i = c.iterator(); + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasMoreElements() { return i.hasNext(); @@ -6179,10 +6544,11 @@ private static class SetFromMap extends AbstractSet public Iterator iterator() { return s.iterator(); } @SideEffectFree public @PolyNull @PolySigned Object[] toArray(Collections.SetFromMap<@PolyNull @PolySigned E> this) { return s.toArray(); } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { return s.toArray(a); } public String toString() { return s.toString(); } + @Pure public int hashCode() { return s.hashCode(); } + @Pure public boolean equals(Object o) { return o == this || s.equals(o); } @Pure public boolean containsAll(Collection c) {return s.containsAll(c);} @@ -6296,7 +6662,9 @@ private SequencedMap map() { public void addFirst(E e) { map().putFirst(e, Boolean.TRUE); } public void addLast(E e) { map().putLast(e, Boolean.TRUE); } + @Pure public E getFirst() { return nsee(map().firstEntry()); } + @Pure public E getLast() { return nsee(map().lastEntry()); } public E removeFirst() { return nsee(map().pollFirstEntry()); } public E removeLast() { return nsee(map().pollLastEntry()); } @@ -6349,6 +6717,7 @@ static class AsLIFOQueue extends AbstractQueue public E remove() { return q.removeFirst(); } @Pure public E peek() { return q.peekFirst(); } + @Pure public E element() { return q.getFirst(); } public void clear() { q.clear(); } @Pure @@ -6364,7 +6733,6 @@ static class AsLIFOQueue extends AbstractQueue public Iterator iterator() { return q.iterator(); } @SideEffectFree public @PolyNull @PolySigned Object[] toArray(Collections.AsLIFOQueue<@PolyNull @PolySigned E> this) { return q.toArray(); } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { return q.toArray(a); } public T[] toArray(IntFunction f) { return q.toArray(f); } public String toString() { return q.toString(); } diff --git a/src/java.base/share/classes/java/util/Deque.java b/src/java.base/share/classes/java/util/Deque.java index 84f03ced15c9f..486ce85966b30 100644 --- a/src/java.base/share/classes/java/util/Deque.java +++ b/src/java.base/share/classes/java/util/Deque.java @@ -49,6 +49,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A linear collection that supports element insertion and removal at @@ -237,6 +238,8 @@ public interface Deque extends Queue, SequencedCollection { * element prevents it from being added to this deque */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addFirst(@GuardSatisfied Deque this, E e); /** @@ -259,6 +262,8 @@ public interface Deque extends Queue, SequencedCollection { * element prevents it from being added to this deque */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addLast(@GuardSatisfied Deque this, E e); /** @@ -277,6 +282,8 @@ public interface Deque extends Queue, SequencedCollection { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offerFirst(E e); /** @@ -295,6 +302,8 @@ public interface Deque extends Queue, SequencedCollection { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offerLast(E e); /** @@ -305,6 +314,8 @@ public interface Deque extends Queue, SequencedCollection { * @return the head of this deque * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E removeFirst(@GuardSatisfied @NonEmpty @CanShrink Deque this); /** @@ -315,6 +326,8 @@ public interface Deque extends Queue, SequencedCollection { * @return the tail of this deque * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E removeLast(@GuardSatisfied @NonEmpty @CanShrink Deque this); /** @@ -323,6 +336,8 @@ public interface Deque extends Queue, SequencedCollection { * * @return the head of this deque, or {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E pollFirst(@GuardSatisfied @CanShrink Deque this); /** @@ -331,6 +346,8 @@ public interface Deque extends Queue, SequencedCollection { * * @return the tail of this deque, or {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E pollLast(@GuardSatisfied @CanShrink Deque this); /** @@ -342,6 +359,7 @@ public interface Deque extends Queue, SequencedCollection { * @return the head of this deque * @throws NoSuchElementException if this deque is empty */ + @Pure @EnsuresNonEmpty("this") E getFirst(@GuardSatisfied @NonEmpty @CanShrink Deque this); @@ -353,6 +371,7 @@ public interface Deque extends Queue, SequencedCollection { * @return the tail of this deque * @throws NoSuchElementException if this deque is empty */ + @Pure @EnsuresNonEmpty("this") E getLast(@GuardSatisfied @NonEmpty @CanShrink Deque this); @@ -362,6 +381,9 @@ public interface Deque extends Queue, SequencedCollection { * * @return the head of this deque, or {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure @Nullable E peekFirst(); /** @@ -370,6 +392,9 @@ public interface Deque extends Queue, SequencedCollection { * * @return the tail of this deque, or {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure @Nullable E peekLast(); /** @@ -389,6 +414,8 @@ public interface Deque extends Queue, SequencedCollection { * deque does not permit null elements * ({@linkplain Collection##optional-restrictions optional}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean removeFirstOccurrence(@GuardSatisfied @CanShrink Deque this, Object o); /** @@ -408,6 +435,8 @@ public interface Deque extends Queue, SequencedCollection { * deque does not permit null elements * ({@linkplain Collection##optional-restrictions optional}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean removeLastOccurrence(@GuardSatisfied @CanShrink Deque this, Object o); // *** Queue methods *** @@ -435,6 +464,8 @@ public interface Deque extends Queue, SequencedCollection { * element prevents it from being added to this deque */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean add(@GuardSatisfied Deque this, E e); /** @@ -458,6 +489,8 @@ public interface Deque extends Queue, SequencedCollection { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offer(E e); /** @@ -471,6 +504,8 @@ public interface Deque extends Queue, SequencedCollection { * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E remove(@GuardSatisfied @NonEmpty @CanShrink Deque this); /** @@ -483,6 +518,8 @@ public interface Deque extends Queue, SequencedCollection { * @return the first element of this deque, or {@code null} if * this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E poll(@GuardSatisfied @CanShrink Deque this); /** @@ -496,6 +533,9 @@ public interface Deque extends Queue, SequencedCollection { * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure E element(@GuardSatisfied @NonEmpty Deque this); /** @@ -508,6 +548,9 @@ public interface Deque extends Queue, SequencedCollection { * @return the head of the queue represented by this deque, or * {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure @Nullable E peek(); /** @@ -534,6 +577,8 @@ public interface Deque extends Queue, SequencedCollection { * @throws IllegalArgumentException if some property of an element of the * specified collection prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean addAll(Collection c); // *** Stack methods *** @@ -556,6 +601,8 @@ public interface Deque extends Queue, SequencedCollection { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void push(@GuardSatisfied Deque this, E e); /** @@ -568,6 +615,8 @@ public interface Deque extends Queue, SequencedCollection { * of the stack represented by this deque) * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E pop(@GuardSatisfied @NonEmpty @CanShrink Deque this); @@ -592,6 +641,8 @@ public interface Deque extends Queue, SequencedCollection { * deque does not permit null elements * ({@linkplain Collection##optional-restrictions optional}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean remove(@GuardSatisfied @CanShrink Deque this, @UnknownSignedness Object o); /** @@ -637,6 +688,8 @@ public interface Deque extends Queue, SequencedCollection { * @return an iterator over the elements in this deque in reverse * sequence */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") Iterator descendingIterator(); /** @@ -649,6 +702,8 @@ public interface Deque extends Queue, SequencedCollection { * @return a reverse-ordered view of this collection, as a {@code Deque} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default Deque reversed() { return ReverseOrderDequeView.of(this); } diff --git a/src/java.base/share/classes/java/util/EnumMap.java b/src/java.base/share/classes/java/util/EnumMap.java index 880386c3749b9..3ea7ee73194ab 100644 --- a/src/java.base/share/classes/java/util/EnumMap.java +++ b/src/java.base/share/classes/java/util/EnumMap.java @@ -41,6 +41,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import jdk.internal.access.SharedSecrets; @@ -127,10 +128,12 @@ public class EnumMap, V> extends AbstractMap * Distinguished non-null value for representing null values. */ private static final Object NULL = new Object() { + @Pure public int hashCode() { return 0; } + @SideEffectFree public String toString() { return "java.util.EnumMap.NULL"; } @@ -238,8 +241,8 @@ public boolean containsValue(@GuardSatisfied @Nullable @UnknownSignedness Object * @return {@code true} if this map contains a mapping for the specified * key */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public boolean containsKey(@GuardSatisfied @UnknownSignedness Object key) { return isValidKey(key) && vals[((Enum)key).ordinal()] != null; } @@ -264,6 +267,7 @@ private boolean containsMapping(@GuardSatisfied @UnknownSignedness Object key, @ * The {@link #containsKey containsKey} operation may be used to * distinguish these two cases. */ + @Pure public @Nullable V get(@UnknownSignedness @Nullable Object key) { return (isValidKey(key) ? unmaskNull(vals[((Enum)key).ordinal()]) : null); @@ -286,6 +290,8 @@ private boolean containsMapping(@GuardSatisfied @UnknownSignedness Object key, @ * @throws NullPointerException if the specified key is null */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(K key, V value) { typeCheck(key); @@ -306,6 +312,8 @@ private boolean containsMapping(@GuardSatisfied @UnknownSignedness Object key, @ * return can also indicate that the map previously associated * {@code null} with the specified key.) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V remove(@GuardSatisfied @UnknownSignedness Object key) { if (!isValidKey(key)) return null; @@ -358,6 +366,8 @@ private boolean isValidKey(@GuardSatisfied @UnknownSignedness Object key) { "and vals are private class members for EnumMap and are absent in AbstractMap."}) @SuppressWarnings({"nullness:contracts.precondition.override.invalid"}) @RequiresNonNull({"keyUniverse", "vals"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(@UnknownInitialization EnumMap this, Map m) { if (m instanceof EnumMap em) { if (em.keyType != keyType) { @@ -382,6 +392,8 @@ public void putAll(@UnknownInitialization EnumMap this, Map keySet() { Set ks = keySet; if (ks == null) { @@ -428,11 +441,15 @@ public Iterator iterator() { public boolean contains(@Nullable @UnknownSignedness Object o) { return containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { int oldSize = size; EnumMap.this.remove(o); return size != oldSize; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { EnumMap.this.clear(); } @@ -448,6 +465,7 @@ public void clear() { * * @return a collection view of the values contained in this map */ + @SideEffectFree public Collection values() { Collection vs = values; if (vs == null) { @@ -471,6 +489,8 @@ public Iterator iterator() { public boolean contains(@Nullable @UnknownSignedness Object o) { return containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { o = maskNull(o); @@ -483,6 +503,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { } return false; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { EnumMap.this.clear(); } @@ -518,6 +540,8 @@ public boolean contains(@Nullable @UnknownSignedness Object o) { return o instanceof Map.Entry entry && containsMapping(entry.getKey(), entry.getValue()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { return o instanceof Map.Entry entry && removeMapping(entry.getKey(), entry.getValue()); @@ -526,6 +550,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { public @NonNegative int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { EnumMap.this.clear(); } @@ -578,6 +604,8 @@ public boolean hasNext() { return index != vals.length; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { checkLastReturnedIndex(); @@ -595,6 +623,8 @@ private void checkLastReturnedIndex() { } private class KeyIterator extends EnumMapIterator { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty KeyIterator this) { if (!hasNext()) throw new NoSuchElementException(); @@ -607,6 +637,8 @@ private class ValueIterator extends EnumMapIterator { @CFComment({"nullness: Value returned by unmaskNull", "will be of type V (not @Nullable V) for mapped value"}) @SuppressWarnings({"nullness:return"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(@NonEmpty ValueIterator this) { if (!hasNext()) throw new NoSuchElementException(); @@ -618,6 +650,8 @@ public V next(@NonEmpty ValueIterator this) { private class EntryIterator extends EnumMapIterator> { private Entry lastReturnedEntry; + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty EntryIterator this) { if (!hasNext()) throw new NoSuchElementException(); @@ -625,6 +659,8 @@ public Map.Entry next(@NonEmpty EntryIterator this) { return lastReturnedEntry; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { lastReturnedIndex = ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index); @@ -640,6 +676,7 @@ private Entry(int index) { this.index = index; } + @Pure public K getKey() { checkIndexForEntryUse(); return keyUniverse[index]; @@ -648,6 +685,7 @@ public K getKey() { @CFComment({"nullness: Value returned by unmaskNull", "will be of type V (not @Nullable V) for mapped value"}) @SuppressWarnings("nullness:return") + @Pure public V getValue() { checkIndexForEntryUse(); return unmaskNull(vals[index]); @@ -663,6 +701,7 @@ public V setValue(V value) { return oldValue; } + @Pure public boolean equals(Object o) { if (index < 0) return o == this; @@ -677,6 +716,7 @@ public boolean equals(Object o) { (ourValue != null && ourValue.equals(hisValue)))); } + @Pure public int hashCode() { if (index < 0) return super.hashCode(); @@ -684,6 +724,7 @@ public int hashCode() { return entryHashCode(index); } + @SideEffectFree public String toString() { if (index < 0) return super.toString(); @@ -710,6 +751,7 @@ private void checkIndexForEntryUse() { * @param o the object to be compared for equality with this map * @return {@code true} if the specified object is equal to this map */ + @Pure public boolean equals(@Nullable Object o) { if (this == o) return true; @@ -738,6 +780,7 @@ public boolean equals(@Nullable Object o) { return true; } + @Pure private boolean equals(EnumMap em) { if (em.size != size) return false; @@ -760,6 +803,7 @@ private boolean equals(EnumMap em) { * Returns the hash code value for this map. The hash code of a map is * defined to be the sum of the hash codes of each entry in the map. */ + @Pure public int hashCode() { int h = 0; @@ -785,6 +829,7 @@ private int entryHashCode(int index) { * @return a shallow copy of this enum map */ @SuppressWarnings("unchecked") + @SideEffectFree public EnumMap clone() { EnumMap result = null; try { diff --git a/src/java.base/share/classes/java/util/EnumSet.java b/src/java.base/share/classes/java/util/EnumSet.java index 9a64d87cbe7f9..c85dc5fe67087 100644 --- a/src/java.base/share/classes/java/util/EnumSet.java +++ b/src/java.base/share/classes/java/util/EnumSet.java @@ -26,7 +26,9 @@ package java.util; import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import jdk.internal.access.SharedSecrets; @@ -144,6 +146,8 @@ public static > EnumSet allOf(Class elementType) { * Adds all of the elements from the appropriate enum type to this enum * set, which is empty prior to the call. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") abstract void addAll(); /** @@ -155,6 +159,7 @@ public static > EnumSet allOf(Class elementType) { * @return A copy of the specified enum set. * @throws NullPointerException if {@code s} is null */ + @SideEffectFree public static > EnumSet copyOf(EnumSet s) { return s.clone(); } @@ -173,6 +178,7 @@ public static > EnumSet copyOf(EnumSet s) { * {@code EnumSet} instance and contains no elements * @throws NullPointerException if {@code c} is null */ + @SideEffectFree public static > EnumSet copyOf(Collection c) { if (c instanceof EnumSet) { return ((EnumSet)c).clone(); @@ -218,6 +224,7 @@ public static > EnumSet complementOf(EnumSet s) { * @throws NullPointerException if {@code e} is null * @return an enum set initially containing the specified element */ + @SideEffectFree public static > EnumSet of(E e) { EnumSet result = noneOf(e.getDeclaringClass()); result.add(e); @@ -239,6 +246,7 @@ public static > EnumSet of(E e) { * @throws NullPointerException if any parameters are null * @return an enum set initially containing the specified elements */ + @SideEffectFree public static > EnumSet of(E e1, E e2) { EnumSet result = noneOf(e1.getDeclaringClass()); result.add(e1); @@ -262,6 +270,7 @@ public static > EnumSet of(E e1, E e2) { * @throws NullPointerException if any parameters are null * @return an enum set initially containing the specified elements */ + @SideEffectFree public static > EnumSet of(E e1, E e2, E e3) { EnumSet result = noneOf(e1.getDeclaringClass()); result.add(e1); @@ -287,6 +296,7 @@ public static > EnumSet of(E e1, E e2, E e3) { * @throws NullPointerException if any parameters are null * @return an enum set initially containing the specified elements */ + @SideEffectFree public static > EnumSet of(E e1, E e2, E e3, E e4) { EnumSet result = noneOf(e1.getDeclaringClass()); result.add(e1); @@ -341,6 +351,7 @@ public static > EnumSet of(E e1, E e2, E e3, E e4, * @return an enum set initially containing the specified elements */ @SafeVarargs + @SideEffectFree public static > EnumSet of(E first, E... rest) { EnumSet result = noneOf(first.getDeclaringClass()); result.add(first); @@ -375,6 +386,8 @@ public static > EnumSet range(E from, E to) { * Adds the specified range to this enum set, which is empty prior * to the call. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") abstract void addRange(E from, E to); /** @@ -383,6 +396,9 @@ public static > EnumSet range(E from, E to) { * @return a copy of this set */ @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @SideEffectFree public EnumSet clone() { try { return (EnumSet) super.clone(); @@ -394,6 +410,8 @@ public EnumSet clone() { /** * Complements the contents of this enum set. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") abstract void complement(); /** diff --git a/src/java.base/share/classes/java/util/Enumeration.java b/src/java.base/share/classes/java/util/Enumeration.java index 06c2eeaee5676..3ae284e8d527c 100644 --- a/src/java.base/share/classes/java/util/Enumeration.java +++ b/src/java.base/share/classes/java/util/Enumeration.java @@ -31,6 +31,7 @@ import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * An object that implements the Enumeration interface generates a @@ -134,6 +135,7 @@ default Iterator asIterator() { return hasMoreElements(); } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Override public E next(/*@NonEmpty Iterator this*/) { return nextElement(); } diff --git a/src/java.base/share/classes/java/util/HashMap.java b/src/java.base/share/classes/java/util/HashMap.java index 2eaa069829a8a..5ea19f8739351 100644 --- a/src/java.base/share/classes/java/util/HashMap.java +++ b/src/java.base/share/classes/java/util/HashMap.java @@ -42,6 +42,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.io.InvalidObjectException; @@ -310,20 +311,27 @@ static class Node implements Map.Entry { this.next = next; } + @Pure public final K getKey() { return key; } + @Pure public final V getValue() { return value; } + @SideEffectFree public final String toString() { return key + "=" + value; } + @Pure public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } + @Pure public final boolean equals(Object o) { if (o == this) return true; @@ -621,8 +629,8 @@ final Node getNode(@Nullable Object key) { * @return {@code true} if this map contains a mapping for the specified * key. */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public boolean containsKey(@GuardSatisfied HashMap this, @GuardSatisfied @Nullable @UnknownSignedness Object key) { return getNode(key) != null; } @@ -640,6 +648,8 @@ public boolean containsKey(@GuardSatisfied HashMap this, @GuardSatisfied @ * previously associated {@code null} with {@code key}.) */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(@GuardSatisfied HashMap this, K key, V value) { return putVal(hash(key), key, value, false, true); } @@ -814,6 +824,8 @@ else if ((e = tab[index = (n - 1) & hash]) != null) { * @param m mappings to be stored in this map * @throws NullPointerException if the specified map is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(@GuardSatisfied HashMap this, Map m) { putMapEntries(m, true); } @@ -827,6 +839,8 @@ public void putAll(@GuardSatisfied HashMap this, Map this, @GuardSatisfied @Nullable @UnknownSignedness Object key) { Node e; return (e = removeNode(hash(key), key, null, false, true)) == null ? @@ -888,6 +902,8 @@ else if (node == p) * Removes all of the mappings from this map. * The map will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied HashMap this) { Node[] tab; modCount++; @@ -1017,12 +1033,16 @@ T[] valuesToArray(T[] a) { final class KeySet extends AbstractSet { @Pure public final @NonNegative int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { HashMap.this.clear(); } @SideEffectFree public final Iterator iterator() { return new KeyIterator(); } @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public final boolean contains(@Nullable @UnknownSignedness Object o) { return containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean remove(@Nullable @UnknownSignedness Object key) { return removeNode(hash(key), key, null, false, true) != null; } @@ -1031,6 +1051,7 @@ public final Spliterator spliterator() { return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0); } + @SideEffectFree public Object[] toArray() { return keysToArray(new Object[size]); } @@ -1039,6 +1060,7 @@ public Object[] toArray() { return keysToArray(prepareArray(a)); } + @DoesNotUnrefineReceiver("modifiability") public final void forEach(Consumer action) { Node[] tab; if (action == null) @@ -1083,6 +1105,8 @@ public Collection values(@GuardSatisfied HashMap this) { final class Values extends AbstractCollection { @Pure public final @NonNegative int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { HashMap.this.clear(); } @SideEffectFree public final Iterator iterator() { return new ValueIterator(); } @@ -1094,6 +1118,7 @@ public final Spliterator spliterator() { return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0); } + @SideEffectFree public Object[] toArray() { return valuesToArray(new Object[size]); } @@ -1143,6 +1168,8 @@ public final void forEach(Consumer action) { final class EntrySet extends AbstractSet> { @Pure public final @NonNegative int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { HashMap.this.clear(); } @SideEffectFree public final Iterator> iterator() { @@ -1157,6 +1184,8 @@ public final boolean contains(@Nullable @UnknownSignedness Object o) { Node candidate = getNode(key); return candidate != null && candidate.equals(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean remove(@Nullable @UnknownSignedness Object o) { if (o instanceof Map.Entry e) { Object key = e.getKey(); @@ -1169,6 +1198,7 @@ public final boolean remove(@Nullable @UnknownSignedness Object o) { public final Spliterator> spliterator() { return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0); } + @DoesNotUnrefineReceiver("modifiability") public final void forEach(Consumer> action) { Node[] tab; if (action == null) @@ -1196,16 +1226,22 @@ public V getOrDefault(@GuardSatisfied @Nullable @UnknownSignedness Object key, V @EnsuresKeyFor(value={"#1"}, map={"this"}) @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V putIfAbsent(K key, V value) { return putVal(hash(key), key, value, true, true); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object key, @GuardSatisfied @Nullable @UnknownSignedness Object value) { return removeNode(hash(key), key, value, true, true) != null; } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { Node e; V v; if ((e = getNode(key)) != null && @@ -1218,6 +1254,8 @@ public boolean replace(K key, V oldValue, V newValue) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V replace(K key, V value) { Node e; if ((e = getNode(key)) != null) { @@ -1240,6 +1278,7 @@ public boolean replace(K key, V oldValue, V newValue) { * mapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { if (mappingFunction == null) @@ -1306,6 +1345,7 @@ else if (t != null) * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { if (remappingFunction == null) @@ -1340,6 +1380,7 @@ else if (t != null) * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { if (remappingFunction == null) @@ -1405,6 +1446,7 @@ else if (v != null) { * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { if (value == null || remappingFunction == null) @@ -1466,6 +1508,7 @@ else if (v != null) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void forEach(BiConsumer action) { Node[] tab; if (action == null) @@ -1482,6 +1525,7 @@ public void forEach(BiConsumer action) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { Node[] tab; if (function == null) @@ -1663,6 +1707,8 @@ final Node nextNode(@NonEmpty HashIterator this) { return e; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void remove() { Node p = current; if (p == null) @@ -1677,16 +1723,22 @@ public final void remove() { final class KeyIterator extends HashIterator implements Iterator { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final K next(@NonEmpty KeyIterator this) { return nextNode().key; } } final class ValueIterator extends HashIterator implements Iterator { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V next(@NonEmpty ValueIterator this) { return nextNode().value; } } final class EntryIterator extends HashIterator implements Iterator> { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final Map.Entry next(@NonEmpty EntryIterator this) { return nextNode(); } } diff --git a/src/java.base/share/classes/java/util/HashSet.java b/src/java.base/share/classes/java/util/HashSet.java index dbff5a4231792..756747e1afd26 100644 --- a/src/java.base/share/classes/java/util/HashSet.java +++ b/src/java.base/share/classes/java/util/HashSet.java @@ -39,6 +39,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.InvalidObjectException; import jdk.internal.access.SharedSecrets; @@ -249,6 +250,8 @@ public boolean contains(@GuardSatisfied HashSet this, @GuardSatisfied @Nullab */ // @SideEffectsOnly("this") @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied HashSet this, E e) { return map.put(e, PRESENT)==null; } @@ -266,6 +269,7 @@ public boolean add(@GuardSatisfied HashSet this, E e) { * @return {@code true} if the set contained the specified element */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied HashSet this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { return map.remove(o)==PRESENT; } @@ -275,6 +279,7 @@ public boolean remove(@GuardSatisfied HashSet this, @GuardSatisfied @Nullable * The set will be empty after this call returns. */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied HashSet this) { map.clear(); } @@ -394,11 +399,13 @@ private void readObject(java.io.ObjectInputStream s) * @return a {@code Spliterator} over the elements in this set * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return new HashMap.KeySpliterator<>(map, 0, -1, 0, 0); } @Override + @SideEffectFree public Object[] toArray() { return map.keysToArray(new Object[map.size()]); } diff --git a/src/java.base/share/classes/java/util/Hashtable.java b/src/java.base/share/classes/java/util/Hashtable.java index c437612b2445f..402eece52ad7d 100644 --- a/src/java.base/share/classes/java/util/Hashtable.java +++ b/src/java.base/share/classes/java/util/Hashtable.java @@ -42,6 +42,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.*; import java.util.function.BiConsumer; @@ -293,6 +294,7 @@ public synchronized boolean isEmpty(@GuardSatisfied Hashtable this) { * @see #keySet() * @see Map */ + @SideEffectFree public synchronized Enumeration<@KeyFor({"this"}) K> keys() { return this.getEnumeration(KEYS); } @@ -309,6 +311,7 @@ public synchronized boolean isEmpty(@GuardSatisfied Hashtable this) { * @see #values() * @see Map */ + @SideEffectFree public synchronized Enumeration elements() { return this.getEnumeration(VALUES); } @@ -374,8 +377,8 @@ public boolean containsValue(@GuardSatisfied Hashtable this, @GuardSatisfi * @throws NullPointerException if the key is {@code null} * @see #contains(Object) */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public synchronized boolean containsKey(@GuardSatisfied Hashtable this, @GuardSatisfied @UnknownSignedness Object key) { Entry tab[] = table; int hash = key.hashCode(); @@ -403,8 +406,8 @@ public synchronized boolean containsKey(@GuardSatisfied Hashtable this, @G * @throws NullPointerException if the specified key is null * @see #put(Object, Object) */ - @Pure @SuppressWarnings("unchecked") + @Pure public synchronized @Nullable V get(@GuardSatisfied Hashtable this, @UnknownSignedness @GuardSatisfied Object key) { Entry tab[] = table; int hash = key.hashCode(); @@ -500,6 +503,8 @@ private void addEntry(int hash, K key, V value, int index) { * @see #get(Object) */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized @Nullable V put(@GuardSatisfied Hashtable this, K key, V value) { // Make sure the value is not null if (value == null) { @@ -533,6 +538,8 @@ private void addEntry(int hash, K key, V value, int index) { * or {@code null} if the key did not have a mapping * @throws NullPointerException if the key is {@code null} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized @Nullable V remove(@GuardSatisfied Hashtable this, @GuardSatisfied @UnknownSignedness Object key) { Entry tab[] = table; int hash = key.hashCode(); @@ -565,6 +572,8 @@ private void addEntry(int hash, K key, V value, int index) { * @throws NullPointerException if the specified map is null * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void putAll(@GuardSatisfied Hashtable this, Map t) { for (Map.Entry e : t.entrySet()) put(e.getKey(), e.getValue()); @@ -573,6 +582,8 @@ public synchronized void putAll(@GuardSatisfied Hashtable this, Map this) { Entry tab[] = table; for (int index = tab.length; --index >= 0; ) @@ -623,6 +634,7 @@ final Hashtable cloneHashtable() { * * @return a string representation of this hashtable */ + @SideEffectFree public synchronized String toString(@GuardSatisfied Hashtable this) { int max = size() - 1; if (max == -1) @@ -710,9 +722,13 @@ public Iterator iterator() { public boolean contains(@UnknownSignedness Object o) { return containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { return Hashtable.this.remove(o) != null; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { Hashtable.this.clear(); } @@ -748,6 +764,8 @@ public Iterator> iterator() { } @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(Map.Entry o) { return super.add(o); } @@ -768,6 +786,8 @@ public boolean contains(@UnknownSignedness Object o) { return false; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { if (!(o instanceof Map.Entry entry)) return false; @@ -799,6 +819,8 @@ public boolean remove(@UnknownSignedness Object o) { return count; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { Hashtable.this.clear(); } @@ -841,6 +863,8 @@ public Iterator iterator() { public boolean contains(@UnknownSignedness Object o) { return containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { Hashtable.this.clear(); } @@ -932,6 +956,7 @@ public synchronized V getOrDefault(@GuardSatisfied @UnknownSignedness Object key @SuppressWarnings("unchecked") @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void forEach(BiConsumer action) { Objects.requireNonNull(action); // explicit check required in case // table is empty. @@ -952,6 +977,7 @@ public synchronized void forEach(BiConsumer action) { @SuppressWarnings("unchecked") @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void replaceAll(BiFunction function) { Objects.requireNonNull(function); // explicit check required in case // table is empty. @@ -973,6 +999,8 @@ public synchronized void replaceAll(BiFunction tab[] = table; @@ -1072,6 +1106,7 @@ public synchronized V replace(K key, V value) { * mapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull V computeIfAbsent(K key, Function mappingFunction) { Objects.requireNonNull(mappingFunction); @@ -1108,6 +1143,7 @@ public synchronized V replace(K key, V value) { * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -1150,6 +1186,7 @@ public synchronized V replace(K key, V value) { * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull V compute(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -1201,6 +1238,7 @@ public synchronized V replace(K key, V value) { * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -1437,14 +1475,18 @@ protected Object clone() { // Map.Entry Ops + @Pure public K getKey() { return key; } + @Pure public V getValue() { return value; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V setValue(V value) { if (value == null) throw new NullPointerException(); @@ -1454,6 +1496,7 @@ public V setValue(V value) { return oldValue; } + @Pure public boolean equals(Object o) { if (!(o instanceof Map.Entry e)) return false; @@ -1462,10 +1505,12 @@ public boolean equals(Object o) { (value==null ? e.getValue()==null : value.equals(e.getValue())); } + @Pure public int hashCode() { return hash ^ Objects.hashCode(value); } + @SideEffectFree public String toString() { return key.toString()+"="+value.toString(); } @@ -1508,6 +1553,7 @@ private class Enumerator implements Enumeration, Iterator { this.iterator = iterator; } + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasMoreElements() { Entry e = entry; @@ -1523,6 +1569,8 @@ public boolean hasMoreElements() { } @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public T nextElement(@NonEmpty Enumerator this) { Entry et = entry; int i = index; @@ -1549,12 +1597,15 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public T next(@NonEmpty Enumerator this) { if (Hashtable.this.modCount != expectedModCount) throw new ConcurrentModificationException(); return nextElement(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (!iterator) throw new UnsupportedOperationException(); diff --git a/src/java.base/share/classes/java/util/IdentityHashMap.java b/src/java.base/share/classes/java/util/IdentityHashMap.java index 6e653f8d8dfdc..12b276b27f0d7 100644 --- a/src/java.base/share/classes/java/util/IdentityHashMap.java +++ b/src/java.base/share/classes/java/util/IdentityHashMap.java @@ -40,6 +40,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -353,8 +354,8 @@ private static int nextKeyIndex(int i, int len) { * * @see #put(Object, Object) */ - @Pure @SuppressWarnings("unchecked") + @Pure public @Nullable V get(@GuardSatisfied IdentityHashMap this, @UnknownSignedness @GuardSatisfied @Nullable Object key) { Object k = maskNull(key); Object[] tab = table; @@ -380,8 +381,8 @@ private static int nextKeyIndex(int i, int len) { * in this map * @see #containsValue(Object) */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public boolean containsKey(@GuardSatisfied IdentityHashMap this, @GuardSatisfied @Nullable @UnknownSignedness Object key) { Object k = maskNull(key); Object[] tab = table; @@ -457,6 +458,8 @@ private boolean containsMapping(@UnknownSignedness @GuardSatisfied @Nullable Obj * @see #containsKey(Object) */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(@GuardSatisfied IdentityHashMap this, K key, V value) { final Object k = maskNull(key); @@ -538,6 +541,8 @@ private boolean resize(int newCapacity) { * @param m mappings to be stored in this map * @throws NullPointerException if the specified map is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(@GuardSatisfied IdentityHashMap this, Map m) { int n = m.size(); if (n == 0) @@ -560,6 +565,8 @@ public void putAll(@GuardSatisfied IdentityHashMap this, Map this, @GuardSatisfied @Nullable @UnknownSignedness Object key) { Object k = maskNull(key); Object[] tab = table; @@ -656,6 +663,8 @@ private void closeDeletion(int d) { * Removes all of the mappings from this map. * The map will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied IdentityHashMap this) { modCount++; Object[] tab = table; @@ -795,6 +804,8 @@ protected int nextIndex(@NonEmpty IdentityHashMapIterator this) { return lastReturnedIndex; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastReturnedIndex == -1) throw new IllegalStateException(); @@ -875,6 +886,8 @@ public void remove() { private class KeyIterator extends IdentityHashMapIterator { @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty KeyIterator this) { return (K) unmaskNull(traversalTable[nextIndex()]); } @@ -882,6 +895,8 @@ public K next(@NonEmpty KeyIterator this) { private class ValueIterator extends IdentityHashMapIterator { @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(@NonEmpty ValueIterator this) { return (V) traversalTable[nextIndex() + 1]; } @@ -892,11 +907,15 @@ private class EntryIterator { private Entry lastReturnedEntry; + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty EntryIterator this) { lastReturnedEntry = new Entry(nextIndex()); return lastReturnedEntry; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { lastReturnedIndex = ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index); @@ -913,18 +932,22 @@ private Entry(int index) { } @SuppressWarnings("unchecked") + @Pure public K getKey() { checkIndexForEntryUse(); return (K) unmaskNull(traversalTable[index]); } @SuppressWarnings("unchecked") + @Pure public V getValue() { checkIndexForEntryUse(); return (V) traversalTable[index+1]; } @SuppressWarnings("unchecked") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V setValue(V value) { checkIndexForEntryUse(); V oldValue = (V) traversalTable[index+1]; @@ -935,6 +958,7 @@ public V setValue(V value) { return oldValue; } + @Pure public boolean equals(@Nullable Object o) { if (index < 0) return super.equals(o); @@ -944,6 +968,7 @@ public boolean equals(@Nullable Object o) { && e.getValue() == traversalTable[index+1]; } + @Pure public int hashCode() { if (lastReturnedIndex < 0) return super.hashCode(); @@ -952,6 +977,7 @@ public int hashCode() { System.identityHashCode(traversalTable[index+1])); } + @SideEffectFree public String toString() { if (index < 0) return super.toString(); @@ -1038,6 +1064,8 @@ public Iterator iterator() { public boolean contains(@Nullable @UnknownSignedness Object o) { return containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { int oldSize = size; IdentityHashMap.this.remove(o); @@ -1048,6 +1076,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { * the former contains an optimization that results in incorrect * behavior when c is a smaller "normal" (non-identity-based) Set. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { Objects.requireNonNull(c); boolean modified = false; @@ -1059,9 +1089,12 @@ public boolean removeAll(Collection c) { } return modified; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { IdentityHashMap.this.clear(); } + @Pure public int hashCode() { int result = 0; for (K key : this) @@ -1073,7 +1106,6 @@ public Object[] toArray() { return toArray(new Object[0]); } @SuppressWarnings("unchecked") - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { int expectedModCount = modCount; int size = size(); @@ -1152,6 +1184,8 @@ public Iterator iterator() { public boolean contains(@Nullable @UnknownSignedness Object o) { return containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { for (Iterator i = iterator(); i.hasNext(); ) { if (i.next() == o) { @@ -1161,6 +1195,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { } return false; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { IdentityHashMap.this.clear(); } @@ -1169,7 +1205,6 @@ public Object[] toArray() { return toArray(new Object[0]); } @SuppressWarnings("unchecked") - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { int expectedModCount = modCount; int size = size(); @@ -1263,6 +1298,8 @@ public boolean contains(@Nullable @UnknownSignedness Object o) { return o instanceof Entry entry && containsMapping(entry.getKey(), entry.getValue()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { return o instanceof Entry entry && removeMapping(entry.getKey(), entry.getValue()); @@ -1271,6 +1308,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { public @NonNegative int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { IdentityHashMap.this.clear(); } @@ -1279,6 +1318,8 @@ public void clear() { * the former contains an optimization that results in incorrect * behavior when c is a smaller "normal" (non-identity-based) Set. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { Objects.requireNonNull(c); boolean modified = false; @@ -1297,7 +1338,6 @@ public Object[] toArray() { } @SuppressWarnings("unchecked") - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { int expectedModCount = modCount; int size = size(); @@ -1439,6 +1479,7 @@ public void forEach(BiConsumer action) { @SuppressWarnings("unchecked") @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { Objects.requireNonNull(function); int expectedModCount = modCount; @@ -1466,6 +1507,8 @@ public void replaceAll(BiFunction function) { * {@code false}. */ @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(Object key, Object value) { return removeMapping(key, value); } @@ -1480,6 +1523,8 @@ public boolean remove(Object key, Object value) { * otherwise it returns {@code false}. */ @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { Object k = maskNull(key); Object[] tab = table; diff --git a/src/java.base/share/classes/java/util/ImmutableCollections.java b/src/java.base/share/classes/java/util/ImmutableCollections.java index 4eb9e17bd8ca4..102db2642c3d0 100644 --- a/src/java.base/share/classes/java/util/ImmutableCollections.java +++ b/src/java.base/share/classes/java/util/ImmutableCollections.java @@ -35,6 +35,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.io.InvalidObjectException; @@ -312,6 +313,7 @@ public ListIterator listIterator(final int index) { } @Override + @Pure public boolean equals(Object o) { if (o == this) { return true; @@ -331,6 +333,7 @@ public boolean equals(Object o) { } @Override + @Pure public int hashCode() { int hash = 1; for (int i = 0, s = size(); i < s; i++) { @@ -390,6 +393,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty ListItr this) { try { int i = cursor; @@ -405,6 +409,7 @@ public void remove() { throw uoe(); } + @Pure public boolean hasPrevious() { if (!isListIterator) { throw uoe(); @@ -426,6 +431,7 @@ public E previous() { } } + @Pure public int nextIndex() { if (!isListIterator) { throw uoe(); @@ -433,6 +439,7 @@ public int nextIndex() { return cursor; } + @Pure public int previousIndex() { if (!isListIterator) { throw uoe(); @@ -483,6 +490,7 @@ static SubList fromList(AbstractImmutableList list, int fromIndex, int return new SubList<>(list, fromIndex, toIndex - fromIndex); } + @Pure public E get(int index) { Objects.checkIndex(index, size); return root.get(offset + index); @@ -518,6 +526,7 @@ private boolean allowNulls() { } @Override + @Pure public int indexOf(Object o) { if (!allowNulls() && o == null) { throw new NullPointerException(); @@ -531,6 +540,7 @@ public int indexOf(Object o) { } @Override + @Pure public int lastIndexOf(Object o) { if (!allowNulls() && o == null) { throw new NullPointerException(); @@ -597,6 +607,7 @@ public int size() { } @Override + @Pure @EnsuresNonEmptyIf(result = false, expression = "this") public boolean isEmpty() { return false; @@ -604,6 +615,7 @@ public boolean isEmpty() { @Override @SuppressWarnings("unchecked") + @Pure public E get(int index) { if (index == 0) { return e0; @@ -614,6 +626,7 @@ public E get(int index) { } @Override + @Pure public int indexOf(Object o) { Objects.requireNonNull(o); if (o.equals(e0)) { @@ -626,6 +639,7 @@ public int indexOf(Object o) { } @Override + @Pure public int lastIndexOf(Object o) { Objects.requireNonNull(o); if (e1 != EMPTY && o.equals(e1)) { @@ -707,6 +721,7 @@ public int size() { } @Override + @Pure public E get(int index) { return elements[index]; } @@ -742,6 +757,7 @@ public Object[] toArray() { } @Override + @Pure public int indexOf(Object o) { if (!allowNulls && o == null) { throw new NullPointerException(); @@ -756,6 +772,7 @@ public int indexOf(Object o) { } @Override + @Pure public int lastIndexOf(Object o) { if (!allowNulls && o == null) { throw new NullPointerException(); @@ -777,6 +794,7 @@ abstract static class AbstractImmutableSet extends AbstractImmutableCollectio implements Set { @Override + @Pure public boolean equals(Object o) { if (o == this) { return true; @@ -797,6 +815,7 @@ public boolean equals(Object o) { } @Override + @Pure public abstract int hashCode(); } @@ -833,6 +852,7 @@ public int size() { } @Override + @Pure @EnsuresNonEmptyIf(result = false, expression = "this") public boolean isEmpty() { return false; @@ -846,6 +866,7 @@ public boolean contains(@UnknownSignedness Object o) { } @Override + @Pure public int hashCode() { return e0.hashCode() + (e1 == EMPTY ? 0 : e1.hashCode()); } @@ -864,6 +885,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @SuppressWarnings("unchecked") public E next(/*@NonEmpty Iterator this*/) { if (idx == 1) { @@ -967,6 +989,7 @@ public int size() { } @Override + @Pure @EnsuresNonEmptyIf(result = false, expression = "this") public boolean isEmpty() { return size == 0; @@ -1002,6 +1025,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty SetNIterator this) { if (remaining > 0) { E element; @@ -1034,6 +1058,7 @@ public Iterator iterator() { } @Override + @Pure public int hashCode() { int h = 0; for (E e : elements) { @@ -1130,6 +1155,7 @@ abstract static class AbstractImmutableMap extends AbstractMap impleme * value should be returned. */ @Override + @Pure public V getOrDefault(Object key, V defaultValue) { V v; return ((v = get(key)) != null) @@ -1157,6 +1183,7 @@ public Set> entrySet() { } @Override + @Pure public V get(Object o) { return o.equals(k0) ? v0 : null; // implicit nullcheck of o } @@ -1174,11 +1201,13 @@ public boolean containsValue(@UnknownSignedness Object o) { } @Override + @Pure public int size() { return 1; } @Override + @Pure @EnsuresNonEmptyIf(result = false, expression = "this") public boolean isEmpty() { return false; @@ -1195,6 +1224,7 @@ private Object writeReplace() { } @Override + @Pure public int hashCode() { return k0.hashCode() ^ v0.hashCode(); } @@ -1265,6 +1295,7 @@ public boolean containsValue(@UnknownSignedness Object o) { } @Override + @Pure public int hashCode() { int hash = 0; for (int i = 0; i < table.length; i += 2) { @@ -1278,6 +1309,7 @@ public int hashCode() { @Override @SuppressWarnings("unchecked") + @Pure public V get(Object o) { if (size == 0) { Objects.requireNonNull(o); @@ -1298,6 +1330,7 @@ public int size() { } @Override + @Pure @EnsuresNonEmptyIf(result = false, expression = "this") public boolean isEmpty() { return size == 0; @@ -1324,6 +1357,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") private int nextIndex() { int idx = this.idx; if (REVERSE) { diff --git a/src/java.base/share/classes/java/util/Iterator.java b/src/java.base/share/classes/java/util/Iterator.java index e5218031bf414..912863723d88a 100644 --- a/src/java.base/share/classes/java/util/Iterator.java +++ b/src/java.base/share/classes/java/util/Iterator.java @@ -34,6 +34,7 @@ import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; import org.checkerframework.framework.qual.Covariant; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; @@ -89,6 +90,7 @@ public interface Iterator { * @throws NoSuchElementException if the iteration has no more elements */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E next(@GuardSatisfied @NonEmpty Iterator this); /** @@ -116,6 +118,8 @@ public interface Iterator { * been called after the last call to the {@code next} * method */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void remove(@GuardSatisfied @CanShrink Iterator this) { throw new UnsupportedOperationException("remove"); } @@ -145,6 +149,7 @@ default void remove(@GuardSatisfied @CanShrink Iterator this) { * @throws NullPointerException if the specified action is null * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(Consumer action) { Objects.requireNonNull(action); while (hasNext()) diff --git a/src/java.base/share/classes/java/util/JumboEnumSet.java b/src/java.base/share/classes/java/util/JumboEnumSet.java index 70541bc3bd330..1f41985d3beb7 100644 --- a/src/java.base/share/classes/java/util/JumboEnumSet.java +++ b/src/java.base/share/classes/java/util/JumboEnumSet.java @@ -38,6 +38,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.dataflow.qual.Pure; @@ -71,6 +72,8 @@ final class JumboEnumSet> extends EnumSet { elements = new long[(universe.length + 63) >>> 6]; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addRange(E from, E to) { int fromIndex = from.ordinal() >>> 6; int toIndex = to.ordinal() >>> 6; @@ -87,6 +90,8 @@ void addRange(E from, E to) { size = to.ordinal() - from.ordinal() + 1; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addAll() { for (int i = 0; i < elements.length; i++) elements[i] = -1; @@ -94,6 +99,8 @@ void addAll() { size = universe.length; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void complement() { for (int i = 0; i < elements.length; i++) elements[i] = ~elements[i]; @@ -110,6 +117,7 @@ void complement() { * * @return an iterator over the elements contained in this set */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty JumboEnumSet this) { return new EnumSetIterator<>(); } @@ -153,6 +161,7 @@ public boolean hasNext() { @Override @SuppressWarnings("unchecked") // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty EnumSetIterator this) { if (!hasNext()) throw new NoSuchElementException(); @@ -164,6 +173,8 @@ public E next(@NonEmpty EnumSetIterator this) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastReturned == 0) throw new IllegalStateException(); @@ -227,6 +238,8 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object e) { * @throws NullPointerException if {@code e} is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { typeCheck(e); @@ -247,6 +260,8 @@ public boolean add(E e) { * @param e element to be removed from this set, if present * @return {@code true} if the set contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object e) { if (e == null) return false; @@ -297,6 +312,8 @@ public boolean containsAll(Collection c) { * @throws NullPointerException if the specified collection or any of * its elements are null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { if (!(c instanceof JumboEnumSet es)) return super.addAll(c); @@ -322,6 +339,8 @@ public boolean addAll(Collection c) { * @return {@code true} if this set changed as a result of the call * @throws NullPointerException if the specified collection is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { if (!(c instanceof JumboEnumSet es)) return super.removeAll(c); @@ -342,6 +361,8 @@ public boolean removeAll(Collection c) { * @return {@code true} if this set changed as a result of the call * @throws NullPointerException if the specified collection is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { if (!(c instanceof JumboEnumSet es)) return super.retainAll(c); @@ -360,6 +381,8 @@ public boolean retainAll(Collection c) { /** * Removes all of the elements from this set. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { Arrays.fill(elements, 0); size = 0; @@ -398,6 +421,7 @@ private boolean recalculateSize() { return size != oldSize; } + @SideEffectFree public EnumSet clone() { JumboEnumSet result = (JumboEnumSet) super.clone(); result.elements = result.elements.clone(); diff --git a/src/java.base/share/classes/java/util/LinkedHashMap.java b/src/java.base/share/classes/java/util/LinkedHashMap.java index c1c76a7dfd5f5..c7630ed325355 100644 --- a/src/java.base/share/classes/java/util/LinkedHashMap.java +++ b/src/java.base/share/classes/java/util/LinkedHashMap.java @@ -38,6 +38,8 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; import java.util.function.BiConsumer; @@ -404,6 +406,8 @@ void afterNodeAccess(Node e) { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putFirst(K k, V v) { try { putMode = PUT_FIRST; @@ -421,6 +425,8 @@ public V putFirst(K k, V v) { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putLast(K k, V v) { try { putMode = PUT_LAST; @@ -547,6 +553,7 @@ public boolean containsValue(@GuardSatisfied LinkedHashMap this, @GuardSat * The {@link #containsKey containsKey} operation may be used to * distinguish these two cases. */ + @CFComment("`get()` is not strictly pure: if `accessOrder==true`, it changes the access order") @Pure public @Nullable V get(@GuardSatisfied LinkedHashMap this, @UnknownSignedness @GuardSatisfied @Nullable Object key) { Node e; @@ -560,6 +567,7 @@ public boolean containsValue(@GuardSatisfied LinkedHashMap this, @GuardSat /** * {@inheritDoc} */ + @CFComment("`getOrDefault()` is not strictly pure: if `accessOrder==true`, it changes the access order") @Pure public V getOrDefault(@Nullable Object key, V defaultValue) { Node e; @@ -573,6 +581,8 @@ public V getOrDefault(@Nullable Object key, V defaultValue) { /** * {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied LinkedHashMap this) { super.clear(); head = tail = null; @@ -715,6 +725,8 @@ final class LinkedKeySet extends AbstractSet implements SequencedSet { LinkedKeySet(boolean reversed) { this.reversed = reversed; } @Pure public final int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { LinkedHashMap.this.clear(); } @SideEffectFree public final Iterator iterator() { @@ -723,6 +735,8 @@ public final Iterator iterator() { @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public final boolean contains(@Nullable @UnknownSignedness Object o) { return containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean remove(@Nullable @UnknownSignedness Object key) { return removeNode(hash(key), key, null, false, true) != null; } @@ -733,6 +747,7 @@ public final Spliterator spliterator() { Spliterator.DISTINCT); } + @SideEffectFree public Object[] toArray() { return keysToArray(new Object[size], reversed); } @@ -741,6 +756,7 @@ public Object[] toArray() { return keysToArray(prepareArray(a), reversed); } + @DoesNotUnrefineReceiver("modifiability") public final void forEach(Consumer action) { if (action == null) throw new NullPointerException(); @@ -757,13 +773,19 @@ public final void forEach(Consumer action) { } public final void addFirst(K k) { throw new UnsupportedOperationException(); } public final void addLast(K k) { throw new UnsupportedOperationException(); } + @Pure public final K getFirst() { return nsee(reversed ? tail : head).key; } + @Pure public final K getLast() { return nsee(reversed ? head : tail).key; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final K removeFirst() { var node = nsee(reversed ? tail : head); removeNode(node.hash, node.key, null, false, false); return node.key; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final K removeLast() { var node = nsee(reversed ? head : tail); removeNode(node.hash, node.key, null, false, false); @@ -797,6 +819,7 @@ public SequencedSet reversed() { * * @return a view of the values contained in this map */ + @SideEffectFree public Collection values() { return sequencedValues(); } @@ -828,6 +851,8 @@ final class LinkedValues extends AbstractCollection implements SequencedColle LinkedValues(boolean reversed) { this.reversed = reversed; } @Pure public final int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { LinkedHashMap.this.clear(); } @SideEffectFree public final Iterator iterator() { @@ -842,6 +867,7 @@ public final Spliterator spliterator() { Spliterator.ORDERED); } + @SideEffectFree public Object[] toArray() { return valuesToArray(new Object[size], reversed); } @@ -850,6 +876,7 @@ public Object[] toArray() { return valuesToArray(prepareArray(a), reversed); } + @DoesNotUnrefineReceiver("modifiability") public final void forEach(Consumer action) { if (action == null) throw new NullPointerException(); @@ -866,13 +893,19 @@ public final void forEach(Consumer action) { } public final void addFirst(V v) { throw new UnsupportedOperationException(); } public final void addLast(V v) { throw new UnsupportedOperationException(); } + @Pure public final V getFirst() { return nsee(reversed ? tail : head).value; } + @Pure public final V getLast() { return nsee(reversed ? head : tail).value; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V removeFirst() { var node = nsee(reversed ? tail : head); removeNode(node.hash, node.key, null, false, false); return node.value; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V removeLast() { var node = nsee(reversed ? head : tail); removeNode(node.hash, node.key, null, false, false); @@ -940,6 +973,8 @@ final class LinkedEntrySet extends AbstractSet> LinkedEntrySet(boolean reversed) { this.reversed = reversed; } @Pure public final int size() { return size; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { LinkedHashMap.this.clear(); } @SideEffectFree public final Iterator> iterator() { @@ -954,6 +989,8 @@ public final boolean contains(@Nullable @UnknownSignedness Object o) { Node candidate = getNode(key); return candidate != null && candidate.equals(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean remove(@Nullable @UnknownSignedness Object o) { if (o instanceof Map.Entry e) { Object key = e.getKey(); @@ -968,6 +1005,7 @@ public final Spliterator> spliterator() { Spliterator.ORDERED | Spliterator.DISTINCT); } + @DoesNotUnrefineReceiver("modifiability") public final void forEach(Consumer> action) { if (action == null) throw new NullPointerException(); @@ -990,13 +1028,19 @@ final Node nsee(Node e) { } public final void addFirst(Map.Entry e) { throw new UnsupportedOperationException(); } public final void addLast(Map.Entry e) { throw new UnsupportedOperationException(); } + @Pure public final Map.Entry getFirst() { return nsee(reversed ? tail : head); } + @Pure public final Map.Entry getLast() { return nsee(reversed ? head : tail); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final Map.Entry removeFirst() { var node = nsee(reversed ? tail : head); removeNode(node.hash, node.key, null, false, false); return node; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final Map.Entry removeLast() { var node = nsee(reversed ? head : tail); removeNode(node.hash, node.key, null, false, false); @@ -1013,6 +1057,7 @@ public SequencedSet> reversed() { // Map overrides + @DoesNotUnrefineReceiver("modifiability") public void forEach(BiConsumer action) { if (action == null) throw new NullPointerException(); @@ -1023,6 +1068,7 @@ public void forEach(BiConsumer action) { throw new ConcurrentModificationException(); } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { if (function == null) throw new NullPointerException(); @@ -1066,6 +1112,8 @@ final LinkedHashMap.Entry nextNode(@NonEmpty LinkedHashIterator this) { return e; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void remove() { Node p = current; if (p == null) @@ -1081,18 +1129,24 @@ public final void remove() { final class LinkedKeyIterator extends LinkedHashIterator implements Iterator { LinkedKeyIterator(boolean reversed) { super(reversed); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final K next(@NonEmpty LinkedKeyIterator this) { return nextNode().getKey(); } } final class LinkedValueIterator extends LinkedHashIterator implements Iterator { LinkedValueIterator(boolean reversed) { super(reversed); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V next(@NonEmpty LinkedValueIterator this) { return nextNode().value; } } final class LinkedEntryIterator extends LinkedHashIterator implements Iterator> { LinkedEntryIterator(boolean reversed) { super(reversed); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final Map.Entry next(@NonEmpty LinkedEntryIterator this) { return nextNode(); } } @@ -1143,68 +1197,90 @@ static class ReversedLinkedHashMapView extends AbstractMap // Object // inherit toString() from AbstractMap; it depends on entrySet() + @Pure public boolean equals(Object o) { return base.equals(o); } + @Pure public int hashCode() { return base.hashCode(); } // Map + @Pure public int size() { return base.size(); } + @Pure public boolean isEmpty() { return base.isEmpty(); } + @Pure public boolean containsKey(Object key) { return base.containsKey(key); } + @Pure public boolean containsValue(Object value) { return base.containsValue(value); } + @CFComment("`get()` is not strictly pure: if `accessOrder==true`, it changes the access order") + @Pure public V get(Object key) { return base.get(key); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V put(K key, V value) { return base.put(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V remove(Object key) { return base.remove(key); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(Map m) { base.putAll(m); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { base.clear(); } + @SideEffectFree public Set keySet() { return base.sequencedKeySet().reversed(); } + @SideEffectFree public Collection values() { return base.sequencedValues().reversed(); } + @SideEffectFree public Set> entrySet() { return base.sequencedEntrySet().reversed(); } + @CFComment("`getOrDefault()` is not strictly pure: if `accessOrder==true`, it changes the access order") + @Pure public V getOrDefault(Object key, V defaultValue) { return base.getOrDefault(key, defaultValue); } + @DoesNotUnrefineReceiver("modifiability") public void forEach(BiConsumer action) { if (action == null) throw new NullPointerException(); @@ -1215,6 +1291,7 @@ public void forEach(BiConsumer action) { throw new ConcurrentModificationException(); } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { if (function == null) throw new NullPointerException(); @@ -1225,34 +1302,46 @@ public void replaceAll(BiFunction function) { throw new ConcurrentModificationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putIfAbsent(K key, V value) { return base.putIfAbsent(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(Object key, Object value) { return base.remove(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { return base.replace(key, oldValue, newValue); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { return base.replace(key, value); } + @DoesNotUnrefineReceiver("modifiability") public V computeIfAbsent(K key, Function mappingFunction) { return base.computeIfAbsent(key, mappingFunction); } + @DoesNotUnrefineReceiver("modifiability") public V computeIfPresent(K key, BiFunction remappingFunction) { return base.computeIfPresent(key, remappingFunction); } + @DoesNotUnrefineReceiver("modifiability") public V compute(K key, BiFunction remappingFunction) { return base.compute(key, remappingFunction); } + @DoesNotUnrefineReceiver("modifiability") public V merge(K key, V value, BiFunction remappingFunction) { return base.merge(key, value, remappingFunction); } @@ -1271,18 +1360,26 @@ public Entry lastEntry() { return base.firstEntry(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollFirstEntry() { return base.pollLastEntry(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Entry pollLastEntry() { return base.pollFirstEntry(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putFirst(K k, V v) { return base.putLast(k, v); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putLast(K k, V v) { return base.putFirst(k, v); } diff --git a/src/java.base/share/classes/java/util/LinkedHashSet.java b/src/java.base/share/classes/java/util/LinkedHashSet.java index dc0110b6b67f0..755512641f681 100644 --- a/src/java.base/share/classes/java/util/LinkedHashSet.java +++ b/src/java.base/share/classes/java/util/LinkedHashSet.java @@ -25,8 +25,12 @@ package java.util; +import org.checkerframework.dataflow.qual.Deterministic; +import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** *

Hash table and linked list implementation of the {@code Set} interface, @@ -207,6 +211,7 @@ public LinkedHashSet(Collection c) { * @since 1.8 */ @Override + @SideEffectFree public Spliterator spliterator() { return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED); } @@ -231,6 +236,7 @@ public static LinkedHashSet newLinkedHashSet(int numElements) { } @SuppressWarnings("unchecked") + @Deterministic LinkedHashMap map() { return (LinkedHashMap) map; } @@ -243,6 +249,8 @@ LinkedHashMap map() { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { map().putFirst(e, PRESENT); } @@ -255,6 +263,8 @@ public void addFirst(E e) { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { map().putLast(e, PRESENT); } @@ -265,6 +275,7 @@ public void addLast(E e) { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure public E getFirst() { return map().sequencedKeySet().getFirst(); } @@ -275,6 +286,7 @@ public E getFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure public E getLast() { return map().sequencedKeySet().getLast(); } @@ -285,6 +297,8 @@ public E getLast() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { return map().sequencedKeySet().removeFirst(); } @@ -295,6 +309,8 @@ public E removeFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { return map().sequencedKeySet().removeLast(); } @@ -308,18 +324,26 @@ public E removeLast() { * @return {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public SequencedSet reversed() { class ReverseLinkedHashSetView extends AbstractSet implements SequencedSet { + @Pure public int size() { return LinkedHashSet.this.size(); } + @SideEffectFree public Iterator iterator() { return map().sequencedKeySet().reversed().iterator(); } public boolean add(E e) { return LinkedHashSet.this.add(e); } public void addFirst(E e) { LinkedHashSet.this.addLast(e); } public void addLast(E e) { LinkedHashSet.this.addFirst(e); } + @Pure public E getFirst() { return LinkedHashSet.this.getLast(); } + @Pure public E getLast() { return LinkedHashSet.this.getFirst(); } public E removeFirst() { return LinkedHashSet.this.removeLast(); } public E removeLast() { return LinkedHashSet.this.removeFirst(); } public SequencedSet reversed() { return LinkedHashSet.this; } + @Pure + @SideEffectFree public Object[] toArray() { return map().keysToArray(new Object[map.size()], true); } public T[] toArray(T[] a) { return map().keysToArray(map.prepareArray(a), true); } } diff --git a/src/java.base/share/classes/java/util/LinkedList.java b/src/java.base/share/classes/java/util/LinkedList.java index 3f7c84a8a2f05..6740c068eb551 100644 --- a/src/java.base/share/classes/java/util/LinkedList.java +++ b/src/java.base/share/classes/java/util/LinkedList.java @@ -44,6 +44,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.io.ObjectInput; @@ -271,6 +272,7 @@ E unlink(Node x) { * @return the first element in this list * @throws NoSuchElementException if this list is empty */ + @Pure public E getFirst(@GuardSatisfied @NonEmpty LinkedList this) { final Node f = first; if (f == null) @@ -284,6 +286,7 @@ public E getFirst(@GuardSatisfied @NonEmpty LinkedList this) { * @return the last element in this list * @throws NoSuchElementException if this list is empty */ + @Pure public E getLast(@GuardSatisfied @NonEmpty LinkedList this) { final Node l = last; if (l == null) @@ -297,6 +300,8 @@ public E getLast(@GuardSatisfied @NonEmpty LinkedList this) { * @return the first element from this list * @throws NoSuchElementException if this list is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { final Node f = first; if (f == null) @@ -310,6 +315,8 @@ public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { * @return the last element from this list * @throws NoSuchElementException if this list is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { final Node l = last; if (l == null) @@ -322,6 +329,8 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { * * @param e the element to add */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(@GuardSatisfied LinkedList this, E e) { linkFirst(e); } @@ -333,6 +342,8 @@ public void addFirst(@GuardSatisfied LinkedList this, E e) { * * @param e the element to add */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(@GuardSatisfied LinkedList this, E e) { linkLast(e); } @@ -372,6 +383,8 @@ public boolean contains(@GuardSatisfied LinkedList this, @GuardSatisfied @Nul */ @ReleasesNoLocks @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied LinkedList this, E e) { linkLast(e); return true; @@ -391,6 +404,8 @@ public boolean add(@GuardSatisfied LinkedList this, E e) { * @return {@code true} if this list contained the specified element */ @ReleasesNoLocks + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @CanShrink LinkedList this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o == null) { for (Node x = first; x != null; x = x.next) { @@ -422,6 +437,8 @@ public boolean remove(@GuardSatisfied @CanShrink LinkedList this, @GuardSatis * @return {@code true} if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied LinkedList this, Collection c) { return addAll(size, c); } @@ -441,6 +458,8 @@ public boolean addAll(@GuardSatisfied LinkedList this, Collection this, @NonNegative int index, Collection c) { checkPositionIndex(index); @@ -484,6 +503,8 @@ public boolean addAll(@GuardSatisfied LinkedList this, @NonNegative int index * Removes all of the elements from this list. * The list will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink LinkedList this) { // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit @@ -526,6 +547,8 @@ public E get(@GuardSatisfied LinkedList this, @NonNegative int index) { * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(@GuardSatisfied LinkedList this, @NonNegative int index, E element) { checkElementIndex(index); Node x = node(index); @@ -543,6 +566,8 @@ public E set(@GuardSatisfied LinkedList this, @NonNegative int index, E eleme * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(@GuardSatisfied LinkedList this, @NonNegative int index, E element) { checkPositionIndex(index); @@ -561,6 +586,8 @@ public void add(@GuardSatisfied LinkedList this, @NonNegative int index, E el * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @CanShrink LinkedList this, @NonNegative int index) { checkElementIndex(index); return unlink(node(index)); @@ -702,6 +729,7 @@ Node node(@NonNegative int index) { * @throws NoSuchElementException if this list is empty * @since 1.5 */ + @Pure public E element(@GuardSatisfied @NonEmpty LinkedList this) { return getFirst(); } @@ -712,6 +740,8 @@ public E element(@GuardSatisfied @NonEmpty LinkedList this) { * @return the head of this list, or {@code null} if this list is empty * @since 1.5 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink LinkedList this) { final Node f = first; return (f == null) ? null : unlinkFirst(f); @@ -724,6 +754,8 @@ public E element(@GuardSatisfied @NonEmpty LinkedList this) { * @throws NoSuchElementException if this list is empty * @since 1.5 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { return removeFirst(); } @@ -735,6 +767,8 @@ public E remove(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { * @return {@code true} (as specified by {@link Queue#offer}) * @since 1.5 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { return add(e); } @@ -747,6 +781,8 @@ public boolean offer(E e) { * @return {@code true} (as specified by {@link Deque#offerFirst}) * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerFirst(E e) { addFirst(e); return true; @@ -759,6 +795,8 @@ public boolean offerFirst(E e) { * @return {@code true} (as specified by {@link Deque#offerLast}) * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerLast(E e) { addLast(e); return true; @@ -800,6 +838,8 @@ public boolean offerLast(E e) { * this list is empty * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst(@GuardSatisfied @CanShrink LinkedList this) { final Node f = first; return (f == null) ? null : unlinkFirst(f); @@ -813,6 +853,8 @@ public boolean offerLast(E e) { * this list is empty * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast(@GuardSatisfied @CanShrink LinkedList this) { final Node l = last; return (l == null) ? null : unlinkLast(l); @@ -827,6 +869,8 @@ public boolean offerLast(E e) { * @param e the element to push * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void push(@GuardSatisfied LinkedList this, E e) { addFirst(e); } @@ -842,6 +886,8 @@ public void push(@GuardSatisfied LinkedList this, E e) { * @throws NoSuchElementException if this list is empty * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pop(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { return removeFirst(); } @@ -855,6 +901,8 @@ public E pop(@GuardSatisfied @NonEmpty @CanShrink LinkedList this) { * @return {@code true} if the list contained the specified element * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeFirstOccurrence(@GuardSatisfied @CanShrink LinkedList this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { return remove(o); } @@ -868,6 +916,8 @@ public boolean removeFirstOccurrence(@GuardSatisfied @CanShrink LinkedList th * @return {@code true} if the list contained the specified element * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeLastOccurrence(@GuardSatisfied @CanShrink LinkedList this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o == null) { for (Node x = last; x != null; x = x.prev) { @@ -932,6 +982,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty ListItr this) { checkForComodification(); if (!hasNext()) @@ -943,10 +994,13 @@ public E next(@NonEmpty ListItr this) { return lastReturned.item; } + @Pure public boolean hasPrevious() { return nextIndex > 0; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E previous() { checkForComodification(); if (!hasPrevious()) @@ -957,14 +1011,18 @@ public E previous() { return lastReturned.item; } + @Pure public int nextIndex() { return nextIndex; } + @Pure public int previousIndex() { return nextIndex - 1; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { checkForComodification(); if (lastReturned == null) @@ -980,6 +1038,8 @@ public void remove() { expectedModCount++; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { if (lastReturned == null) throw new IllegalStateException(); @@ -987,6 +1047,8 @@ public void set(E e) { lastReturned.item = e; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { checkForComodification(); lastReturned = null; @@ -1045,9 +1107,12 @@ public boolean hasNext() { return itr.hasPrevious(); } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty DescendingIterator this) { return itr.previous(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { itr.remove(); } @@ -1344,22 +1409,29 @@ static class ReverseOrderLinkedListView extends LinkedList implements java this.rdeque = rdeque; } + @SideEffectFree public String toString() { return rlist.toString(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { return rlist.retainAll(c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return rlist.removeAll(c); } + @Pure public boolean containsAll(Collection c) { return rlist.containsAll(c); } + @Pure public boolean isEmpty() { return rlist.isEmpty(); } @@ -1372,6 +1444,8 @@ public Stream stream() { return rlist.stream(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { return rlist.removeIf(filter); } @@ -1384,18 +1458,22 @@ public void forEach(Consumer action) { rlist.forEach(action); } + @SideEffectFree public Iterator iterator() { return rlist.iterator(); } + @Pure public int hashCode() { return rlist.hashCode(); } + @Pure public boolean equals(Object o) { return rlist.equals(o); } + @SideEffectFree public List subList(int fromIndex, int toIndex) { return rlist.subList(fromIndex, toIndex); } @@ -1404,10 +1482,13 @@ public ListIterator listIterator() { return rlist.listIterator(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { rlist.sort(c); } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { rlist.replaceAll(operator); } @@ -1416,6 +1497,7 @@ public LinkedList reversed() { return list; } + @SideEffectFree public Spliterator spliterator() { return rlist.spliterator(); } @@ -1424,6 +1506,7 @@ public T[] toArray(T[] a) { return rlist.toArray(a); } + @SideEffectFree public Object[] toArray() { return rlist.toArray(); } @@ -1436,138 +1519,195 @@ public ListIterator listIterator(int index) { return rlist.listIterator(index); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeLastOccurrence(Object o) { return rdeque.removeLastOccurrence(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeFirstOccurrence(Object o) { return rdeque.removeFirstOccurrence(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pop() { return rdeque.pop(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void push(E e) { rdeque.push(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pollLast() { return rdeque.pollLast(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pollFirst() { return rdeque.pollFirst(); } + @Pure public E peekLast() { return rdeque.peekLast(); } + @Pure public E peekFirst() { return rdeque.peekFirst(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerLast(E e) { return rdeque.offerLast(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerFirst(E e) { return rdeque.offerFirst(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { return rdeque.offer(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove() { return rdeque.remove(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll() { return rdeque.poll(); } + @Pure public E element() { return rdeque.element(); } + @Pure public E peek() { return rdeque.peek(); } + @Pure public int lastIndexOf(Object o) { return rlist.lastIndexOf(o); } + @Pure public int indexOf(Object o) { return rlist.indexOf(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { return rlist.remove(index); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { rlist.add(index, element); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { return rlist.set(index, element); } + @Pure public E get(int index) { return rlist.get(index); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { rlist.clear(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { return rlist.addAll(index, c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { return rlist.addAll(c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(Object o) { return rlist.remove(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return rlist.add(e); } + @Pure public int size() { return rlist.size(); } + @Pure public boolean contains(Object o) { return rlist.contains(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { rdeque.addLast(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { rdeque.addFirst(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { return rdeque.removeLast(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { return rdeque.removeFirst(); } + @Pure public E getLast() { return rdeque.getLast(); } + @Pure public E getFirst() { return rdeque.getFirst(); } diff --git a/src/java.base/share/classes/java/util/List.java b/src/java.base/share/classes/java/util/List.java index 6b79651d38974..d67a63db81e1c 100644 --- a/src/java.base/share/classes/java/util/List.java +++ b/src/java.base/share/classes/java/util/List.java @@ -46,6 +46,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.UnaryOperator; @@ -269,7 +270,6 @@ public interface List extends SequencedCollection { * this list * @throws NullPointerException if the specified array is null */ - @SideEffectFree @Nullable T[] toArray(@PolyNull T[] a); @@ -299,6 +299,7 @@ public interface List extends SequencedCollection { */ @ReleasesNoLocks // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmpty("this") boolean add(@GuardSatisfied List this, E e); @@ -324,6 +325,7 @@ public interface List extends SequencedCollection { * is not supported by this list */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean remove(@GuardSatisfied @CanShrink List this, @UnknownSignedness Object o); @@ -372,6 +374,7 @@ public interface List extends SequencedCollection { * @see #add(Object) */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmptyIf(result = true, expression = "this") boolean addAll(@GuardSatisfied List this, Collection c); @@ -403,6 +406,7 @@ public interface List extends SequencedCollection { * ({@code index < 0 || index > size()}) */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmptyIf(result = true, expression = "this") boolean addAll(@GuardSatisfied List this, @IndexOrHigh({"this"}) int index, Collection c); @@ -424,6 +428,8 @@ public interface List extends SequencedCollection { * @see #remove(Object) * @see #contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean removeAll(@GuardSatisfied @CanShrink List this, Collection c); /** @@ -446,6 +452,8 @@ public interface List extends SequencedCollection { * @see #remove(Object) * @see #contains(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean retainAll(@GuardSatisfied @CanShrink List this, Collection c); /** @@ -477,6 +485,7 @@ public interface List extends SequencedCollection { * (optional) * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default void replaceAll(UnaryOperator operator) { Objects.requireNonNull(operator); final ListIterator li = this.listIterator(); @@ -545,6 +554,8 @@ default void replaceAll(UnaryOperator operator) { * @since 1.8 */ @SuppressWarnings({"unchecked", "rawtypes"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void sort(Comparator c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); @@ -562,6 +573,8 @@ default void sort(Comparator c) { * @throws UnsupportedOperationException if the {@code clear} operation * is not supported by this list */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void clear(@GuardSatisfied @CanShrink List this); @@ -636,6 +649,8 @@ default void sort(Comparator c) { * @throws IndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index >= size()}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E set(@GuardSatisfied List this, @IndexFor({"this"}) int index, E element); /** @@ -659,6 +674,7 @@ default void sort(Comparator c) { */ @ReleasesNoLocks // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void add(@GuardSatisfied List this, @IndexOrHigh({"this"}) int index, E element); /** @@ -675,6 +691,8 @@ default void sort(Comparator c) { * ({@code index < 0 || index >= size()}) */ @ReleasesNoLocks + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E remove(@GuardSatisfied @CanShrink List this, @IndexFor({"this"}) int index); @@ -843,6 +861,8 @@ default Spliterator spliterator() { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void addFirst(E e) { this.add(0, e); } @@ -857,6 +877,8 @@ default void addFirst(E e) { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void addLast(E e) { this.add(e); } @@ -871,6 +893,7 @@ default void addLast(E e) { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure default E getFirst() { if (this.isEmpty()) { throw new NoSuchElementException(); @@ -889,6 +912,7 @@ default E getFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure default E getLast() { if (this.isEmpty()) { throw new NoSuchElementException(); @@ -908,6 +932,8 @@ default E getLast() { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeFirst() { if (this.isEmpty()) { throw new NoSuchElementException(); @@ -927,6 +953,8 @@ default E removeFirst() { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeLast() { if (this.isEmpty()) { throw new NoSuchElementException(); @@ -961,6 +989,7 @@ default List reversed() { * * @since 9 */ + @SideEffectFree @SuppressWarnings("unchecked") static List of() { return (List) ImmutableCollections.EMPTY_LIST; @@ -978,6 +1007,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1) { return new ImmutableCollections.List12<>(e1); } @@ -995,6 +1025,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2) { return new ImmutableCollections.List12<>(e1, e2); } @@ -1013,6 +1044,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3); } @@ -1032,6 +1064,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4); } @@ -1052,6 +1085,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4, E e5) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5); } @@ -1073,6 +1107,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4, E e5, E e6) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5, e6); @@ -1096,6 +1131,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5, e6, e7); @@ -1120,6 +1156,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5, e6, e7, e8); @@ -1145,6 +1182,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5, e6, e7, e8, e9); @@ -1171,6 +1209,7 @@ static List of() { * * @since 9 */ + @SideEffectFree static @NonEmpty List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) { return ImmutableCollections.listFromTrustedArray(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); @@ -1201,6 +1240,7 @@ static List of() { * * @since 9 */ + @SideEffectFree @SafeVarargs @SuppressWarnings("varargs") static @PolyNonEmpty List of(E @PolyNonEmpty... elements) { @@ -1234,6 +1274,7 @@ static List of() { * @throws NullPointerException if coll is null, or if it contains any nulls * @since 10 */ + @SideEffectFree static @PolyNonEmpty List copyOf(@PolyNonEmpty Collection coll) { return ImmutableCollections.listCopy(coll); } diff --git a/src/java.base/share/classes/java/util/ListIterator.java b/src/java.base/share/classes/java/util/ListIterator.java index 8066796dee94c..ec6b6e863346c 100644 --- a/src/java.base/share/classes/java/util/ListIterator.java +++ b/src/java.base/share/classes/java/util/ListIterator.java @@ -34,6 +34,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * An iterator for lists that allows the programmer @@ -97,6 +98,7 @@ public interface ListIterator extends Iterator { * @throws NoSuchElementException if the iteration has no next element */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E next(@GuardSatisfied @NonEmpty ListIterator this); /** @@ -123,6 +125,8 @@ public interface ListIterator extends Iterator { * @throws NoSuchElementException if the iteration has no previous * element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E previous(@GuardSatisfied ListIterator this); /** @@ -166,6 +170,8 @@ public interface ListIterator extends Iterator { * {@code add} have been called after the last call to * {@code next} or {@code previous} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void remove(@GuardSatisfied ListIterator this); /** @@ -188,6 +194,8 @@ public interface ListIterator extends Iterator { * {@code add} have been called after the last call to * {@code next} or {@code previous} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void set(@GuardSatisfied ListIterator this, E e); /** @@ -210,5 +218,7 @@ public interface ListIterator extends Iterator { * @throws IllegalArgumentException if some aspect of this element * prevents it from being added to this list */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void add(@GuardSatisfied ListIterator this, E e); } diff --git a/src/java.base/share/classes/java/util/Map.java b/src/java.base/share/classes/java/util/Map.java index 64297dd01c7f8..a4894dfa10a19 100644 --- a/src/java.base/share/classes/java/util/Map.java +++ b/src/java.base/share/classes/java/util/Map.java @@ -45,6 +45,7 @@ import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; import org.checkerframework.framework.qual.Covariant; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.BiConsumer; import java.util.function.BiFunction; @@ -222,8 +223,8 @@ public interface Map { */ @CFComment("nullness: key is not @Nullable because this map might not permit null values") @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) - @EnsuresNonEmptyIf(result=true, expression={"this"}) @Pure + @EnsuresNonEmptyIf(result=true, expression={"this"}) boolean containsKey(@GuardSatisfied Map this, @GuardSatisfied @UnknownSignedness Object key); /** @@ -242,8 +243,8 @@ public interface Map { * @throws NullPointerException if the specified value is null and this * map does not permit null values ({@linkplain Collection##optional-restrictions optional}) */ - @EnsuresNonEmptyIf(result=true, expression={"this"}) @Pure + @EnsuresNonEmptyIf(result=true, expression={"this"}) boolean containsValue(@GuardSatisfied Map this, @GuardSatisfied @UnknownSignedness Object value); /** @@ -302,6 +303,8 @@ public interface Map { @ReleasesNoLocks @EnsuresKeyFor(value={"#1"}, map={"this"}) @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable V put(@GuardSatisfied Map this, K key, V value); /** @@ -333,6 +336,8 @@ public interface Map { * map does not permit null keys ({@linkplain Collection##optional-restrictions optional}) */ @CFComment("nullness: key is not @Nullable because this map might not permit null values") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable V remove(@GuardSatisfied Map this, @GuardSatisfied @UnknownSignedness Object key); @@ -359,6 +364,8 @@ public interface Map { * @throws IllegalArgumentException if some property of a key or value in * the specified map prevents it from being stored in this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void putAll(@GuardSatisfied Map this, Map m); /** @@ -368,6 +375,8 @@ public interface Map { * @throws UnsupportedOperationException if the {@code clear} operation * is not supported by this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void clear(@GuardSatisfied Map this); @@ -532,6 +541,8 @@ interface Entry { * required to, throw this exception if the entry has been * removed from the backing map. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") V setValue(Map.@GuardSatisfied Entry this, V value); /** @@ -672,6 +683,7 @@ public static Comparator> comparingByValue(Comparator Map.Entry copyOf(Map.Entry e) { Objects.requireNonNull(e); if (e instanceof KeyValueHolder) { @@ -696,6 +708,7 @@ public static Comparator> comparingByValue(Comparator this, @GuardSatisfied @Nullable Object o); /** @@ -711,6 +724,7 @@ public static Comparator> comparingByValue(Comparator this); // Defaultable methods @@ -768,6 +782,7 @@ default V getOrDefault(@GuardSatisfied @UnknownSignedness Object key, V defaultV * removed during iteration * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default void forEach(@NonLeaked BiConsumer action) { Objects.requireNonNull(action); for (Map.Entry entry : entrySet()) { @@ -818,6 +833,7 @@ default void forEach(@NonLeaked BiConsumer action) { * removed during iteration * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default void replaceAll(BiFunction function) { Objects.requireNonNull(function); for (Map.Entry entry : entrySet()) { @@ -885,6 +901,8 @@ default void replaceAll(BiFunction function) * @since 1.8 */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default @Nullable V putIfAbsent(K key, V value) { V v = get(key); if (v == null) { @@ -929,6 +947,8 @@ default void replaceAll(BiFunction function) * @since 1.8 */ @CFComment("nullness: key and value are not @Nullable because this map might not permit null values") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default boolean remove(@GuardSatisfied @UnknownSignedness Object key, @GuardSatisfied @UnknownSignedness Object value) { Object curValue = get(key); if (!Objects.equals(curValue, value) || @@ -979,6 +999,8 @@ default boolean remove(@GuardSatisfied @UnknownSignedness Object key, @GuardSati * or value prevents it from being stored in this map * @since 1.8 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default boolean replace(K key, V oldValue, V newValue) { Object curValue = get(key); if (!Objects.equals(curValue, oldValue) || @@ -1027,6 +1049,8 @@ default boolean replace(K key, V oldValue, V newValue) { * or value prevents it from being stored in this map * @since 1.8 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default @Nullable V replace(K key, V value) { V curValue; if (((curValue = get(key)) != null) || containsKey(key)) { @@ -1109,6 +1133,7 @@ default boolean replace(K key, V oldValue, V newValue) { * ({@linkplain Collection##optional-restrictions optional}) * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default @PolyNull V computeIfAbsent(K key, Function mappingFunction) { Objects.requireNonNull(mappingFunction); @@ -1186,6 +1211,7 @@ default boolean replace(K key, V oldValue, V newValue) { * ({@linkplain Collection##optional-restrictions optional}) * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default @Nullable V computeIfPresent(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -1272,6 +1298,7 @@ default boolean replace(K key, V oldValue, V newValue) { * ({@linkplain Collection##optional-restrictions optional}) * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default @Nullable V compute(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -1370,6 +1397,7 @@ default boolean replace(K key, V oldValue, V newValue) { * null * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") default @Nullable V merge(K key, @NonNull V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -1396,6 +1424,7 @@ default boolean replace(K key, V oldValue, V newValue) { * @since 9 */ @SuppressWarnings("unchecked") + @SideEffectFree static Map of() { return (Map) ImmutableCollections.EMPTY_MAP; } @@ -1413,6 +1442,7 @@ static Map of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Map of(K k1, V v1) { return new ImmutableCollections.Map1<>(k1, v1); } @@ -1433,6 +1463,7 @@ static Map of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Map of(K k1, V v1, K k2, V v2) { return new ImmutableCollections.MapN<>(k1, v1, k2, v2); } @@ -1455,6 +1486,7 @@ static Map of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Map of(K k1, V v1, K k2, V v2, K k3, V v3) { return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3); } @@ -1479,6 +1511,7 @@ static Map of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4); } @@ -1505,6 +1538,7 @@ static Map of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); } @@ -1791,6 +1825,7 @@ static Map of() { * @since 10 */ @SuppressWarnings({"rawtypes","unchecked"}) + @SideEffectFree static @PolyNonEmpty Map copyOf(@PolyNonEmpty Map map) { if (map instanceof ImmutableCollections.AbstractImmutableMap) { return (Map)map; diff --git a/src/java.base/share/classes/java/util/NavigableMap.java b/src/java.base/share/classes/java/util/NavigableMap.java index 6fd455e0255de..12653e7153764 100644 --- a/src/java.base/share/classes/java/util/NavigableMap.java +++ b/src/java.base/share/classes/java/util/NavigableMap.java @@ -41,6 +41,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A {@link SortedMap} extended with navigation methods returning the @@ -251,6 +252,8 @@ public interface NavigableMap extends SortedMap { * @return the removed first entry of this map, * or {@code null} if this map is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") Map.@Nullable Entry pollFirstEntry(@GuardSatisfied NavigableMap this); /** @@ -260,6 +263,8 @@ public interface NavigableMap extends SortedMap { * @return the removed last entry of this map, * or {@code null} if this map is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") Map.@Nullable Entry pollLastEntry(@GuardSatisfied NavigableMap this); /** diff --git a/src/java.base/share/classes/java/util/NavigableSet.java b/src/java.base/share/classes/java/util/NavigableSet.java index 3bcdf18792013..fd6b8d14e497f 100644 --- a/src/java.base/share/classes/java/util/NavigableSet.java +++ b/src/java.base/share/classes/java/util/NavigableSet.java @@ -42,6 +42,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A {@link SortedSet} extended with navigation methods reporting @@ -160,6 +161,8 @@ public interface NavigableSet extends SortedSet { * * @return the first element, or {@code null} if this set is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E pollFirst(@GuardSatisfied NavigableSet this); /** @@ -168,6 +171,8 @@ public interface NavigableSet extends SortedSet { * * @return the last element, or {@code null} if this set is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E pollLast(@GuardSatisfied NavigableSet this); /** @@ -193,6 +198,8 @@ public interface NavigableSet extends SortedSet { * * @return a reverse order view of this set */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") NavigableSet descendingSet(); /** @@ -201,6 +208,8 @@ public interface NavigableSet extends SortedSet { * * @return an iterator over the elements in this set, in descending order */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") Iterator descendingIterator(); /** @@ -349,6 +358,8 @@ NavigableSet subSet(@GuardSatisfied NavigableSet this, @GuardSatisfied E f * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeFirst() { if (this.isEmpty()) { throw new NoSuchElementException(); @@ -368,6 +379,8 @@ default E removeFirst() { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeLast() { if (this.isEmpty()) { throw new NoSuchElementException(); @@ -388,6 +401,8 @@ default E removeLast() { * @return a reverse-ordered view of this collection, as a {@code NavigableSet} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default NavigableSet reversed() { return this.descendingSet(); } diff --git a/src/java.base/share/classes/java/util/Objects.java b/src/java.base/share/classes/java/util/Objects.java index 9544c1cce499e..c33b12a060453 100644 --- a/src/java.base/share/classes/java/util/Objects.java +++ b/src/java.base/share/classes/java/util/Objects.java @@ -301,8 +301,8 @@ public static int compare(@GuardSatisfied @Nullable @UnknownSignedness T a, * @see java.util.function.Predicate * @since 1.8 */ - @EnsuresNonNullIf(expression={"#1"}, result=false) @Pure + @EnsuresNonNullIf(expression={"#1"}, result=false) public static boolean isNull(@GuardSatisfied @Nullable @UnknownSignedness Object obj) { return obj == null; } @@ -321,8 +321,8 @@ public static boolean isNull(@GuardSatisfied @Nullable @UnknownSignedness Object * @see java.util.function.Predicate * @since 1.8 */ - @EnsuresNonNullIf(expression={"#1"}, result=true) @Pure + @EnsuresNonNullIf(expression={"#1"}, result=true) public static boolean nonNull(@GuardSatisfied @Nullable @UnknownSignedness Object obj) { return obj != null; } @@ -385,8 +385,8 @@ public static boolean nonNull(@GuardSatisfied @Nullable @UnknownSignedness Objec * @throws NullPointerException if {@code obj} is {@code null} * @since 1.8 */ - @EnsuresNonNull("#1") @Pure + @EnsuresNonNull("#1") public static @NonNull T requireNonNull(@GuardSatisfied @NonNull @UnknownSignedness T obj, @GuardSatisfied Supplier messageSupplier) { if (obj == null) throw new NullPointerException(messageSupplier == null ? diff --git a/src/java.base/share/classes/java/util/PrimitiveIterator.java b/src/java.base/share/classes/java/util/PrimitiveIterator.java index ed9efa28db275..61ead18c6991d 100644 --- a/src/java.base/share/classes/java/util/PrimitiveIterator.java +++ b/src/java.base/share/classes/java/util/PrimitiveIterator.java @@ -27,6 +27,7 @@ import org.checkerframework.checker.lock.qual.GuardSatisfied; import org.checkerframework.checker.nonempty.qual.NonEmpty; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; import java.util.function.DoubleConsumer; @@ -90,6 +91,7 @@ public interface PrimitiveIterator extends Iterator { * @throws NullPointerException if the specified action is null */ @SuppressWarnings("overloads") + @DoesNotUnrefineReceiver("modifiability") void forEachRemaining(T_CONS action); /** @@ -105,6 +107,8 @@ public static interface OfInt extends PrimitiveIterator { * @return the next {@code int} element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") int nextInt(@NonEmpty OfInt this); /** @@ -116,6 +120,7 @@ public static interface OfInt extends PrimitiveIterator { * action.accept(nextInt()); * } */ + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(IntConsumer action) { Objects.requireNonNull(action); while (hasNext()) @@ -129,6 +134,8 @@ default void forEachRemaining(IntConsumer action) { * {@link #nextInt()}, and returns that boxed result. */ @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default Integer next(PrimitiveIterator.@GuardSatisfied OfInt this) { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.nextInt()"); @@ -145,6 +152,7 @@ default Integer next(PrimitiveIterator.@GuardSatisfied OfInt this) { * and then passed to {@link #forEachRemaining}. */ @Override + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(Consumer action) { if (action instanceof IntConsumer) { forEachRemaining((IntConsumer) action); @@ -173,6 +181,8 @@ public static interface OfLong extends PrimitiveIterator { * @return the next {@code long} element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") long nextLong(@NonEmpty OfLong this); /** @@ -184,6 +194,7 @@ public static interface OfLong extends PrimitiveIterator { * action.accept(nextLong()); * } */ + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(LongConsumer action) { Objects.requireNonNull(action); while (hasNext()) @@ -197,6 +208,8 @@ default void forEachRemaining(LongConsumer action) { * {@link #nextLong()}, and returns that boxed result. */ @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default Long next(PrimitiveIterator.@GuardSatisfied OfLong this) { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfLong.nextLong()"); @@ -213,6 +226,7 @@ default Long next(PrimitiveIterator.@GuardSatisfied OfLong this) { * and then passed to {@link #forEachRemaining}. */ @Override + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(Consumer action) { if (action instanceof LongConsumer) { forEachRemaining((LongConsumer) action); @@ -240,6 +254,8 @@ public static interface OfDouble extends PrimitiveIterator */ + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(DoubleConsumer action) { Objects.requireNonNull(action); while (hasNext()) @@ -264,6 +281,8 @@ default void forEachRemaining(DoubleConsumer action) { * {@link #nextDouble()}, and returns that boxed result. */ @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default Double next(PrimitiveIterator.@GuardSatisfied OfDouble this) { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.nextLong()"); @@ -281,6 +300,7 @@ default Double next(PrimitiveIterator.@GuardSatisfied OfDouble this) { * {@link #forEachRemaining}. */ @Override + @DoesNotUnrefineReceiver("modifiability") default void forEachRemaining(Consumer action) { if (action instanceof DoubleConsumer) { forEachRemaining((DoubleConsumer) action); diff --git a/src/java.base/share/classes/java/util/PriorityQueue.java b/src/java.base/share/classes/java/util/PriorityQueue.java index e39c4b531b5ce..b972e27762c84 100644 --- a/src/java.base/share/classes/java/util/PriorityQueue.java +++ b/src/java.base/share/classes/java/util/PriorityQueue.java @@ -44,6 +44,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; import java.util.function.Predicate; @@ -330,6 +331,8 @@ private void grow(int minCapacity) { * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied PriorityQueue this, E e) { return offer(e); } @@ -343,6 +346,8 @@ public boolean add(@GuardSatisfied PriorityQueue this, E e) { * according to the priority queue's ordering * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { if (e == null) throw new NullPointerException(); @@ -360,6 +365,7 @@ public boolean offer(E e) { return (E) queue[0]; } + @Pure private int indexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o != null) { final Object[] es = queue; @@ -381,6 +387,8 @@ private int indexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @CanShrink PriorityQueue this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { int i = indexOf(o); if (i == -1) @@ -474,7 +482,6 @@ public boolean contains(@GuardSatisfied PriorityQueue this, @GuardSatisfied @ * this queue * @throws NullPointerException if the specified array is null */ - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { final int size = this.size; if (a.length < size) @@ -547,6 +554,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { if (expectedModCount != modCount) throw new ConcurrentModificationException(); @@ -561,6 +569,8 @@ public E next(@NonEmpty Itr this) { throw new NoSuchElementException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (expectedModCount != modCount) throw new ConcurrentModificationException(); @@ -593,6 +603,8 @@ public void remove() { * Removes all of the elements from this priority queue. * The queue will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink PriorityQueue this) { modCount++; final Object[] es = queue; @@ -601,6 +613,8 @@ public void clear(@GuardSatisfied @CanShrink PriorityQueue this) { size = 0; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink PriorityQueue this) { final Object[] es; final E result; @@ -849,6 +863,7 @@ private void readObject(java.io.ObjectInputStream s) * @return a {@code Spliterator} over the elements in this queue * @since 1.8 */ + @SideEffectFree public final Spliterator spliterator() { return new PriorityQueueSpliterator(0, -1, 0); } @@ -924,6 +939,8 @@ public int characteristics() { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@GuardSatisfied @CanShrink PriorityQueue this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -932,6 +949,8 @@ public boolean removeIf(@GuardSatisfied @CanShrink PriorityQueue this, Predic /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@GuardSatisfied @CanShrink PriorityQueue this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -940,6 +959,8 @@ public boolean removeAll(@GuardSatisfied @CanShrink PriorityQueue this, Colle /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(@GuardSatisfied @CanShrink PriorityQueue this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); @@ -995,6 +1016,7 @@ private boolean bulkRemove(Predicate filter) { /** * @throws NullPointerException {@inheritDoc} */ + @DoesNotUnrefineReceiver("modifiability") public void forEach(Consumer action) { Objects.requireNonNull(action); final int expectedModCount = modCount; diff --git a/src/java.base/share/classes/java/util/Properties.java b/src/java.base/share/classes/java/util/Properties.java index a25c6f4be6f8f..c6bd98b91b8b2 100644 --- a/src/java.base/share/classes/java/util/Properties.java +++ b/src/java.base/share/classes/java/util/Properties.java @@ -35,6 +35,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.io.PrintStream; @@ -238,6 +239,8 @@ private Properties(Properties defaults, int initialCapacity) { * @see #getProperty * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized @Nullable Object setProperty(@GuardSatisfied Properties this, @PropertyKey String key, String value) { return put(key, value); } @@ -1327,12 +1330,14 @@ public boolean isEmpty() { } @Override + @SideEffectFree public Enumeration keys() { // CHM.keys() returns Iterator w/ remove() - instead wrap keySet() return Collections.enumeration(map.keySet()); } @Override + @SideEffectFree public Enumeration elements() { // CHM.elements() returns Iterator w/ remove() - instead wrap values() return Collections.enumeration(map.values()); @@ -1358,41 +1363,53 @@ public boolean containsKey(@GuardSatisfied @Nullable @UnknownSignedness Object k } @Override + @Pure public @Nullable Object get(Object key) { return map.get(key); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized Object put(Object key, Object value) { return map.put(key, value); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized Object remove(@GuardSatisfied @Nullable @UnknownSignedness Object key) { return map.remove(key); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void putAll(Map t) { map.putAll(t); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void clear() { map.clear(); } @Override + @SideEffectFree public synchronized String toString() { return map.toString(); } @Override + @SideEffectFree public Set<@KeyFor("this") Object> keySet() { return Collections.synchronizedSet(map.keySet(), this); } @Override + @SideEffectFree public Collection values() { return Collections.synchronizedCollection(map.values(), this); } @@ -1415,16 +1432,22 @@ private EntrySet(Set> entrySet) { this.entrySet = entrySet; } - @Pure @Override public int size() { return entrySet.size(); } + @Pure + @Override public int size() { return entrySet.size(); } @Pure @EnsuresNonEmptyIf(result = false, expression = "this") @Override public boolean isEmpty() { return entrySet.isEmpty(); } @Pure @EnsuresNonEmptyIf(result = true, expression = "this") @Override public boolean contains(@UnknownSignedness Object o) { return entrySet.contains(o); } + @SideEffectFree @Override public Object[] toArray() { return entrySet.toArray(); } @Override public @Nullable T[] toArray(@PolyNull T[] a) { return entrySet.toArray(a); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Override public void clear() { entrySet.clear(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Override public boolean remove(@UnknownSignedness Object o) { return entrySet.remove(o); } @Override @@ -1444,42 +1467,52 @@ public boolean containsAll(Collection c) { } @Override + @Pure public boolean equals(Object o) { return o == this || entrySet.equals(o); } @Override + @Pure public int hashCode() { return entrySet.hashCode(); } @Override + @SideEffectFree public String toString() { return entrySet.toString(); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return entrySet.removeAll(c); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { return entrySet.retainAll(c); } @Override + @SideEffectFree public Iterator> iterator() { return entrySet.iterator(); } } @Override + @Pure public synchronized boolean equals(Object o) { return map.equals(o); } @Override + @Pure public synchronized int hashCode() { return map.hashCode(); } @@ -1491,54 +1524,68 @@ public Object getOrDefault(@GuardSatisfied @Nullable @UnknownSignedness Object k } @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void forEach(BiConsumer action) { map.forEach(action); } @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void replaceAll(BiFunction function) { map.replaceAll(function); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized Object putIfAbsent(Object key, Object value) { return map.putIfAbsent(key, value); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object key, @GuardSatisfied @Nullable @UnknownSignedness Object value) { return map.remove(key, value); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized boolean replace(Object key, Object oldValue, Object newValue) { return map.replace(key, oldValue, newValue); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized Object replace(Object key, Object value) { return map.replace(key, value); } @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull Object computeIfAbsent(Object key, Function mappingFunction) { return map.computeIfAbsent(key, mappingFunction); } @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull Object computeIfPresent(Object key, BiFunction remappingFunction) { return map.computeIfPresent(key, remappingFunction); } @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @PolyNull Object compute(Object key, BiFunction remappingFunction) { return map.compute(key, remappingFunction); } @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized @Nullable Object merge(Object key, Object value, BiFunction remappingFunction) { return map.merge(key, value, remappingFunction); @@ -1551,6 +1598,7 @@ public synchronized Object replace(Object key, Object value) { protected void rehash() { /* no-op */ } @Override + @SideEffectFree public synchronized Object clone() { Properties clone = (Properties) cloneHashtable(); clone.map = new ConcurrentHashMap<>(map); diff --git a/src/java.base/share/classes/java/util/Queue.java b/src/java.base/share/classes/java/util/Queue.java index 056c44880016d..c3051c1b7489a 100644 --- a/src/java.base/share/classes/java/util/Queue.java +++ b/src/java.base/share/classes/java/util/Queue.java @@ -44,6 +44,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A collection designed for holding elements prior to processing. @@ -166,6 +167,8 @@ public interface Queue extends Collection { * prevents it from being added to this queue */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean add(@GuardSatisfied Queue this, E e); /** @@ -185,6 +188,8 @@ public interface Queue extends Collection { * @throws IllegalArgumentException if some property of this element * prevents it from being added to this queue */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offer(E e); /** @@ -195,6 +200,8 @@ public interface Queue extends Collection { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E remove(@GuardSatisfied @NonEmpty @CanShrink Queue this); /** @@ -203,6 +210,8 @@ public interface Queue extends Collection { * * @return the head of this queue, or {@code null} if this queue is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E poll(@GuardSatisfied @CanShrink Queue this); /** @@ -213,6 +222,7 @@ public interface Queue extends Collection { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ + @Pure E element(@GuardSatisfied @NonEmpty Queue this); /** @@ -221,5 +231,8 @@ public interface Queue extends Collection { * * @return the head of this queue, or {@code null} if this queue is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure @Nullable E peek(); } diff --git a/src/java.base/share/classes/java/util/RegularEnumSet.java b/src/java.base/share/classes/java/util/RegularEnumSet.java index 2d0c1de09aa8c..af48606a713c8 100644 --- a/src/java.base/share/classes/java/util/RegularEnumSet.java +++ b/src/java.base/share/classes/java/util/RegularEnumSet.java @@ -40,6 +40,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * Private implementation class for EnumSet, for "regular sized" enum types @@ -63,15 +64,21 @@ final class RegularEnumSet> extends EnumSet { super(elementType, universe); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addRange(E from, E to) { elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addAll() { if (universe.length != 0) elements = -1L >>> -universe.length; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void complement() { if (universe.length != 0) { elements = ~elements; @@ -89,6 +96,7 @@ void complement() { * * @return an iterator over the elements contained in this set */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty RegularEnumSet this) { return new EnumSetIterator<>(); } @@ -117,6 +125,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @SuppressWarnings("unchecked") public E next(@NonEmpty EnumSetIterator this) { if (unseen == 0) @@ -126,6 +135,8 @@ public E next(@NonEmpty EnumSetIterator this) { return (E) universe[Long.numberOfTrailingZeros(lastReturned)]; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastReturned == 0) throw new IllegalStateException(); @@ -184,6 +195,8 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object e) { * @throws NullPointerException if {@code e} is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { typeCheck(e); @@ -198,6 +211,8 @@ public boolean add(E e) { * @param e element to be removed from this set, if present * @return {@code true} if the set contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object e) { if (e == null) return false; @@ -240,6 +255,8 @@ public boolean containsAll(Collection c) { * @throws NullPointerException if the specified collection or any * of its elements are null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { if (!(c instanceof RegularEnumSet es)) return super.addAll(c); @@ -265,6 +282,8 @@ public boolean addAll(Collection c) { * @return {@code true} if this set changed as a result of the call * @throws NullPointerException if the specified collection is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { if (!(c instanceof RegularEnumSet es)) return super.removeAll(c); @@ -285,6 +304,8 @@ public boolean removeAll(Collection c) { * @return {@code true} if this set changed as a result of the call * @throws NullPointerException if the specified collection is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { if (!(c instanceof RegularEnumSet es)) return super.retainAll(c); @@ -303,6 +324,8 @@ public boolean retainAll(Collection c) { /** * Removes all of the elements from this set. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { elements = 0; } diff --git a/src/java.base/share/classes/java/util/ReverseOrderDequeView.java b/src/java.base/share/classes/java/util/ReverseOrderDequeView.java index a580ad8f7db3c..19702f274b3db 100644 --- a/src/java.base/share/classes/java/util/ReverseOrderDequeView.java +++ b/src/java.base/share/classes/java/util/ReverseOrderDequeView.java @@ -25,11 +25,14 @@ package java.util; +import org.checkerframework.dataflow.qual.SideEffectFree; + import java.util.function.Consumer; import java.util.function.IntFunction; import java.util.stream.Stream; import java.util.stream.StreamSupport; import jdk.internal.util.ArraysSupport; +import org.checkerframework.dataflow.qual.Pure; /** * Provides a reverse-ordered view of any Deque. Not serializable. @@ -41,6 +44,7 @@ private ReverseOrderDequeView(Deque deque) { base = deque; } + @SideEffectFree public static Deque of(Deque deque) { if (deque instanceof ReverseOrderDequeView rodv) { return rodv.base; @@ -56,10 +60,12 @@ public void forEach(Consumer action) { action.accept(e); } + @SideEffectFree public Iterator iterator() { return base.descendingIterator(); } + @SideEffectFree public Spliterator spliterator() { return Spliterators.spliteratorUnknownSize(base.descendingIterator(), 0); } @@ -84,14 +90,17 @@ public void clear() { base.clear(); } + @Pure public boolean contains(Object o) { return base.contains(o); } + @Pure public boolean containsAll(Collection c) { return base.containsAll(c); } + @Pure public boolean isEmpty() { return base.isEmpty(); } @@ -149,6 +158,7 @@ public boolean retainAll(Collection c) { return modified; } + @Pure public int size() { return base.size(); } @@ -157,6 +167,7 @@ public Stream stream() { return StreamSupport.stream(spliterator(), false); } + @SideEffectFree public Object[] toArray() { return ArraysSupport.reverse(base.toArray()); } @@ -170,6 +181,7 @@ public T[] toArray(IntFunction generator) { return ArraysSupport.reverse(base.toArray(generator)); } + @SideEffectFree // copied from AbstractCollection public String toString() { Iterator it = iterator(); @@ -201,14 +213,17 @@ public Iterator descendingIterator() { return base.iterator(); } + @Pure public E element() { return base.getLast(); } + @Pure public E getFirst() { return base.getLast(); } + @Pure public E getLast() { return base.getFirst(); } @@ -225,14 +240,17 @@ public boolean offerLast(E e) { return base.offerFirst(e); } + @Pure public E peek() { return base.peekLast(); } + @Pure public E peekFirst() { return base.peekLast(); } + @Pure public E peekLast() { return base.peekFirst(); } diff --git a/src/java.base/share/classes/java/util/ReverseOrderListView.java b/src/java.base/share/classes/java/util/ReverseOrderListView.java index 0f7409bef16d0..1ec525c8d1a2f 100644 --- a/src/java.base/share/classes/java/util/ReverseOrderListView.java +++ b/src/java.base/share/classes/java/util/ReverseOrderListView.java @@ -25,6 +25,10 @@ package java.util; +import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; + import java.util.Objects; import java.util.function.Consumer; import java.util.function.IntFunction; @@ -42,6 +46,7 @@ class ReverseOrderListView implements List { final List base; final boolean modifiable; + @SideEffectFree public static List of(List list, boolean modifiable) { if (list instanceof ReverseOrderListView rolv) { return rolv.base; @@ -80,8 +85,13 @@ void checkModifiable() { class DescendingIterator implements Iterator { final ListIterator it = base.listIterator(base.size()); + @Pure public boolean hasNext() { return it.hasPrevious(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next() { return it.previous(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { checkModifiable(); it.remove(); @@ -98,6 +108,7 @@ class DescendingListIterator implements ListIterator { it = base.listIterator(size - pos); } + @Pure public boolean hasNext() { return it.hasPrevious(); } @@ -106,6 +117,7 @@ public E next() { return it.previous(); } + @Pure public boolean hasPrevious() { return it.hasNext(); } @@ -114,10 +126,12 @@ public E previous() { return it.next(); } + @Pure public int nextIndex() { return base.size() - it.nextIndex(); } + @Pure public int previousIndex() { return nextIndex() - 1; } @@ -146,10 +160,12 @@ public void forEach(Consumer action) { action.accept(e); } + @SideEffectFree public Iterator iterator() { return new DescendingIterator(); } + @SideEffectFree public Spliterator spliterator() { // TODO can probably improve this return Spliterators.spliteratorUnknownSize(new DescendingIterator(), 0); @@ -181,15 +197,18 @@ public void clear() { base.clear(); } + @Pure public boolean contains(Object o) { return base.contains(o); } + @Pure public boolean containsAll(Collection c) { return base.containsAll(c); } // copied from AbstractList + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -208,6 +227,7 @@ public boolean equals(Object o) { } // copied from AbstractList + @Pure public int hashCode() { int hashCode = 1; for (E e : this) @@ -215,6 +235,7 @@ public int hashCode() { return hashCode; } + @Pure public boolean isEmpty() { return base.isEmpty(); } @@ -275,6 +296,7 @@ public boolean retainAll(Collection c) { return modified; } + @Pure public int size() { return base.size(); } @@ -283,6 +305,7 @@ public Stream stream() { return StreamSupport.stream(spliterator(), false); } + @SideEffectFree public Object[] toArray() { return ArraysSupport.reverse(base.toArray()); } @@ -297,6 +320,7 @@ public T[] toArray(IntFunction generator) { } // copied from AbstractCollection + @SideEffectFree public String toString() { Iterator it = iterator(); if (! it.hasNext()) @@ -336,17 +360,20 @@ public boolean addAll(int index, Collection c) { } } + @Pure public E get(int i) { int size = base.size(); Objects.checkIndex(i, size); return base.get(size - i - 1); } + @Pure public int indexOf(Object o) { int i = base.lastIndexOf(o); return i == -1 ? -1 : base.size() - i - 1; } + @Pure public int lastIndexOf(Object o) { int i = base.indexOf(o); return i == -1 ? -1 : base.size() - i - 1; @@ -374,6 +401,7 @@ public boolean removeIf(Predicate filter) { return base.removeIf(filter); } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { checkModifiable(); base.replaceAll(operator); @@ -391,6 +419,7 @@ public E set(int index, E element) { return base.set(size - index - 1, element); } + @SideEffectFree public List subList(int fromIndex, int toIndex) { int size = base.size(); Objects.checkFromToIndex(fromIndex, toIndex, size); diff --git a/src/java.base/share/classes/java/util/ReverseOrderSortedMapView.java b/src/java.base/share/classes/java/util/ReverseOrderSortedMapView.java index 404950e8fd54e..ffef3f2a837f9 100644 --- a/src/java.base/share/classes/java/util/ReverseOrderSortedMapView.java +++ b/src/java.base/share/classes/java/util/ReverseOrderSortedMapView.java @@ -25,6 +25,9 @@ package java.util; +import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; + /** * Provides a reversed-ordered view of a SortedMap. Not serializable. * @@ -39,6 +42,7 @@ private ReverseOrderSortedMapView(SortedMap map) { cmp = Collections.reverseOrder(map.comparator()); } + @SideEffectFree public static SortedMap of(SortedMap map) { if (map instanceof ReverseOrderSortedMapView rosmv) { return rosmv.base; @@ -53,6 +57,7 @@ public static SortedMap of(SortedMap map) { // hashCode: inherited from AbstractMap + @SideEffectFree public String toString() { return toString(this, descendingEntryIterator(base)); } @@ -63,18 +68,22 @@ public void clear() { base.clear(); } + @Pure public boolean containsKey(Object key) { return base.containsKey(key); } + @Pure public boolean containsValue(Object value) { return base.containsValue(value); } + @Pure public V get(Object key) { return base.get(key); } + @Pure public boolean isEmpty() { return base.isEmpty(); } @@ -91,38 +100,51 @@ public V remove(Object key) { return base.remove(key); } + @Pure public int size() { return base.size(); } + @SideEffectFree public Set keySet() { return new AbstractSet<>() { // inherit add(), which throws UOE + @SideEffectFree public Iterator iterator() { return descendingKeyIterator(base); } + @Pure public int size() { return base.size(); } public void clear() { base.keySet().clear(); } + @Pure public boolean contains(Object o) { return base.keySet().contains(o); } public boolean remove(Object o) { return base.keySet().remove(o); } }; } + @SideEffectFree public Collection values() { return new AbstractCollection<>() { // inherit add(), which throws UOE + @SideEffectFree public Iterator iterator() { return descendingValueIterator(base); } + @Pure public int size() { return base.size(); } public void clear() { base.values().clear(); } + @Pure public boolean contains(Object o) { return base.values().contains(o); } public boolean remove(Object o) { return base.values().remove(o); } }; } + @SideEffectFree public Set> entrySet() { return new AbstractSet<>() { // inherit add(), which throws UOE + @SideEffectFree public Iterator> iterator() { return descendingEntryIterator(base); } + @Pure public int size() { return base.size(); } public void clear() { base.entrySet().clear(); } + @Pure public boolean contains(Object o) { return base.entrySet().contains(o); } public boolean remove(Object o) { return base.entrySet().remove(o); } }; @@ -168,10 +190,12 @@ public V putLast(K k, V v) { // ========== SortedMap ========== + @Pure public Comparator comparator() { return cmp; } + @SideEffectFree public SortedMap subMap(K fromKey, K toKey) { if (cmp.compare(fromKey, toKey) <= 0) { return new Submap(fromKey, toKey); @@ -180,10 +204,12 @@ public SortedMap subMap(K fromKey, K toKey) { } } + @SideEffectFree public SortedMap headMap(K toKey) { return new Submap(null, toKey); } + @SideEffectFree public SortedMap tailMap(K fromKey) { return new Submap(fromKey, null); } @@ -196,6 +222,7 @@ static Iterator descendingKeyIterator(SortedMap map) { SortedMap view = map; K prev = null; + @Pure public boolean hasNext() { return ! view.isEmpty(); } @@ -223,6 +250,7 @@ static Iterator descendingValueIterator(SortedMap map) { return new Iterator<>() { Iterator keyIterator = descendingKeyIterator(map); + @Pure public boolean hasNext() { return keyIterator.hasNext(); } @@ -241,6 +269,7 @@ static Iterator> descendingEntryIterator(SortedMap return new Iterator<>() { Iterator keyIterator = descendingKeyIterator(map); + @Pure public boolean hasNext() { return keyIterator.hasNext(); } @@ -267,26 +296,32 @@ static class ViewEntry implements Map.Entry { this.value = value; } + @Pure public K getKey() { return key; } + @Pure public V getValue() { return value; } public V setValue(V newValue) { return map.put(key, newValue); } + @Pure public boolean equals(Object o) { return o instanceof Map.Entry e && Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue()); } + @Pure public int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } + @SideEffectFree public String toString() { return key + "=" + value; } } // copied and modified from AbstractMap + @SideEffectFree static String toString(Map thisMap, Iterator> i) { if (! i.hasNext()) return "{}"; @@ -337,6 +372,7 @@ Iterator> entryIterator() { boolean dead = false; Iterator> it = descendingEntryIterator(base); + @Pure public boolean hasNext() { if (dead) return false; @@ -387,16 +423,20 @@ public void remove() { // hashCode: inherited from AbstractMap + @SideEffectFree public String toString() { return ReverseOrderSortedMapView.toString(this, entryIterator()); } + @SideEffectFree public Set> entrySet() { return new AbstractSet<>() { + @SideEffectFree public Iterator> iterator() { return entryIterator(); } + @Pure public int size() { int sz = 0; for (var it = entryIterator(); it.hasNext();) { @@ -424,10 +464,12 @@ public V remove(Object o) { return null; } + @Pure public int size() { return entrySet().size(); } + @Pure public Comparator comparator() { return cmp; } @@ -446,6 +488,7 @@ public K lastKey() { return last.getKey(); } + @SideEffectFree public SortedMap subMap(K from, K to) { if (aboveHead(from) && belowTail(from) && aboveHead(to) && belowTail(to) && @@ -456,6 +499,7 @@ public SortedMap subMap(K from, K to) { } } + @SideEffectFree public SortedMap headMap(K to) { if (aboveHead(to) && belowTail(to)) return new Submap(head, to); @@ -463,6 +507,7 @@ public SortedMap headMap(K to) { throw new IllegalArgumentException(); } + @SideEffectFree public SortedMap tailMap(K from) { if (aboveHead(from) && belowTail(from)) return new Submap(from, tail); diff --git a/src/java.base/share/classes/java/util/ReverseOrderSortedSetView.java b/src/java.base/share/classes/java/util/ReverseOrderSortedSetView.java index 678c82fc311e4..be4109cac72e8 100644 --- a/src/java.base/share/classes/java/util/ReverseOrderSortedSetView.java +++ b/src/java.base/share/classes/java/util/ReverseOrderSortedSetView.java @@ -25,11 +25,14 @@ package java.util; +import org.checkerframework.dataflow.qual.SideEffectFree; + import java.util.function.Consumer; import java.util.function.IntFunction; import java.util.stream.Stream; import java.util.stream.StreamSupport; import jdk.internal.util.ArraysSupport; +import org.checkerframework.dataflow.qual.Pure; /** * Provides a reversed-ordered view of a SortedSet. Not serializable. @@ -43,6 +46,7 @@ private ReverseOrderSortedSetView(SortedSet set) { comp = Collections.reverseOrder(set.comparator()); } + @SideEffectFree public static SortedSet of(SortedSet set) { if (set instanceof ReverseOrderSortedSetView rossv) { return rossv.base; @@ -54,6 +58,7 @@ public static SortedSet of(SortedSet set) { // ========== Object ========== // copied from AbstractSet + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -71,6 +76,7 @@ public boolean equals(Object o) { } // copied from AbstractSet + @Pure public int hashCode() { int h = 0; Iterator i = iterator(); @@ -83,6 +89,7 @@ public int hashCode() { } // copied from AbstractCollection + @SideEffectFree public String toString() { Iterator it = iterator(); if (! it.hasNext()) @@ -106,10 +113,12 @@ public void forEach(Consumer action) { action.accept(e); } + @SideEffectFree public Iterator iterator() { return descendingIterator(base); } + @SideEffectFree public Spliterator spliterator() { return Spliterators.spliteratorUnknownSize(descendingIterator(base), 0); } @@ -129,14 +138,17 @@ public void clear() { base.clear(); } + @Pure public boolean contains(Object o) { return base.contains(o); } + @Pure public boolean containsAll(Collection c) { return base.containsAll(c); } + @Pure public boolean isEmpty() { return base.isEmpty(); } @@ -158,6 +170,7 @@ public boolean retainAll(Collection c) { return base.retainAll(c); } + @Pure public int size() { return base.size(); } @@ -166,6 +179,7 @@ public Stream stream() { return StreamSupport.stream(spliterator(), false); } + @SideEffectFree public Object[] toArray() { return ArraysSupport.reverse(base.toArray()); } @@ -181,6 +195,7 @@ public T[] toArray(IntFunction generator) { // ========== SortedSet ========== + @Pure public Comparator comparator() { return comp; } @@ -189,14 +204,17 @@ public Comparator comparator() { public E last() { return base.first(); } + @SideEffectFree public SortedSet headSet(E to) { return new Subset(null, to); } + @SideEffectFree public SortedSet subSet(E from, E to) { return new Subset(from, to); } + @SideEffectFree public SortedSet tailSet(E from) { return new Subset(from, null); } @@ -209,6 +227,7 @@ static Iterator descendingIterator(SortedSet set) { SortedSet view = set; T prev = null; + @Pure public boolean hasNext() { return ! view.isEmpty(); } @@ -261,12 +280,14 @@ boolean belowTail(E e) { return tail == null || cmp.compare(e, tail) < 0; } + @SideEffectFree public Iterator iterator() { return new Iterator<>() { E cache = null; boolean dead = false; Iterator it = descendingIterator(base); + @Pure public boolean hasNext() { if (dead) return false; @@ -320,6 +341,7 @@ public boolean remove(Object o) { return false; } + @Pure public int size() { int sz = 0; for (E e : this) @@ -327,14 +349,17 @@ public int size() { return sz; } + @Pure public Comparator comparator() { return ReverseOrderSortedSetView.this.comparator(); } + @SideEffectFree public E first() { return this.iterator().next(); } + @SideEffectFree public E last() { var it = this.iterator(); if (! it.hasNext()) @@ -345,6 +370,7 @@ public E last() { return last; } + @SideEffectFree public SortedSet subSet(E from, E to) { if (aboveHead(from) && belowTail(from) && aboveHead(to) && belowTail(to) && @@ -355,6 +381,7 @@ public SortedSet subSet(E from, E to) { } } + @SideEffectFree public SortedSet headSet(E to) { if (aboveHead(to) && belowTail(to)) return ReverseOrderSortedSetView.this.new Subset(head, to); @@ -362,6 +389,7 @@ public SortedSet headSet(E to) { throw new IllegalArgumentException(); } + @SideEffectFree public SortedSet tailSet(E from) { if (aboveHead(from) && belowTail(from)) return ReverseOrderSortedSetView.this.new Subset(null, tail); diff --git a/src/java.base/share/classes/java/util/Scanner.java b/src/java.base/share/classes/java/util/Scanner.java index 4fd3ef0f7b67a..2d030b467120a 100644 --- a/src/java.base/share/classes/java/util/Scanner.java +++ b/src/java.base/share/classes/java/util/Scanner.java @@ -41,6 +41,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.*; import java.math.*; @@ -1491,6 +1492,7 @@ public boolean hasNext(@GuardSatisfied Scanner this) { * @see java.util.Iterator */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public String next(@GuardSatisfied @NonEmpty Scanner this) { ensureOpen(); clearCaches(); @@ -1516,6 +1518,8 @@ public String next(@GuardSatisfied @NonEmpty Scanner this) { * @throws UnsupportedOperationException if this method is invoked. * @see java.util.Iterator */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove(@GuardSatisfied Scanner this) { throw new UnsupportedOperationException(); } @@ -1554,6 +1558,7 @@ public boolean hasNext(@GuardSatisfied Scanner this, String pattern) { * @throws IllegalStateException if this scanner is closed */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public String next(@GuardSatisfied @NonEmpty Scanner this, String pattern) { return next(patternCache.forName(pattern)); } @@ -1605,6 +1610,7 @@ public boolean hasNext(@GuardSatisfied Scanner this, Pattern pattern) { * @throws IllegalStateException if this scanner is closed */ // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public String next(@GuardSatisfied @NonEmpty Scanner this, Pattern pattern) { ensureOpen(); if (pattern == null) diff --git a/src/java.base/share/classes/java/util/SequencedCollection.java b/src/java.base/share/classes/java/util/SequencedCollection.java index 54237c7a3f775..a424be88c5806 100644 --- a/src/java.base/share/classes/java/util/SequencedCollection.java +++ b/src/java.base/share/classes/java/util/SequencedCollection.java @@ -25,6 +25,9 @@ package java.util; +import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; + /** * A collection that has a well-defined encounter order, that supports operations at both ends, * and that is reversible. The elements of a sequenced collection have an @@ -87,6 +90,8 @@ public interface SequencedCollection extends Collection { * * @return a reverse-ordered view of this collection */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") SequencedCollection reversed(); /** @@ -103,6 +108,8 @@ public interface SequencedCollection extends Collection { * @throws UnsupportedOperationException if this collection implementation * does not support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void addFirst(E e) { throw new UnsupportedOperationException(); } @@ -121,6 +128,8 @@ default void addFirst(E e) { * @throws UnsupportedOperationException if this collection implementation * does not support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void addLast(E e) { throw new UnsupportedOperationException(); } @@ -137,6 +146,7 @@ default void addLast(E e) { * @return the retrieved element * @throws NoSuchElementException if this collection is empty */ + @Pure default E getFirst() { return this.iterator().next(); } @@ -153,6 +163,7 @@ default E getFirst() { * @return the retrieved element * @throws NoSuchElementException if this collection is empty */ + @Pure default E getLast() { return this.reversed().iterator().next(); } @@ -172,6 +183,8 @@ default E getLast() { * @throws UnsupportedOperationException if this collection implementation * does not support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeFirst() { var it = this.iterator(); E e = it.next(); @@ -194,6 +207,8 @@ default E removeFirst() { * @throws UnsupportedOperationException if this collection implementation * does not support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeLast() { var it = this.reversed().iterator(); E e = it.next(); diff --git a/src/java.base/share/classes/java/util/SequencedMap.java b/src/java.base/share/classes/java/util/SequencedMap.java index 6bff204b65dc9..5241bcad1d722 100644 --- a/src/java.base/share/classes/java/util/SequencedMap.java +++ b/src/java.base/share/classes/java/util/SequencedMap.java @@ -25,6 +25,9 @@ package java.util; +import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; + import jdk.internal.util.NullableKeyValueHolder; /** @@ -134,6 +137,8 @@ public interface SequencedMap extends Map { * * @return a reverse-ordered view of this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") SequencedMap reversed(); /** @@ -184,6 +189,8 @@ default Map.Entry lastEntry() { * @throws UnsupportedOperationException if this collection implementation does not * support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default Map.Entry pollFirstEntry() { var it = entrySet().iterator(); if (it.hasNext()) { @@ -209,6 +216,8 @@ default Map.Entry pollFirstEntry() { * @throws UnsupportedOperationException if this collection implementation does not * support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default Map.Entry pollLastEntry() { var it = reversed().entrySet().iterator(); if (it.hasNext()) { @@ -235,6 +244,8 @@ default Map.Entry pollLastEntry() { * @throws UnsupportedOperationException if this collection implementation does not * support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default V putFirst(K k, V v) { throw new UnsupportedOperationException(); } @@ -254,6 +265,8 @@ default V putFirst(K k, V v) { * @throws UnsupportedOperationException if this collection implementation does not * support this operation */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default V putLast(K k, V v) { throw new UnsupportedOperationException(); } @@ -272,6 +285,8 @@ default V putLast(K k, V v) { * * @return a {@code SequencedSet} view of this map's {@code keySet} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default SequencedSet sequencedKeySet() { class SeqKeySet extends AbstractMap.ViewCollection implements SequencedSet { Collection view() { @@ -280,9 +295,11 @@ Collection view() { public SequencedSet reversed() { return SequencedMap.this.reversed().sequencedKeySet(); } + @Pure public boolean equals(Object other) { return view().equals(other); } + @Pure public int hashCode() { return view().hashCode(); } @@ -305,6 +322,8 @@ public int hashCode() { * * @return a {@code SequencedCollection} view of this map's {@code values} collection */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default SequencedCollection sequencedValues() { class SeqValues extends AbstractMap.ViewCollection implements SequencedCollection { Collection view() { @@ -331,6 +350,8 @@ public SequencedCollection reversed() { * * @return a {@code SequencedSet} view of this map's {@code entrySet} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default SequencedSet> sequencedEntrySet() { class SeqEntrySet extends AbstractMap.ViewCollection> implements SequencedSet> { @@ -340,9 +361,11 @@ Collection> view() { public SequencedSet> reversed() { return SequencedMap.this.reversed().sequencedEntrySet(); } + @Pure public boolean equals(Object other) { return view().equals(other); } + @Pure public int hashCode() { return view().hashCode(); } diff --git a/src/java.base/share/classes/java/util/SequencedSet.java b/src/java.base/share/classes/java/util/SequencedSet.java index c02bfc123ff8f..6a65d046fee3b 100644 --- a/src/java.base/share/classes/java/util/SequencedSet.java +++ b/src/java.base/share/classes/java/util/SequencedSet.java @@ -25,6 +25,8 @@ package java.util; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; + /** * A collection that is both a {@link SequencedCollection} and a {@link Set}. As such, * it can be thought of either as a {@code Set} that also has a well-defined @@ -54,5 +56,7 @@ public interface SequencedSet extends SequencedCollection, Set { * * @return a reverse-ordered view of this collection, as a {@code SequencedSet} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") SequencedSet reversed(); } diff --git a/src/java.base/share/classes/java/util/ServiceLoader.java b/src/java.base/share/classes/java/util/ServiceLoader.java index c8c5494d0c944..3a43f94a57b30 100644 --- a/src/java.base/share/classes/java/util/ServiceLoader.java +++ b/src/java.base/share/classes/java/util/ServiceLoader.java @@ -34,6 +34,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.BufferedReader; import java.io.IOException; @@ -733,6 +734,7 @@ public Class type() { } @Override + @Pure public S get() { if (factoryMethod != null) { return invokeFactoryMethod(); @@ -832,11 +834,13 @@ public S run() throws Exception { // when running with a security manager. @Override + @Pure public int hashCode() { return Objects.hash(service, type, acc); } @Override + @Pure public boolean equals(Object ob) { return ob instanceof @SuppressWarnings("unchecked")ProviderImpl that && this.service == that.service @@ -979,6 +983,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Provider next(@NonEmpty LayerLookupIterator this) { if (!hasNext()) throw new NoSuchElementException(); @@ -1300,6 +1305,7 @@ public boolean hasNext() { @SuppressWarnings("removal") @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Provider next(@NonEmpty LazyClassPathLookupIterator this) { if (acc == null) { return nextService(); @@ -1331,6 +1337,7 @@ public boolean hasNext() { } @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Provider next(/*@NonEmpty Iterator> this*/) { if (first.hasNext()) { return first.next(); @@ -1420,6 +1427,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public S next(/*@NonEmpty Iterator this*/) { checkReloadCount(); S next; diff --git a/src/java.base/share/classes/java/util/Set.java b/src/java.base/share/classes/java/util/Set.java index 914bfe89d5d7e..54ac68a1f87d1 100644 --- a/src/java.base/share/classes/java/util/Set.java +++ b/src/java.base/share/classes/java/util/Set.java @@ -40,6 +40,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A collection that contains no duplicate elements. More formally, sets @@ -242,7 +243,6 @@ public interface Set extends Collection { * set * @throws NullPointerException if the specified array is null */ - @SideEffectFree @Nullable T [] toArray(@PolyNull T[] a); @@ -468,6 +468,7 @@ public interface Set extends Collection { * @since 1.8 */ @Override + @SideEffectFree default Spliterator spliterator() { return Spliterators.spliterator(this, Spliterator.DISTINCT); } @@ -482,6 +483,7 @@ default Spliterator spliterator() { * @since 9 */ @SuppressWarnings("unchecked") + @SideEffectFree static Set of() { return (Set) ImmutableCollections.EMPTY_SET; } @@ -497,6 +499,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1) { return new ImmutableCollections.Set12<>(e1); } @@ -514,6 +517,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2) { return new ImmutableCollections.Set12<>(e1, e2); } @@ -532,6 +536,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3) { return new ImmutableCollections.SetN<>(e1, e2, e3); } @@ -551,6 +556,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4); } @@ -571,6 +577,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4, E e5) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5); } @@ -592,6 +599,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4, E e5, E e6) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5, e6); @@ -615,6 +623,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5, e6, e7); @@ -639,6 +648,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5, e6, e7, e8); @@ -664,6 +674,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5, e6, e7, e8, e9); @@ -690,6 +701,7 @@ static Set of() { * * @since 9 */ + @SideEffectFree static @NonEmpty Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) { return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); @@ -723,6 +735,7 @@ static Set of() { */ @SafeVarargs @SuppressWarnings("varargs") + @SideEffectFree static Set of(E... elements) { switch (elements.length) { // implicit null check of elements case 0: @@ -756,6 +769,7 @@ static Set of(E... elements) { * @since 10 */ @SuppressWarnings("unchecked") + @SideEffectFree static @PolyNonEmpty Set copyOf(@PolyNonEmpty Collection coll) { if (coll instanceof ImmutableCollections.AbstractImmutableSet) { return (Set)coll; diff --git a/src/java.base/share/classes/java/util/SortedMap.java b/src/java.base/share/classes/java/util/SortedMap.java index 38dd78b2920c2..4051fa9679b42 100644 --- a/src/java.base/share/classes/java/util/SortedMap.java +++ b/src/java.base/share/classes/java/util/SortedMap.java @@ -33,6 +33,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A {@link Map} that further provides a total ordering on its keys. @@ -313,6 +314,8 @@ public interface SortedMap extends SequencedMap { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default V putFirst(K k, V v) { throw new UnsupportedOperationException(); } @@ -328,6 +331,8 @@ default V putFirst(K k, V v) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default V putLast(K k, V v) { throw new UnsupportedOperationException(); } @@ -342,6 +347,8 @@ default V putLast(K k, V v) { * @return a reverse-ordered view of this map, as a {@code SortedMap} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default SortedMap reversed() { return ReverseOrderSortedMapView.of(this); } diff --git a/src/java.base/share/classes/java/util/SortedSet.java b/src/java.base/share/classes/java/util/SortedSet.java index 31b1820d4b85a..e10b5935e9573 100644 --- a/src/java.base/share/classes/java/util/SortedSet.java +++ b/src/java.base/share/classes/java/util/SortedSet.java @@ -32,6 +32,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A {@link Set} that further provides a total ordering on its elements. @@ -268,6 +269,7 @@ public interface SortedSet extends Set, SequencedSet { * @since 1.8 */ @Override + @SideEffectFree default Spliterator spliterator() { return new Spliterators.IteratorSpliterator( this, Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED) { @@ -291,6 +293,8 @@ public Comparator getComparator() { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void addFirst(E e) { throw new UnsupportedOperationException(); } @@ -306,6 +310,8 @@ default void addFirst(E e) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default void addLast(E e) { throw new UnsupportedOperationException(); } @@ -319,6 +325,7 @@ default void addLast(E e) { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure default E getFirst() { return this.first(); } @@ -332,6 +339,7 @@ default E getFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure default E getLast() { return this.last(); } @@ -348,6 +356,8 @@ default E getLast() { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeFirst() { E e = this.first(); this.remove(e); @@ -366,6 +376,8 @@ default E removeFirst() { * @throws UnsupportedOperationException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default E removeLast() { E e = this.last(); this.remove(e); @@ -382,6 +394,8 @@ default E removeLast() { * @return a reverse-ordered view of this collection, as a {@code SortedSet} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") default SortedSet reversed() { return ReverseOrderSortedSetView.of(this); } diff --git a/src/java.base/share/classes/java/util/Spliterators.java b/src/java.base/share/classes/java/util/Spliterators.java index 8213d28fa6eeb..947c091c4b649 100644 --- a/src/java.base/share/classes/java/util/Spliterators.java +++ b/src/java.base/share/classes/java/util/Spliterators.java @@ -29,6 +29,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.function.Consumer; import java.util.function.DoubleConsumer; import java.util.function.IntConsumer; @@ -697,6 +698,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public T next(@NonEmpty Adapter this) { if (!valueReady && !hasNext()) throw new NoSuchElementException(); @@ -759,6 +761,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int nextInt(@NonEmpty Adapter this) { if (!valueReady && !hasNext()) throw new NoSuchElementException(); @@ -817,6 +820,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public long nextLong(@NonEmpty Adapter this) { if (!valueReady && !hasNext()) throw new NoSuchElementException(); @@ -875,6 +879,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public double nextDouble(@NonEmpty Adapter this) { if (!valueReady && !hasNext()) throw new NoSuchElementException(); diff --git a/src/java.base/share/classes/java/util/Stack.java b/src/java.base/share/classes/java/util/Stack.java index 4a960c5b90dbb..5877afa458b70 100644 --- a/src/java.base/share/classes/java/util/Stack.java +++ b/src/java.base/share/classes/java/util/Stack.java @@ -33,6 +33,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * The {@code Stack} class represents a last-in-first-out @@ -75,6 +76,8 @@ public Stack() { * @return the {@code item} argument. * @see java.util.Vector#addElement */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E push(@GuardSatisfied Stack this, E item) { addElement(item); @@ -89,6 +92,8 @@ public E push(@GuardSatisfied Stack this, E item) { * of the {@code Vector} object). * @throws EmptyStackException if this stack is empty. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized E pop(@GuardSatisfied @NonEmpty @CanShrink Stack this) { E obj; int len = size(); diff --git a/src/java.base/share/classes/java/util/TreeMap.java b/src/java.base/share/classes/java/util/TreeMap.java index fb91d84f210b0..e11638f940254 100644 --- a/src/java.base/share/classes/java/util/TreeMap.java +++ b/src/java.base/share/classes/java/util/TreeMap.java @@ -41,6 +41,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.Serializable; import java.util.function.BiConsumer; @@ -309,6 +310,9 @@ public boolean containsValue(@GuardSatisfied TreeMap this, @GuardSatisfied * and this map uses natural ordering, or its comparator * does not permit null keys */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @Pure public @Nullable V get(@GuardSatisfied TreeMap this, @UnknownSignedness @GuardSatisfied Object key) { Entry p = getEntry(key); return (p==null ? null : p.value); @@ -341,6 +345,8 @@ public boolean containsValue(@GuardSatisfied TreeMap this, @GuardSatisfied * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putFirst(K k, V v) { throw new UnsupportedOperationException(); } @@ -353,6 +359,8 @@ public V putFirst(K k, V v) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putLast(K k, V v) { throw new UnsupportedOperationException(); } @@ -369,6 +377,8 @@ public V putLast(K k, V v) { * the specified map contains a null key and this map does not * permit null keys */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(@GuardSatisfied TreeMap this, Map map) { int mapSize = map.size(); if (size==0 && mapSize!=0 && map instanceof SortedMap) { @@ -590,11 +600,15 @@ final Entry getLowerEntry(K key) { * does not permit null keys */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(@GuardSatisfied TreeMap this, K key, V value) { return put(key, value, true); } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putIfAbsent(K key, V value) { return put(key, value, false); } @@ -610,6 +624,7 @@ public V putIfAbsent(K key, V value) { * mapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public V computeIfAbsent(K key, Function mappingFunction) { Objects.requireNonNull(mappingFunction); V newValue; @@ -680,6 +695,7 @@ else if (cmp > 0) * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public V computeIfPresent(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); Entry oldEntry = getEntry(key); @@ -701,6 +717,7 @@ public V computeIfPresent(K key, BiFunction r * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public V compute(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); V newValue; @@ -763,6 +780,7 @@ else if (cmp > 0) * remapping function modified this map */ @Override + @DoesNotUnrefineReceiver("modifiability") public V merge(K key, V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); @@ -937,6 +955,8 @@ private V mergeValue(Entry t, V value, BiFunction this, @GuardSatisfied @UnknownSignedness Object key) { Entry p = getEntry(key); if (p == null) @@ -951,6 +971,8 @@ private V mergeValue(Entry t, V value, BiFunction this) { modCount++; size = 0; @@ -963,6 +985,9 @@ public void clear(@GuardSatisfied TreeMap this) { * * @return a shallow copy of this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @SideEffectFree public Object clone(@GuardSatisfied TreeMap this) { TreeMap clone; try { @@ -1007,6 +1032,8 @@ public Object clone(@GuardSatisfied TreeMap this) { /** * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.@Nullable Entry pollFirstEntry(@GuardSatisfied TreeMap this) { Entry p = getFirstEntry(); Map.Entry result = exportEntry(p); @@ -1018,6 +1045,8 @@ public Object clone(@GuardSatisfied TreeMap this) { /** * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.@Nullable Entry pollLastEntry(@GuardSatisfied TreeMap this) { Entry p = getLastEntry(); Map.Entry result = exportEntry(p); @@ -1150,6 +1179,9 @@ public Object clone(@GuardSatisfied TreeMap this) { * operations. It does not support the {@code add} or {@code addAll} * operations. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") + @SideEffectFree public Set<@KeyFor({"this"}) K> keySet(@GuardSatisfied TreeMap this) { return navigableKeySet(); } @@ -1192,6 +1224,7 @@ public Object clone(@GuardSatisfied TreeMap this) { * {@code retainAll} and {@code clear} operations. It does not * support the {@code add} or {@code addAll} operations. */ + @SideEffectFree public Collection values(@GuardSatisfied TreeMap this) { Collection vs = values; if (vs == null) { @@ -1324,6 +1357,8 @@ public SortedMap tailMap(@GuardSatisfied TreeMap this, K fromKey) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { Entry p = getEntry(key); if (p!=null && Objects.equals(oldValue, p.value)) { @@ -1334,6 +1369,8 @@ public boolean replace(K key, V oldValue, V newValue) { } @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { Entry p = getEntry(key); if (p!=null) { @@ -1358,6 +1395,7 @@ public void forEach(BiConsumer action) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { Objects.requireNonNull(function); int expectedModCount = modCount; @@ -1390,6 +1428,8 @@ public boolean contains(@UnknownSignedness Object o) { return TreeMap.this.containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { for (Entry e = getFirstEntry(); e != null; e = successor(e)) { if (valEquals(e.getValue(), o)) { @@ -1400,6 +1440,8 @@ public boolean remove(@UnknownSignedness Object o) { return false; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { TreeMap.this.clear(); } @@ -1426,6 +1468,8 @@ public boolean contains(@UnknownSignedness Object o) { return p != null && valEquals(p.getValue(), value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { if (!(o instanceof Map.Entry entry)) return false; @@ -1443,6 +1487,8 @@ public boolean remove(@UnknownSignedness Object o) { return TreeMap.this.size(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { TreeMap.this.clear(); } @@ -1496,13 +1542,18 @@ public Iterator descendingIterator() { @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object o) { return m.containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { m.clear(); } public E lower(E e) { return m.lowerKey(e); } public E floor(E e) { return m.floorKey(e); } public E ceiling(E e) { return m.ceilingKey(e); } public E higher(E e) { return m.higherKey(e); } + @SideEffectFree public E first() { return m.firstKey(); } + @SideEffectFree public E last() { return m.lastKey(); } + @Pure public Comparator comparator() { return m.comparator(); } public E pollFirst() { Map.Entry e = m.pollFirstEntry(); @@ -1512,6 +1563,8 @@ public E pollLast() { Map.Entry e = m.pollLastEntry(); return (e == null) ? null : e.getKey(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { int oldSize = size(); m.remove(o); @@ -1522,18 +1575,23 @@ public NavigableSet subSet(E fromElement, boolean fromInclusive, return new KeySet<>(m.subMap(fromElement, fromInclusive, toElement, toInclusive)); } + @SideEffectFree public NavigableSet headSet(E toElement, boolean inclusive) { return new KeySet<>(m.headMap(toElement, inclusive)); } + @SideEffectFree public NavigableSet tailSet(E fromElement, boolean inclusive) { return new KeySet<>(m.tailMap(fromElement, inclusive)); } + @SideEffectFree public SortedSet subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); } + @SideEffectFree public SortedSet headSet(E toElement) { return headSet(toElement, false); } + @SideEffectFree public SortedSet tailSet(E fromElement) { return tailSet(fromElement, true); } @@ -1591,6 +1649,8 @@ final Entry prevEntry() { return e; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastReturned == null) throw new IllegalStateException(); @@ -1609,6 +1669,8 @@ final class EntryIterator extends PrivateEntryIterator> { EntryIterator(Entry first) { super(first); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty EntryIterator this) { return nextEntry(); } @@ -1618,6 +1680,8 @@ final class ValueIterator extends PrivateEntryIterator { ValueIterator(Entry first) { super(first); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(@NonEmpty ValueIterator this) { return nextEntry().value; } @@ -1627,6 +1691,8 @@ final class KeyIterator extends PrivateEntryIterator { KeyIterator(Entry first) { super(first); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty KeyIterator this) { return nextEntry().key; } @@ -1636,9 +1702,13 @@ final class DescendingKeyIterator extends PrivateEntryIterator { DescendingKeyIterator(Entry first) { super(first); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty DescendingKeyIterator this) { return prevEntry().key; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastReturned == null) throw new IllegalStateException(); @@ -1887,24 +1957,30 @@ public final boolean containsKey(@UnknownSignedness Object key) { } @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V put(K key, V value) { if (!inRange(key)) throw new IllegalArgumentException("key out of range"); return m.put(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putIfAbsent(K key, V value) { if (!inRange(key)) throw new IllegalArgumentException("key out of range"); return m.putIfAbsent(key, value); } + @DoesNotUnrefineReceiver("modifiability") public V merge(K key, V value, BiFunction remappingFunction) { if (!inRange(key)) throw new IllegalArgumentException("key out of range"); return m.merge(key, value, remappingFunction); } + @DoesNotUnrefineReceiver("modifiability") public V computeIfAbsent(K key, Function mappingFunction) { if (!inRange(key)) { // Do not throw if mapping function returns null @@ -1915,6 +1991,7 @@ public V computeIfAbsent(K key, Function mappingFunction return m.computeIfAbsent(key, mappingFunction); } + @DoesNotUnrefineReceiver("modifiability") public V compute(K key, BiFunction remappingFunction) { if (!inRange(key)) { // Do not throw if remapping function returns null @@ -1925,14 +2002,18 @@ public V compute(K key, BiFunction remappingF return m.compute(key, remappingFunction); } + @DoesNotUnrefineReceiver("modifiability") public V computeIfPresent(K key, BiFunction remappingFunction) { return !inRange(key) ? null : m.computeIfPresent(key, remappingFunction); } + @Pure public final V get(Object key) { return !inRange(key) ? null : m.get(key); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V remove(Object key) { return !inRange(key) ? null : m.remove(key); } @@ -2013,6 +2094,7 @@ public final NavigableSet navigableKeySet() { (navigableKeySetView = new TreeMap.KeySet<>(this)); } + @SideEffectFree public final Set keySet() { return navigableKeySet(); } @@ -2078,6 +2160,8 @@ public boolean contains(@UnknownSignedness Object o) { valEquals(node.getValue(), entry.getValue()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { if (!(o instanceof Entry entry)) return false; @@ -2171,9 +2255,13 @@ final class SubMapEntryIterator extends SubMapIterator> { TreeMap.Entry fence) { super(first, fence); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty SubMapEntryIterator this) { return nextEntry(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { removeAscending(); } @@ -2185,9 +2273,13 @@ final class DescendingSubMapEntryIterator extends SubMapIterator> super(last, fence); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty DescendingSubMapEntryIterator this) { return prevEntry(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { removeDescending(); } @@ -2200,9 +2292,13 @@ final class SubMapKeyIterator extends SubMapIterator TreeMap.Entry fence) { super(first, fence); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty SubMapKeyIterator this) { return nextEntry().key; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { removeAscending(); } @@ -2238,9 +2334,13 @@ final class DescendingSubMapKeyIterator extends SubMapIterator TreeMap.Entry fence) { super(last, fence); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty DescendingSubMapKeyIterator this) { return prevEntry().key; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { removeDescending(); } @@ -2280,6 +2380,7 @@ static final class AscendingSubMap extends NavigableSubMap { super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); } + @Pure public Comparator comparator() { return m.comparator(); } @@ -2337,6 +2438,7 @@ Iterator descendingKeyIterator() { } final class AscendingEntrySetView extends EntrySetView { + @SideEffectFree public Iterator> iterator() { return new SubMapEntryIterator(absLowest(), absHighFence()); } @@ -2372,6 +2474,7 @@ static final class DescendingSubMap extends NavigableSubMap { private final Comparator reverseComparator = Collections.reverseOrder(m.comparator); + @Pure public Comparator comparator() { return reverseComparator; } @@ -2429,6 +2532,7 @@ Iterator descendingKeyIterator() { } final class DescendingEntrySetView extends EntrySetView { + @SideEffectFree public Iterator> iterator() { return new DescendingSubMapEntryIterator(absHighest(), absLowFence()); } @@ -2482,6 +2586,7 @@ private Object readResolve() { public SortedMap headMap(K toKey) { throw new InternalError(); } @SideEffectFree public SortedMap tailMap(K fromKey) { throw new InternalError(); } + @Pure public Comparator comparator() { throw new InternalError(); } } @@ -2519,6 +2624,7 @@ static final class Entry implements Map.Entry { * * @return the key */ + @Pure public K getKey() { return key; } @@ -2528,6 +2634,7 @@ public K getKey() { * * @return the value associated with the key */ + @Pure public V getValue() { return value; } @@ -2545,18 +2652,21 @@ public V setValue(V value) { return oldValue; } + @Pure public boolean equals(Object o) { return o instanceof Map.Entry e && valEquals(key,e.getKey()) && valEquals(value,e.getValue()); } + @Pure public int hashCode() { int keyHash = (key==null ? 0 : key.hashCode()); int valueHash = (value==null ? 0 : value.hashCode()); return keyHash ^ valueHash; } + @SideEffectFree public String toString() { return key + "=" + value; } diff --git a/src/java.base/share/classes/java/util/TreeSet.java b/src/java.base/share/classes/java/util/TreeSet.java index b4770fab88f6a..9f6529637419a 100644 --- a/src/java.base/share/classes/java/util/TreeSet.java +++ b/src/java.base/share/classes/java/util/TreeSet.java @@ -39,6 +39,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; /** * A {@link NavigableSet} implementation based on a {@link TreeMap}. @@ -209,6 +210,7 @@ public TreeSet(@Nullable Comparator comparator) { * @return an iterator over the elements in this set in descending order * @since 1.6 */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator descendingIterator(@PolyGrowShrink @PolyNonEmpty TreeSet this) { return m.descendingKeySet().iterator(); } @@ -216,6 +218,8 @@ public TreeSet(@Nullable Comparator comparator) { /** * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public NavigableSet descendingSet() { return new TreeSet<>(m.descendingMap()); } @@ -280,6 +284,8 @@ public boolean contains(@GuardSatisfied TreeSet this, @GuardSatisfied @Unknow * does not permit null elements */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(@GuardSatisfied TreeSet this, E e) { return m.put(e, PRESENT)==null; } @@ -301,6 +307,8 @@ public boolean add(@GuardSatisfied TreeSet this, E e) { * and this set uses natural ordering, or its comparator * does not permit null elements */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied TreeSet this, @GuardSatisfied @UnknownSignedness Object o) { return m.remove(o)==PRESENT; } @@ -309,6 +317,8 @@ public boolean remove(@GuardSatisfied TreeSet this, @GuardSatisfied @UnknownS * Removes all of the elements from this set. * The set will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied TreeSet this) { m.clear(); } @@ -324,6 +334,8 @@ public void clear(@GuardSatisfied TreeSet this) { * if any element is null and this set uses natural ordering, or * its comparator does not permit null elements */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied TreeSet this, Collection c) { // Use linear-time version if applicable if (m.size()==0 && c.size() > 0 && @@ -485,6 +497,8 @@ public E last(@GuardSatisfied @NonEmpty TreeSet this) { /** * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst(@GuardSatisfied TreeSet this) { Map.Entry e = m.pollFirstEntry(); return (e == null) ? null : e.getKey(); @@ -493,6 +507,8 @@ public E last(@GuardSatisfied @NonEmpty TreeSet this) { /** * @since 1.6 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast(@GuardSatisfied TreeSet this) { Map.Entry e = m.pollLastEntry(); return (e == null) ? null : e.getKey(); @@ -506,6 +522,8 @@ public E last(@GuardSatisfied @NonEmpty TreeSet this) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { throw new UnsupportedOperationException(); } @@ -518,6 +536,8 @@ public void addFirst(E e) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { throw new UnsupportedOperationException(); } @@ -614,6 +634,7 @@ private void readObject(java.io.ObjectInputStream s) * @return a {@code Spliterator} over the elements in this set * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return TreeMap.keySpliteratorFor(m); } diff --git a/src/java.base/share/classes/java/util/Vector.java b/src/java.base/share/classes/java/util/Vector.java index bc17a9821db60..83d13368bd24f 100644 --- a/src/java.base/share/classes/java/util/Vector.java +++ b/src/java.base/share/classes/java/util/Vector.java @@ -43,6 +43,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.io.ObjectInputStream; @@ -351,10 +352,12 @@ public synchronized boolean isEmpty(@GuardSatisfied Vector this) { * @return an enumeration of the components of this vector * @see Iterator */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Enumeration elements(@PolyGrowShrink @PolyNonEmpty Vector this) { return new Enumeration() { int count = 0; + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasMoreElements() { return count < elementCount; @@ -492,6 +495,7 @@ public boolean contains(@GuardSatisfied Vector this, @GuardSatisfied @Nullabl * @throws ArrayIndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index >= size()}) */ + @Pure public synchronized E elementAt(@NonNegative int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); @@ -548,6 +552,8 @@ public synchronized E lastElement(@NonEmpty Vector this) { * @throws ArrayIndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index >= size()}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void setElementAt(@GuardSatisfied Vector this, E obj, @NonNegative int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + @@ -575,6 +581,8 @@ public synchronized void setElementAt(@GuardSatisfied Vector this, E obj, @No * @throws ArrayIndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index >= size()}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void removeElementAt(@GuardSatisfied @CanShrink Vector this, @NonNegative int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + @@ -615,6 +623,8 @@ else if (index < 0) { * @throws ArrayIndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index > size()}) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void insertElementAt(@GuardSatisfied Vector this, E obj, @NonNegative int index) { if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index @@ -643,6 +653,8 @@ public synchronized void insertElementAt(@GuardSatisfied Vector this, E obj, * * @param obj the component to be added */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void addElement(@GuardSatisfied Vector this, E obj) { modCount++; add(obj, elementData, elementCount); @@ -663,6 +675,8 @@ public synchronized void addElement(@GuardSatisfied Vector this, E obj) { * @return {@code true} if the argument was a component of this * vector; {@code false} otherwise. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized boolean removeElement(@GuardSatisfied @CanShrink Vector this, Object obj) { modCount++; int i = indexOf(obj); @@ -679,6 +693,8 @@ public synchronized boolean removeElement(@GuardSatisfied @CanShrink Vector t *

This method is identical in functionality to the {@link #clear} * method (which is part of the {@link List} interface). */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void removeAllElements(@GuardSatisfied @CanShrink Vector this) { final Object[] es = elementData; for (int to = elementCount, i = elementCount = 0; i < to; i++) @@ -766,6 +782,7 @@ E elementData(int index) { } @SuppressWarnings("unchecked") + @Pure static E elementAt(Object[] es, int index) { return (E) es[index]; } @@ -798,6 +815,8 @@ public synchronized E get(@GuardSatisfied Vector this, @NonNegative int index * ({@code index < 0 || index >= size()}) * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized E set(@GuardSatisfied Vector this, @NonNegative int index, E element) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); @@ -828,6 +847,8 @@ private void add(E e, Object[] elementData, int s) { */ // @SideEffectsOnly("this") @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized boolean add(@GuardSatisfied Vector this, E e) { modCount++; add(e, elementData, elementCount); @@ -845,6 +866,8 @@ public synchronized boolean add(@GuardSatisfied Vector this, E e) { * @return true if the Vector contained the specified element * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @CanShrink Vector this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { return removeElement(o); } @@ -860,6 +883,8 @@ public boolean remove(@GuardSatisfied @CanShrink Vector this, @GuardSatisfied * ({@code index < 0 || index > size()}) * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(@GuardSatisfied Vector this, @NonNegative int index, E element) { insertElementAt(element, index); } @@ -875,6 +900,8 @@ public void add(@GuardSatisfied Vector this, @NonNegative int index, E elemen * ({@code index < 0 || index >= size()}) * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized E remove(@GuardSatisfied @CanShrink Vector this, @NonNegative int index) { modCount++; if (index >= elementCount) @@ -896,6 +923,8 @@ public synchronized E remove(@GuardSatisfied @CanShrink Vector this, @NonNega * * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink Vector this) { removeAllElements(); } @@ -930,6 +959,8 @@ public synchronized boolean containsAll(@GuardSatisfied Vector this, @GuardSa * @throws NullPointerException if the specified collection is null * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(@GuardSatisfied Vector this, Collection c) { Object[] a = c.toArray(); modCount++; @@ -964,6 +995,8 @@ public boolean addAll(@GuardSatisfied Vector this, Collection c) * or if the specified collection is null * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@GuardSatisfied @CanShrink Vector this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -988,6 +1021,8 @@ public boolean removeAll(@GuardSatisfied @CanShrink Vector this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); @@ -998,6 +1033,8 @@ public boolean retainAll(@GuardSatisfied @CanShrink Vector this, Collection this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1067,6 +1104,8 @@ private synchronized boolean bulkRemove(Predicate filter) { * @throws NullPointerException if the specified collection is null * @since 1.2 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized boolean addAll(@GuardSatisfied Vector this, @NonNegative int index, Collection c) { if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index); @@ -1172,6 +1211,8 @@ public synchronized String toString(@GuardSatisfied Vector this) { * This call shortens the list by {@code (toIndex - fromIndex)} elements. * (If {@code toIndex==fromIndex}, this operation has no effect.) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") protected synchronized void removeRange(@GuardSatisfied @CanShrink Vector this, int fromIndex, int toIndex) { modCount++; shiftTailOverGap(elementData, fromIndex, toIndex); @@ -1290,6 +1331,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { synchronized (Vector.this) { checkForComodification(); @@ -1301,6 +1343,8 @@ public E next(@NonEmpty Itr this) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastRet == -1) throw new IllegalStateException(); @@ -1349,14 +1393,17 @@ final class ListItr extends Itr implements ListIterator { cursor = index; } + @Pure public boolean hasPrevious() { return cursor != 0; } + @Pure public int nextIndex() { return cursor; } + @Pure public int previousIndex() { return cursor - 1; } @@ -1372,6 +1419,8 @@ public E previous() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { if (lastRet == -1) throw new IllegalStateException(); @@ -1381,6 +1430,8 @@ public void set(E e) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { int i = cursor; synchronized (Vector.this) { @@ -1397,6 +1448,7 @@ public void add(E e) { * @throws NullPointerException {@inheritDoc} */ @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void forEach(Consumer action) { Objects.requireNonNull(action); final int expectedModCount = modCount; @@ -1413,6 +1465,7 @@ public synchronized void forEach(Consumer action) { */ @SuppressWarnings({"unchecked"}) @Override + @DoesNotUnrefineReceiver("modifiability") public synchronized void replaceAll(UnaryOperator operator) { Objects.requireNonNull(operator); final int expectedModCount = modCount; @@ -1428,6 +1481,8 @@ public synchronized void replaceAll(UnaryOperator operator) { @SuppressWarnings("unchecked") @Override + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public synchronized void sort(Comparator c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, elementCount, c); diff --git a/src/java.base/share/classes/java/util/WeakHashMap.java b/src/java.base/share/classes/java/util/WeakHashMap.java index 775f00f72c3ec..85f6a9be05868 100644 --- a/src/java.base/share/classes/java/util/WeakHashMap.java +++ b/src/java.base/share/classes/java/util/WeakHashMap.java @@ -41,6 +41,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.ref.WeakReference; import java.lang.ref.ReferenceQueue; @@ -448,8 +449,8 @@ public boolean isEmpty(@GuardSatisfied WeakHashMap this) { * @return {@code true} if there is a mapping for {@code key}; * {@code false} otherwise */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public boolean containsKey(@GuardSatisfied WeakHashMap this, @GuardSatisfied @Nullable @UnknownSignedness Object key) { return getEntry(key) != null; } @@ -482,6 +483,8 @@ Entry getEntry(Object key) { * previously associated {@code null} with {@code key}.) */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(@GuardSatisfied WeakHashMap this, K key, V value) { Object k = maskNull(key); int h = hash(k); @@ -574,6 +577,8 @@ private void transfer(Entry[] src, Entry[] dest) { * @param m mappings to be stored in this map. * @throws NullPointerException if the specified map is null. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(@GuardSatisfied WeakHashMap this, Map m) { int numKeysToBeAdded = m.size(); if (numKeysToBeAdded == 0) @@ -623,6 +628,8 @@ public void putAll(@GuardSatisfied WeakHashMap this, Map this, @GuardSatisfied @Nullable @UnknownSignedness Object key) { Object k = maskNull(key); int h = hash(k); @@ -682,6 +689,8 @@ boolean removeMapping(Object o) { * Removes all of the mappings from this map. * The map will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied WeakHashMap this) { // clear out ref queue. We don't need to expunge entries // since table is getting cleared. @@ -754,20 +763,25 @@ private static class Entry extends WeakReference implements Map.Ent } @SuppressWarnings("unchecked") + @Pure public K getKey() { return (K) WeakHashMap.unmaskNull(get()); } + @Pure public V getValue() { return value; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } + @Pure public boolean equals(Object o) { if (!(o instanceof Map.Entry e)) return false; @@ -782,12 +796,14 @@ public boolean equals(Object o) { return false; } + @Pure public int hashCode() { K k = getKey(); V v = getValue(); return Objects.hashCode(k) ^ Objects.hashCode(v); } + @SideEffectFree public String toString() { return getKey() + "=" + getValue(); } @@ -853,6 +869,8 @@ protected Entry nextEntry() { return lastReturned; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastReturned == null) throw new IllegalStateException(); @@ -868,18 +886,24 @@ public void remove() { } private class ValueIterator extends HashIterator { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(@NonEmpty ValueIterator this) { return nextEntry().value; } } private class KeyIterator extends HashIterator { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty KeyIterator this) { return nextEntry().getKey(); } } private class EntryIterator extends HashIterator> { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty EntryIterator this) { return nextEntry(); } @@ -929,6 +953,8 @@ public boolean contains(@Nullable @UnknownSignedness Object o) { return containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { if (containsKey(o)) { WeakHashMap.this.remove(o); @@ -938,6 +964,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { return false; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { WeakHashMap.this.clear(); } @@ -988,6 +1016,8 @@ public boolean contains(@Nullable @UnknownSignedness Object o) { return containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { WeakHashMap.this.clear(); } @@ -1032,6 +1062,8 @@ && getEntry(e.getKey()) != null && getEntry(e.getKey()).equals(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { return removeMapping(o); } @@ -1041,6 +1073,8 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { return WeakHashMap.this.size(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { WeakHashMap.this.clear(); } @@ -1057,7 +1091,6 @@ public Object[] toArray() { return deepCopy().toArray(); } - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { return deepCopy().toArray(a); } @@ -1070,6 +1103,7 @@ public Spliterator> spliterator() { @SuppressWarnings("unchecked") @Override + @DoesNotUnrefineReceiver("modifiability") public void forEach(BiConsumer action) { Objects.requireNonNull(action); int expectedModCount = modCount; @@ -1092,6 +1126,7 @@ public void forEach(BiConsumer action) { @SuppressWarnings("unchecked") @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { Objects.requireNonNull(function); int expectedModCount = modCount; diff --git a/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java b/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java index 1de0feedc38d2..3fe027bafbeac 100644 --- a/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/ArrayBlockingQueue.java @@ -48,8 +48,10 @@ import org.checkerframework.checker.signedness.qual.PolySigned; import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.ref.WeakReference; import java.util.AbstractQueue; @@ -343,6 +345,8 @@ public ArrayBlockingQueue(int capacity, boolean fair, * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return super.add(e); } @@ -356,6 +360,8 @@ public boolean add(E e) { * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { Objects.requireNonNull(e); final ReentrantLock lock = this.lock; @@ -379,6 +385,8 @@ public boolean offer(E e) { * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) throws InterruptedException { Objects.requireNonNull(e); final ReentrantLock lock = this.lock; @@ -400,6 +408,8 @@ public void put(E e) throws InterruptedException { * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { @@ -420,6 +430,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll(@GuardSatisfied @CanShrink ArrayBlockingQueue this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -430,6 +442,8 @@ public E poll(@GuardSatisfied @CanShrink ArrayBlockingQueue this) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink ArrayBlockingQueue this) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); @@ -442,6 +456,8 @@ public E take(@GuardSatisfied @CanShrink ArrayBlockingQueue this) throws Inte } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll(@GuardSatisfied @CanShrink ArrayBlockingQueue this, long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; @@ -527,6 +543,8 @@ public int remainingCapacity() { * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink ArrayBlockingQueue this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o == null) return false; final ReentrantLock lock = this.lock; @@ -596,6 +614,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) * * @return an array containing all of the elements in this queue */ + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(ArrayBlockingQueue<@PolyNull @PolySigned E> this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -670,6 +689,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) } } + @SideEffectFree public String toString() { return Helpers.collectionToString(this); } @@ -678,6 +698,8 @@ public String toString() { * Atomically removes all of the elements from this queue. * The queue will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink ArrayBlockingQueue this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -717,6 +739,8 @@ private static void circularClear(Object[] items, int i, int end) { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink ArrayBlockingQueue this, Collection c) { return drainTo(c, Integer.MAX_VALUE); } @@ -727,6 +751,8 @@ public int drainTo(@GuardSatisfied @CanShrink ArrayBlockingQueue this, Collec * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink ArrayBlockingQueue this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) @@ -779,6 +805,7 @@ else if (i > take) * * @return an iterator over the elements in this queue in proper sequence */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty ArrayBlockingQueue this) { return new Itr(); } @@ -1234,6 +1261,7 @@ private void noNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { final E e = nextItem; if (e == null) @@ -1292,6 +1320,8 @@ public void forEachRemaining(Consumer action) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { final ReentrantLock lock = ArrayBlockingQueue.this.lock; lock.lock(); @@ -1460,6 +1490,7 @@ boolean takeIndexWrapped() { * @return a {@code Spliterator} over the elements in this queue * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return Spliterators.spliterator (this, (Spliterator.ORDERED | @@ -1493,6 +1524,8 @@ public void forEach(Consumer action) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink ArrayBlockingQueue this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1501,6 +1534,8 @@ public boolean removeIf(@CanShrink ArrayBlockingQueue this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1509,6 +1544,8 @@ public boolean removeAll(@CanShrink ArrayBlockingQueue this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); diff --git a/src/java.base/share/classes/java/util/concurrent/BlockingDeque.java b/src/java.base/share/classes/java/util/concurrent/BlockingDeque.java index 81eecd574430a..5f626cc338dbf 100644 --- a/src/java.base/share/classes/java/util/concurrent/BlockingDeque.java +++ b/src/java.base/share/classes/java/util/concurrent/BlockingDeque.java @@ -48,6 +48,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.Deque; import java.util.Iterator; @@ -231,6 +232,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addFirst(E e); /** @@ -246,6 +249,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void addLast(E e); /** @@ -262,6 +267,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offerFirst(E e); /** @@ -278,6 +285,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offerLast(E e); /** @@ -292,6 +301,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void putFirst(E e) throws InterruptedException; /** @@ -306,6 +317,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void putLast(E e) throws InterruptedException; /** @@ -327,6 +340,8 @@ public interface BlockingDeque extends BlockingQueue< * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException; @@ -349,6 +364,8 @@ boolean offerFirst(E e, long timeout, TimeUnit unit) * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException; @@ -359,6 +376,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @return the head of this deque * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E takeFirst(@CanShrink BlockingDeque this) throws InterruptedException; /** @@ -368,6 +387,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @return the tail of this deque * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E takeLast(@CanShrink BlockingDeque this) throws InterruptedException; /** @@ -383,6 +404,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * waiting time elapses before an element is available * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E pollFirst(@CanShrink BlockingDeque this, long timeout, TimeUnit unit) throws InterruptedException; @@ -399,6 +422,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * waiting time elapses before an element is available * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E pollLast(@CanShrink BlockingDeque this, long timeout, TimeUnit unit) throws InterruptedException; @@ -418,6 +443,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @throws NullPointerException if the specified element is null * (optional) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean removeFirstOccurrence(@CanShrink BlockingDeque this, Object o); /** @@ -436,6 +463,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @throws NullPointerException if the specified element is null * (optional) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean removeLastOccurrence(@CanShrink BlockingDeque this, Object o); // *** BlockingQueue methods *** @@ -459,6 +488,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @EnsuresNonEmpty("this") boolean add(E e); @@ -480,6 +511,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offer(E e); /** @@ -497,6 +530,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void put(E e) throws InterruptedException; /** @@ -517,6 +552,8 @@ boolean offerLast(E e, long timeout, TimeUnit unit) * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; @@ -531,6 +568,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E remove(@GuardSatisfied @NonEmpty @CanShrink BlockingDeque this); /** @@ -542,6 +581,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * * @return the head of this deque, or {@code null} if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E poll(@CanShrink BlockingDeque this); /** @@ -554,6 +595,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * @return the head of this deque * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E take(@CanShrink BlockingDeque this) throws InterruptedException; /** @@ -568,6 +611,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * specified waiting time elapses before an element is available * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E poll(@CanShrink BlockingDeque this, long timeout, TimeUnit unit) throws InterruptedException; @@ -582,6 +627,7 @@ boolean offer(E e, long timeout, TimeUnit unit) * @return the head of this deque * @throws NoSuchElementException if this deque is empty */ + @Pure E element(@NonEmpty BlockingDeque this); /** @@ -593,6 +639,7 @@ boolean offer(E e, long timeout, TimeUnit unit) * * @return the head of this deque, or {@code null} if this deque is empty */ + @Pure @Nullable E peek(); /** @@ -614,6 +661,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * @throws NullPointerException if the specified element is null * (optional) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean remove(@CanShrink BlockingDeque this, @UnknownSignedness Object o); /** @@ -665,5 +714,7 @@ boolean offer(E e, long timeout, TimeUnit unit) * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void push(E e); } diff --git a/src/java.base/share/classes/java/util/concurrent/BlockingQueue.java b/src/java.base/share/classes/java/util/concurrent/BlockingQueue.java index f33a527593edb..dc9b4edda9eb8 100644 --- a/src/java.base/share/classes/java/util/concurrent/BlockingQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/BlockingQueue.java @@ -45,6 +45,7 @@ import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.Collection; import java.util.Queue; @@ -208,6 +209,8 @@ public interface BlockingQueue extends Queue { * element prevents it from being added to this queue */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean add(E e); /** @@ -227,6 +230,8 @@ public interface BlockingQueue extends Queue { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offer(E e); /** @@ -241,6 +246,8 @@ public interface BlockingQueue extends Queue { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") void put(E e) throws InterruptedException; /** @@ -261,6 +268,8 @@ public interface BlockingQueue extends Queue { * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; @@ -271,6 +280,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * @return the head of this queue * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") E take() throws InterruptedException; /** @@ -285,6 +296,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * specified waiting time elapses before an element is available * @throws InterruptedException if interrupted while waiting */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable E poll(long timeout, TimeUnit unit) throws InterruptedException; @@ -319,6 +332,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * @throws NullPointerException if the specified element is null * (optional) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean remove(@CanShrink BlockingQueue this, @UnknownSignedness Object o); /** @@ -361,6 +376,8 @@ boolean offer(E e, long timeout, TimeUnit unit) * queue, or some property of an element of this queue prevents * it from being added to the specified collection */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") int drainTo(@GuardSatisfied @CanShrink BlockingQueue this, Collection c); /** @@ -386,5 +403,7 @@ boolean offer(E e, long timeout, TimeUnit unit) * queue, or some property of an element of this queue prevents * it from being added to the specified collection */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") int drainTo(@GuardSatisfied @CanShrink BlockingQueue this, Collection c, int maxElements); } diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java index ef9487f798e25..7035b52234837 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java @@ -52,6 +52,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.ObjectStreamField; import java.io.Serializable; @@ -658,16 +659,23 @@ static class Node implements Map.Entry { this.next = next; } + @Pure public final K getKey() { return key; } + @Pure public final V getValue() { return val; } + @Pure public final int hashCode() { return key.hashCode() ^ val.hashCode(); } + @SideEffectFree public final String toString() { return Helpers.mapEntryToString(key, val); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V setValue(V value) { throw new UnsupportedOperationException(); } + @Pure public final boolean equals(Object o) { Object k, v, u; Map.Entry e; return ((o instanceof Map.Entry) && @@ -983,8 +991,8 @@ else if (eh < 0) * {@code equals} method; {@code false} otherwise * @throws NullPointerException if the specified key is null */ - @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) @Pure + @EnsuresKeyForIf(expression={"#1"}, result=true, map={"this"}) public boolean containsKey(@GuardSatisfied @UnknownSignedness Object key) { return get(key) != null; } @@ -1029,6 +1037,8 @@ public boolean containsValue(@GuardSatisfied @UnknownSignedness Object value) { * @throws NullPointerException if the specified key or value is null */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V put(K key, V value) { return putVal(key, value, false); } @@ -1110,6 +1120,8 @@ else if (f instanceof ReservationNode) * * @param m mappings to be stored in this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(Map m) { tryPresize(m.size()); for (Map.Entry e : m.entrySet()) @@ -1125,6 +1137,8 @@ public void putAll(Map m) { * {@code null} if there was no mapping for {@code key} * @throws NullPointerException if the specified key is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V remove(@GuardSatisfied @UnknownSignedness Object key) { return replaceNode(key, null, null); } @@ -1210,6 +1224,8 @@ else if (f instanceof ReservationNode) /** * Removes all of the mappings from this map. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { long delta = 0L; // negative number of deletions int i = 0; @@ -1323,6 +1339,7 @@ public Collection values() { * * @return the hash code value for this map */ + @Pure public int hashCode() { int h = 0; Node[] t; @@ -1345,6 +1362,7 @@ public int hashCode() { * * @return a string representation of this map */ + @SideEffectFree public String toString() { Node[] t; int f = (t = table) == null ? 0 : t.length; @@ -1570,6 +1588,8 @@ private void readObject(java.io.ObjectInputStream s) * @throws NullPointerException if the specified key or value is null */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V putIfAbsent(K key, V value) { return putVal(key, value, true); } @@ -1579,6 +1599,8 @@ private void readObject(java.io.ObjectInputStream s) * * @throws NullPointerException if the specified key is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @UnknownSignedness Object key, @GuardSatisfied @UnknownSignedness Object value) { if (key == null) throw new NullPointerException(); @@ -1590,6 +1612,8 @@ public boolean remove(@GuardSatisfied @UnknownSignedness Object key, @GuardSatis * * @throws NullPointerException if any of the arguments are null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { if (key == null || oldValue == null || newValue == null) throw new NullPointerException(); @@ -1603,6 +1627,8 @@ public boolean replace(K key, V oldValue, V newValue) { * or {@code null} if there was no mapping for the key * @throws NullPointerException if the specified key or value is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable V replace(K key, V value) { if (key == null || value == null) throw new NullPointerException(); @@ -1628,6 +1654,7 @@ public V getOrDefault(@GuardSatisfied @UnknownSignedness Object key, V defaultVa return (v = get(key)) == null ? defaultValue : v; } + @DoesNotUnrefineReceiver("modifiability") public void forEach(BiConsumer action) { if (action == null) throw new NullPointerException(); Node[] t; @@ -1639,6 +1666,7 @@ public void forEach(BiConsumer action) { } } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { if (function == null) throw new NullPointerException(); Node[] t; @@ -1722,6 +1750,7 @@ boolean removeValueIf(Predicate function) { * @throws RuntimeException or Error if the mappingFunction does so, * in which case the mapping is left unestablished */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { if (key == null || mappingFunction == null) throw new NullPointerException(); @@ -1834,6 +1863,7 @@ else if (f instanceof ReservationNode) * @throws RuntimeException or Error if the remappingFunction does so, * in which case the mapping is unchanged */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { if (key == null || remappingFunction == null) throw new NullPointerException(); @@ -1928,6 +1958,7 @@ else if (f instanceof ReservationNode) * @throws RuntimeException or Error if the remappingFunction does so, * in which case the mapping is unchanged */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { if (key == null || remappingFunction == null) @@ -2057,6 +2088,7 @@ else if (f instanceof ReservationNode) * @throws RuntimeException or Error if the remappingFunction does so, * in which case the mapping is unchanged */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { if (key == null || value == null || remappingFunction == null) throw new NullPointerException(); @@ -2255,6 +2287,7 @@ public static KeySetView newKeySet(int initialCapacity) { * @return the set view * @throws NullPointerException if the mappedValue is null */ + @SideEffectFree public KeySetView keySet(V mappedValue) { if (mappedValue == null) throw new NullPointerException(); @@ -3478,6 +3511,8 @@ static class BaseIterator extends Traverser { @Pure public final boolean hasMoreElements() { return next != null; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void remove() { Node p; if ((p = lastReturned) == null) @@ -3494,6 +3529,8 @@ static final class KeyIterator extends BaseIterator super(tab, size, index, limit, map); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final K next(@NonEmpty KeyIterator this) { Node p; if ((p = next) == null) @@ -3504,6 +3541,8 @@ public final K next(@NonEmpty KeyIterator this) { return k; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final K nextElement(@NonEmpty KeyIterator this) { return next(); } } @@ -3514,6 +3553,8 @@ static final class ValueIterator extends BaseIterator super(tab, size, index, limit, map); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V next(@NonEmpty ValueIterator this) { Node p; if ((p = next) == null) @@ -3524,6 +3565,8 @@ public final V next(@NonEmpty ValueIterator this) { return v; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final V nextElement(@NonEmpty ValueIterator this) { return next(); } } @@ -3534,6 +3577,8 @@ static final class EntryIterator extends BaseIterator super(tab, size, index, limit, map); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final Map.Entry next(@NonEmpty EntryIterator this) { Node p; if ((p = next) == null) @@ -3558,13 +3603,18 @@ static final class MapEntry implements Map.Entry { this.val = val; this.map = map; } + @Pure public K getKey() { return key; } + @Pure public V getValue() { return val; } + @Pure public int hashCode() { return key.hashCode() ^ val.hashCode(); } + @SideEffectFree public String toString() { return Helpers.mapEntryToString(key, val); } + @Pure public boolean equals(Object o) { Object k, v; Map.Entry e; return ((o instanceof Map.Entry) && @@ -3582,6 +3632,8 @@ public boolean equals(Object o) { * could even have been removed, in which case the put will * re-establish). We do not and cannot guarantee more. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V setValue(V value) { if (value == null) throw new NullPointerException(); V v = val; @@ -4473,6 +4525,8 @@ abstract static sealed class CollectionView * Removes all of the elements from this view, by removing all * the mappings from the map backing this view. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void clear() { map.clear(); } @Pure public final int size() { return map.size(); } @@ -4495,6 +4549,8 @@ abstract static sealed class CollectionView @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public abstract boolean contains(@UnknownSignedness Object o); + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public abstract boolean remove(@UnknownSignedness Object o); private static final String OOME_MSG = "Required array size too large"; @@ -4564,6 +4620,7 @@ abstract static sealed class CollectionView * * @return a string representation of this collection */ + @SideEffectFree public final String toString() { StringBuilder sb = new StringBuilder(); sb.append('['); @@ -4591,6 +4648,8 @@ public final boolean containsAll(Collection c) { if (c == null) throw new NullPointerException(); boolean modified = false; @@ -4613,6 +4672,8 @@ public boolean removeAll(Collection c) { if (c == null) throw new NullPointerException(); boolean modified = false; @@ -4677,6 +4738,8 @@ public static final class KeySetView extends CollectionView * @return {@code true} if the backing map contained the specified key * @throws NullPointerException if the specified key is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { return map.remove(o) != null; } /** @@ -4701,6 +4764,8 @@ public Iterator iterator() { * for additions was provided */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(K e) { V v; if ((v = value) == null) @@ -4719,6 +4784,8 @@ public boolean add(K e) { * @throws UnsupportedOperationException if no default mapped value * for additions was provided */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { boolean added = false; V v; @@ -4731,6 +4798,7 @@ public boolean addAll(Collection c) { return added; } + @Pure public int hashCode() { int h = 0; for (K e : this) @@ -4738,6 +4806,7 @@ public int hashCode() { return h; } + @Pure public boolean equals(Object o) { Set c; return ((o instanceof Set) && @@ -4780,6 +4849,8 @@ public final boolean contains(@UnknownSignedness Object o) { return map.containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean remove(@UnknownSignedness Object o) { if (o != null) { for (Iterator it = iterator(); it.hasNext();) { @@ -4801,9 +4872,13 @@ public final Iterator iterator() { } @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean add(V e) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @@ -4820,6 +4895,8 @@ public final boolean addAll(Collection c) { return modified; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { return map.removeValueIf(filter); } @@ -4865,6 +4942,8 @@ public boolean contains(@UnknownSignedness Object o) { (v == r || v.equals(r))); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { Object k, v; Map.Entry e; return ((o instanceof Map.Entry) && @@ -4885,10 +4964,14 @@ public Iterator> iterator() { } @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(Entry e) { return map.putVal(e.getKey(), e.getValue(), false) == null; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection> c) { boolean added = false; for (Entry e : c) { @@ -4898,10 +4981,13 @@ public boolean addAll(Collection> c) { return added; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate> filter) { return map.removeEntryIf(filter); } + @Pure public final int hashCode() { int h = 0; Node[] t; @@ -4914,6 +5000,7 @@ public final int hashCode() { return h; } + @Pure public final boolean equals(Object o) { Set c; return ((o instanceof Set) && diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java index eb7cf2b7696b0..ce2d2a01ebd73 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java @@ -52,6 +52,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; @@ -842,6 +843,8 @@ private void initHeadTail(Node h, Node t) { * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { linkFirst(e); } @@ -855,6 +858,8 @@ public void addFirst(E e) { * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { linkLast(e); } @@ -866,6 +871,8 @@ public void addLast(E e) { * @return {@code true} (as specified by {@link Deque#offerFirst}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerFirst(E e) { linkFirst(e); return true; @@ -880,6 +887,8 @@ public boolean offerFirst(E e) { * @return {@code true} (as specified by {@link Deque#offerLast}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerLast(E e) { linkLast(e); return true; @@ -920,6 +929,7 @@ public boolean offerLast(E e) { /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E getFirst(@NonEmpty ConcurrentLinkedDeque this) { return screenNullResult(peekFirst()); } @@ -927,10 +937,13 @@ public E getFirst(@NonEmpty ConcurrentLinkedDeque this) { /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E getLast(@NonEmpty ConcurrentLinkedDeque this) { return screenNullResult(peekLast()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst(@GuardSatisfied @CanShrink ConcurrentLinkedDeque this) { restart: for (;;) { for (Node first = first(), p = first;;) { @@ -952,6 +965,8 @@ public E getLast(@NonEmpty ConcurrentLinkedDeque this) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast(@GuardSatisfied @CanShrink ConcurrentLinkedDeque this) { restart: for (;;) { for (Node last = last(), p = last;;) { @@ -976,6 +991,8 @@ public E getLast(@NonEmpty ConcurrentLinkedDeque this) { /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink ConcurrentLinkedDeque this) { return screenNullResult(pollFirst()); } @@ -983,6 +1000,8 @@ public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink ConcurrentLinkedDeque< /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast(@GuardSatisfied @NonEmpty @CanShrink ConcurrentLinkedDeque this) { return screenNullResult(pollLast()); } @@ -996,6 +1015,8 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink ConcurrentLinkedDeque this) { return pollFirst(); } @Pure public @Nullable E peek() { return peekFirst(); } @@ -1020,21 +1045,28 @@ public boolean add(E e) { /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@NonEmpty @CanShrink ConcurrentLinkedDeque this) { return removeFirst(); } /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pop(@GuardSatisfied @NonEmpty @CanShrink ConcurrentLinkedDeque this) { return removeFirst(); } /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E element(@NonEmpty ConcurrentLinkedDeque this) { return getFirst(); } /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void push(E e) { addFirst(e); } /** @@ -1049,6 +1081,8 @@ public boolean add(E e) { * @return {@code true} if the deque contained the specified element * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeFirstOccurrence(@CanShrink ConcurrentLinkedDeque this, Object o) { Objects.requireNonNull(o); for (Node p = first(); p != null; p = succ(p)) { @@ -1075,6 +1109,8 @@ public boolean removeFirstOccurrence(@CanShrink ConcurrentLinkedDeque this, O * @return {@code true} if the deque contained the specified element * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeLastOccurrence(@CanShrink ConcurrentLinkedDeque this, Object o) { Objects.requireNonNull(o); for (Node p = last(); p != null; p = pred(p)) { @@ -1167,6 +1203,8 @@ public int size() { * @return {@code true} if the deque contained the specified element * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink ConcurrentLinkedDeque this, @GuardSatisfied @UnknownSignedness Object o) { return removeFirstOccurrence(o); } @@ -1183,6 +1221,8 @@ public boolean remove(@CanShrink ConcurrentLinkedDeque this, @GuardSatisfied * of its elements are null * @throws IllegalArgumentException if the collection is this deque */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { if (c == this) // As historically specified in AbstractQueue#addAll @@ -1237,11 +1277,14 @@ else if (p.prev == p) // NEXT_TERMINATOR /** * Removes all of the elements from this deque. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink ConcurrentLinkedDeque this) { while (pollFirst() != null) ; } + @SideEffectFree public String toString() { String[] a = null; restart: for (;;) { @@ -1353,7 +1396,6 @@ else if (a != null && size <= a.length) { * this deque * @throws NullPointerException if the specified array is null */ - @SideEffectFree @SuppressWarnings("unchecked") public @Nullable T[] toArray(@PolyNull T[] a) { if (a == null) throw new NullPointerException(); @@ -1446,6 +1488,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty AbstractItr this) { E item = nextItem; if (item == null) throw new NoSuchElementException(); @@ -1453,6 +1496,8 @@ public E next(@NonEmpty AbstractItr this) { return item; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { Node l = lastRet; if (l == null) throw new IllegalStateException(); @@ -1465,6 +1510,7 @@ public void remove() { /** Forward iterator */ private class Itr extends AbstractItr { Itr() {} // prevent access constructor creation + @SideEffectFree Node startNode() { return first(); } Node nextNode(Node p) { return succ(p); } } @@ -1472,6 +1518,7 @@ private class Itr extends AbstractItr { /** Descending iterator */ private class DescendingItr extends AbstractItr { DescendingItr() {} // prevent access constructor creation + @SideEffectFree Node startNode() { return last(); } Node nextNode(Node p) { return pred(p); } } @@ -1638,6 +1685,8 @@ private void readObject(java.io.ObjectInputStream s) /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink ConcurrentLinkedDeque this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1646,6 +1695,8 @@ public boolean removeIf(@CanShrink ConcurrentLinkedDeque this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1654,6 +1705,8 @@ public boolean removeAll(@CanShrink ConcurrentLinkedDeque this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java index 3d6a2969ee619..20956d9251c4d 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java @@ -51,6 +51,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; @@ -298,6 +299,8 @@ public ConcurrentLinkedQueue(Collection c) { * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return offer(e); } @@ -370,6 +373,8 @@ private Node skipDeadNodes(Node pred, Node c, Node p, Node q) { * @return {@code true} (as specified by {@link Queue#offer}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { final Node newNode = new Node(Objects.requireNonNull(e)); @@ -399,6 +404,8 @@ else if (p == q) } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink ConcurrentLinkedQueue this) { restartFromHead: for (;;) { for (Node h = head, p = h, q;; p = q) { @@ -543,6 +550,8 @@ public boolean contains(@GuardSatisfied @UnknownSignedness Object o) { * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink ConcurrentLinkedQueue this, @GuardSatisfied @UnknownSignedness Object o) { if (o == null) return false; restartFromHead: for (;;) { @@ -579,6 +588,8 @@ public boolean remove(@CanShrink ConcurrentLinkedQueue this, @GuardSatisfied * of its elements are null * @throws IllegalArgumentException if the collection is this queue */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { if (c == this) // As historically specified in AbstractQueue#addAll @@ -627,6 +638,7 @@ else if (p == q) } } + @SideEffectFree public String toString() { String[] a = null; restartFromHead: for (;;) { @@ -736,7 +748,6 @@ else if (a != null && size <= a.length) { * this queue * @throws NullPointerException if the specified array is null */ - @SideEffectFree @SuppressWarnings("unchecked") public @Nullable T[] toArray(@PolyNull T[] a) { Objects.requireNonNull(a); @@ -803,6 +814,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { final Node pred = nextNode; if (pred == null) throw new NoSuchElementException(); @@ -825,6 +837,8 @@ public E next(@NonEmpty Itr this) { // Default implementation of forEachRemaining is "good enough". + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { Node l = lastRet; if (l == null) throw new IllegalStateException(); @@ -991,6 +1005,8 @@ public Spliterator spliterator() { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink ConcurrentLinkedQueue this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -999,6 +1015,8 @@ public boolean removeIf(@CanShrink ConcurrentLinkedQueue this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1007,11 +1025,15 @@ public boolean removeAll(@CanShrink ConcurrentLinkedQueue this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink ConcurrentLinkedQueue this) { bulkRemove(e -> true); } diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java index 018492522c1a4..c19b3b58ff04f 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java @@ -35,13 +35,14 @@ package java.util.concurrent; -import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.checker.nullness.qual.EnsuresKeyFor; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.PolyNull; +import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.Map; import java.util.Objects; @@ -117,6 +118,7 @@ default V getOrDefault(Object key, V defaultValue) { * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") default void forEach(BiConsumer action) { Objects.requireNonNull(action); for (Map.Entry entry : entrySet()) { @@ -165,6 +167,8 @@ default void forEach(BiConsumer action) { * or value prevents it from being stored in this map */ @EnsuresKeyFor(value={"#1"}, map={"this"}) + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable V putIfAbsent(K key, V value); /** @@ -196,6 +200,8 @@ default void forEach(BiConsumer action) { * and this map does not permit null keys or values * (optional) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value); /** @@ -228,6 +234,8 @@ default void forEach(BiConsumer action) { * @throws IllegalArgumentException if some property of a specified key * or value prevents it from being stored in this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") boolean replace(K key, V oldValue, V newValue); /** @@ -260,6 +268,8 @@ default void forEach(BiConsumer action) { * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") @Nullable V replace(K key, V value); /** @@ -293,6 +303,7 @@ default void forEach(BiConsumer action) { * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") default void replaceAll(BiFunction function) { Objects.requireNonNull(function); forEach((k,v) -> { @@ -333,6 +344,7 @@ default void replaceAll(BiFunction function) * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") default @PolyNull V computeIfAbsent(K key, Function mappingFunction) { Objects.requireNonNull(mappingFunction); @@ -375,6 +387,7 @@ default void replaceAll(BiFunction function) * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") default @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); @@ -423,6 +436,7 @@ default void replaceAll(BiFunction function) * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") default @PolyNull V compute(K key, BiFunction remappingFunction) { retry: for (;;) { @@ -483,6 +497,7 @@ else if ((oldValue = putIfAbsent(key, newValue)) == null) * @since 1.8 */ @Override + @DoesNotUnrefineReceiver("modifiability") default @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java index f201d63bcb851..1ff2b80f20889 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java @@ -157,6 +157,7 @@ ConcurrentNavigableMap subMap(K fromKey, boolean fromInclusive, * * @return a navigable set view of the keys in this map */ + @SideEffectFree NavigableSet keySet(); /** diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java index e5b18d09250d6..5da0d61133791 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java @@ -47,6 +47,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; @@ -1134,6 +1135,7 @@ public ConcurrentSkipListMap(SortedMap m) { * * @return a shallow copy of this map */ + @SideEffectFree public ConcurrentSkipListMap clone() { try { @SuppressWarnings("unchecked") @@ -1322,6 +1324,7 @@ public boolean containsKey(@GuardSatisfied @UnknownSignedness Object key) { * with the keys currently in the map * @throws NullPointerException if the specified key is null */ + @Pure public V get(Object key) { return doGet(key); } @@ -1356,6 +1359,8 @@ public V getOrDefault(@GuardSatisfied @UnknownSignedness Object key, V defaultVa * with the keys currently in the map * @throws NullPointerException if the specified key or value is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V put(K key, V value) { if (value == null) throw new NullPointerException(); @@ -1372,6 +1377,8 @@ public V put(K key, V value) { * with the keys currently in the map * @throws NullPointerException if the specified key is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V remove(@GuardSatisfied @UnknownSignedness Object key) { return doRemove(key, null); } @@ -1427,6 +1434,8 @@ public boolean isEmpty() { /** * Removes all of the mappings from this map. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { Index h, r, d; Node b; VarHandle.acquireFence(); @@ -1472,6 +1481,7 @@ else if ((d = h.down) != null) // remove levels * or the mappingFunction is null * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfAbsent(K key, Function mappingFunction) { if (key == null || mappingFunction == null) @@ -1496,6 +1506,7 @@ else if ((d = h.down) != null) // remove levels * or the remappingFunction is null * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V computeIfPresent(K key, BiFunction remappingFunction) { if (key == null || remappingFunction == null) @@ -1528,6 +1539,7 @@ else if (doRemove(key, v) != null) * or the remappingFunction is null * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V compute(K key, BiFunction remappingFunction) { if (key == null || remappingFunction == null) @@ -1567,6 +1579,7 @@ else if (doRemove(key, v) != null) * or the remappingFunction is null * @since 1.8 */ + @DoesNotUnrefineReceiver("modifiability") public @PolyNull V merge(K key, @NonNull V value, BiFunction remappingFunction) { if (key == null || value == null || remappingFunction == null) @@ -1629,6 +1642,7 @@ else if (doRemove(key, v) != null) * * @return a navigable set view of the keys in this map */ + @SideEffectFree public NavigableSet keySet() { KeySet ks; if ((ks = keySet) != null) return ks; @@ -1661,6 +1675,7 @@ public NavigableSet navigableKeySet() { *

The view's iterators and spliterators are * weakly consistent. */ + @SideEffectFree public Collection values() { Values vs; if ((vs = values) != null) return vs; @@ -1806,6 +1821,8 @@ public boolean equals(@Nullable Object o) { * with the keys currently in the map * @throws NullPointerException if the specified key or value is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putIfAbsent(K key, V value) { if (value == null) throw new NullPointerException(); @@ -1819,6 +1836,8 @@ public V putIfAbsent(K key, V value) { * with the keys currently in the map * @throws NullPointerException if the specified key is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @UnknownSignedness Object key, @GuardSatisfied @UnknownSignedness Object value) { if (key == null) throw new NullPointerException(); @@ -1832,6 +1851,8 @@ public boolean remove(@GuardSatisfied @UnknownSignedness Object key, @GuardSatis * with the keys currently in the map * @throws NullPointerException if any of the arguments are null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { if (key == null || oldValue == null || newValue == null) throw new NullPointerException(); @@ -1857,6 +1878,8 @@ public boolean replace(K key, V oldValue, V newValue) { * with the keys currently in the map * @throws NullPointerException if the specified key or value is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { if (key == null || value == null) throw new NullPointerException(); @@ -1871,6 +1894,7 @@ public V replace(K key, V value) { /* ------ SortedMap API methods ------ */ + @Pure public Comparator comparator() { return comparator; } @@ -1903,6 +1927,8 @@ public K lastKey(@NonEmpty ConcurrentSkipListMap this) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putFirst(K k, V v) { throw new UnsupportedOperationException(); } @@ -1915,6 +1941,8 @@ public V putFirst(K k, V v) { * @throws UnsupportedOperationException always * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putLast(K k, V v) { throw new UnsupportedOperationException(); } @@ -2113,6 +2141,8 @@ public Map.Entry lastEntry() { * The returned entry does not support * the {@code Entry.setValue} method. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry pollFirstEntry() { return doRemoveFirstEntry(); } @@ -2123,6 +2153,8 @@ public Map.Entry pollFirstEntry() { * The returned entry does not support * the {@code Entry.setValue} method. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry pollLastEntry() { return doRemoveLastEntry(); } @@ -2163,6 +2195,8 @@ final void advance(Node b) { next = n; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void remove() { Node n; K k; if ((n = lastReturned) == null || (k = n.key) == null) @@ -2175,6 +2209,8 @@ public final void remove() { } final class ValueIterator extends Iter { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(@NonEmpty ValueIterator this) { V v; if ((v = nextValue) == null) @@ -2185,6 +2221,8 @@ public V next(@NonEmpty ValueIterator this) { } final class KeyIterator extends Iter { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty KeyIterator this) { Node n; if ((n = next) == null) @@ -2196,6 +2234,8 @@ public K next(@NonEmpty KeyIterator this) { } final class EntryIterator extends Iter> { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty EntryIterator this) { Node n; if ((n = next) == null) @@ -2235,28 +2275,41 @@ static final class KeySet @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object o) { return m.containsKey(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { return m.remove(o) != null; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { m.clear(); } public K lower(K e) { return m.lowerKey(e); } public K floor(K e) { return m.floorKey(e); } public K ceiling(K e) { return m.ceilingKey(e); } public K higher(K e) { return m.higherKey(e); } + @Pure public Comparator comparator() { return m.comparator(); } + @SideEffectFree public K first() { return m.firstKey(); } + @SideEffectFree public K last() { return m.lastKey(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K pollFirst() { Map.Entry e = m.pollFirstEntry(); return (e == null) ? null : e.getKey(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K pollLast() { Map.Entry e = m.pollLastEntry(); return (e == null) ? null : e.getKey(); } + @SideEffectFree public Iterator iterator() { return (m instanceof ConcurrentSkipListMap) ? ((ConcurrentSkipListMap)m).new KeyIterator() : ((SubMap)m).new SubMapKeyIterator(); } + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -2269,6 +2322,7 @@ public boolean equals(Object o) { return false; } } + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(KeySet<@PolyNull @PolySigned K,V> this) { return toList(this).toArray(); } public @Nullable T[] toArray(@PolyNull T[] a) { return toList(this).toArray(a); } public Iterator descendingIterator() { @@ -2281,18 +2335,23 @@ public NavigableSet subSet(K fromElement, return new KeySet<>(m.subMap(fromElement, fromInclusive, toElement, toInclusive)); } + @SideEffectFree public NavigableSet headSet(K toElement, boolean inclusive) { return new KeySet<>(m.headMap(toElement, inclusive)); } + @SideEffectFree public NavigableSet tailSet(K fromElement, boolean inclusive) { return new KeySet<>(m.tailMap(fromElement, inclusive)); } + @SideEffectFree public NavigableSet subSet(K fromElement, K toElement) { return subSet(fromElement, true, toElement, false); } + @SideEffectFree public NavigableSet headSet(K toElement) { return headSet(toElement, false); } + @SideEffectFree public NavigableSet tailSet(K fromElement) { return tailSet(fromElement, true); } @@ -2300,6 +2359,7 @@ public NavigableSet descendingSet() { return new KeySet<>(m.descendingMap()); } + @SideEffectFree public Spliterator spliterator() { return (m instanceof ConcurrentSkipListMap) ? ((ConcurrentSkipListMap)m).keySpliterator() @@ -2312,6 +2372,7 @@ static final class Values extends AbstractCollection { Values(ConcurrentNavigableMap map) { m = map; } + @SideEffectFree public Iterator iterator() { return (m instanceof ConcurrentSkipListMap) ? ((ConcurrentSkipListMap)m).new ValueIterator() @@ -2325,16 +2386,22 @@ public Iterator iterator() { @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean contains(@UnknownSignedness Object o) { return m.containsValue(o); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { m.clear(); } + @SideEffectFree public Object[] toArray() { return toList(this).toArray(); } public @Nullable T[] toArray(@PolyNull T[] a) { return toList(this).toArray(a); } + @SideEffectFree public Spliterator spliterator() { return (m instanceof ConcurrentSkipListMap) ? ((ConcurrentSkipListMap)m).valueSpliterator() : ((SubMap)m).new SubMapValueIterator(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { if (filter == null) throw new NullPointerException(); if (m instanceof ConcurrentSkipListMap) @@ -2358,6 +2425,7 @@ static final class EntrySet extends AbstractSet> { EntrySet(ConcurrentNavigableMap map) { m = map; } + @SideEffectFree public Iterator> iterator() { return (m instanceof ConcurrentSkipListMap) ? ((ConcurrentSkipListMap)m).new EntryIterator() @@ -2373,6 +2441,8 @@ public boolean contains(@UnknownSignedness Object o) { V v = m.get(e.getKey()); return v != null && v.equals(e.getValue()); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object o) { if (!(o instanceof Map.Entry)) return false; @@ -2389,9 +2459,12 @@ public boolean isEmpty() { public int size() { return m.size(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { m.clear(); } + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -2404,14 +2477,18 @@ public boolean equals(Object o) { return false; } } + @SideEffectFree public Object[] toArray() { return toList(this).toArray(); } public @Nullable T[] toArray(@PolyNull T[] a) { return toList(this).toArray(a); } + @SideEffectFree public Spliterator> spliterator() { return (m instanceof ConcurrentSkipListMap) ? ((ConcurrentSkipListMap)m).entrySpliterator() : ((SubMap)m).new SubMapEntryIterator(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate> filter) { if (filter == null) throw new NullPointerException(); if (m instanceof ConcurrentSkipListMap) @@ -2693,16 +2770,21 @@ public boolean containsKey(@UnknownSignedness Object key) { return inBounds(key, m.comparator) && m.containsKey(key); } + @Pure public V get(Object key) { if (key == null) throw new NullPointerException(); return (!inBounds(key, m.comparator)) ? null : m.get(key); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V put(K key, V value) { checkKeyBounds(key, m.comparator); return m.put(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V remove(Object key) { return (!inBounds(key, m.comparator)) ? null : m.remove(key); } @@ -2742,6 +2824,8 @@ public boolean containsValue(@UnknownSignedness Object value) { return false; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { Comparator cmp = m.comparator; for (ConcurrentSkipListMap.Node n = loNode(cmp); @@ -2754,20 +2838,28 @@ public void clear() { /* ---------------- ConcurrentMap API methods -------------- */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V putIfAbsent(K key, V value) { checkKeyBounds(key, m.comparator); return m.putIfAbsent(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@UnknownSignedness Object key, @UnknownSignedness Object value) { return inBounds(key, m.comparator) && m.remove(key, value); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean replace(K key, V oldValue, V newValue) { checkKeyBounds(key, m.comparator); return m.replace(key, oldValue, newValue); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V replace(K key, V value) { checkKeyBounds(key, m.comparator); return m.replace(key, value); @@ -2775,6 +2867,7 @@ public V replace(K key, V value) { /* ---------------- SortedMap API methods -------------- */ + @Pure public Comparator comparator() { Comparator cmp = m.comparator(); if (isDescending) @@ -2917,16 +3010,21 @@ public Map.Entry lastEntry() { return isDescending ? lowestEntry() : highestEntry(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry pollFirstEntry() { return isDescending ? removeHighest() : removeLowest(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry pollLastEntry() { return isDescending ? removeLowest() : removeHighest(); } /* ---------------- Submap Views -------------- */ + @SideEffectFree public NavigableSet keySet() { KeySet ks; if ((ks = keySetView) != null) return ks; @@ -2940,6 +3038,7 @@ public NavigableSet navigableKeySet() { return keySetView = new KeySet<>(this); } + @SideEffectFree public Collection values() { Values vs; if ((vs = valuesView) != null) return vs; @@ -3038,6 +3137,8 @@ private void descend() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { Node l = lastReturned; if (l == null) @@ -3070,6 +3171,8 @@ public long estimateSize() { } final class SubMapValueIterator extends SubMapIter { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public V next(@NonEmpty SubMapValueIterator this) { V v = nextValue; advance(); @@ -3081,6 +3184,8 @@ public int characteristics() { } final class SubMapKeyIterator extends SubMapIter { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public K next(@NonEmpty SubMapKeyIterator this) { Node n = next; advance(); @@ -3096,6 +3201,8 @@ public final Comparator getComparator() { } final class SubMapEntryIterator extends SubMapIter> { + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Map.Entry next(@NonEmpty SubMapEntryIterator this) { Node n = next; V v = nextValue; @@ -3110,6 +3217,7 @@ public int characteristics() { // default Map method overrides + @DoesNotUnrefineReceiver("modifiability") public void forEach(BiConsumer action) { if (action == null) throw new NullPointerException(); Node b, n; V v; @@ -3122,6 +3230,7 @@ public void forEach(BiConsumer action) { } } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction function) { if (function == null) throw new NullPointerException(); Node b, n; V v; diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListSet.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListSet.java index 1e02531ced656..b9bd22662ded4 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListSet.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListSet.java @@ -48,6 +48,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.reflect.Field; import java.util.AbstractSet; @@ -261,6 +262,8 @@ public boolean contains(@GuardSatisfied @UnknownSignedness Object o) { * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return m.putIfAbsent(e, Boolean.TRUE) == null; } @@ -279,6 +282,8 @@ public boolean add(E e) { * with the elements currently in this set * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @UnknownSignedness Object o) { return m.remove(o, Boolean.TRUE); } @@ -286,6 +291,8 @@ public boolean remove(@GuardSatisfied @UnknownSignedness Object o) { /** * Removes all of the elements from this set. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { m.clear(); } @@ -354,6 +361,8 @@ public boolean equals(@Nullable Object o) { * @throws NullPointerException if the specified collection or any * of its elements are null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { // Override AbstractSet version to avoid unnecessary call to size() boolean modified = false; @@ -397,11 +406,15 @@ public E higher(E e) { return m.higherKey(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst() { Map.Entry e = m.pollFirstEntry(); return (e == null) ? null : e.getKey(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast() { Map.Entry e = m.pollLastEntry(); return (e == null) ? null : e.getKey(); @@ -418,6 +431,7 @@ public E higher(E e) { /** * @throws java.util.NoSuchElementException {@inheritDoc} */ + @SideEffectFree public E first(@NonEmpty ConcurrentSkipListSet this) { return m.firstKey(); } @@ -425,6 +439,7 @@ public E first(@NonEmpty ConcurrentSkipListSet this) { /** * @throws java.util.NoSuchElementException {@inheritDoc} */ + @SideEffectFree public E last(@NonEmpty ConcurrentSkipListSet this) { return m.lastKey(); } @@ -473,6 +488,7 @@ public void addLast(E e) { * @throws NullPointerException if {@code toElement} is null * @throws IllegalArgumentException {@inheritDoc} */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty NavigableSet headSet(@PolyGrowShrink @PolyNonEmpty ConcurrentSkipListSet this, E toElement, boolean inclusive) { return new ConcurrentSkipListSet(m.headMap(toElement, inclusive)); } @@ -482,6 +498,7 @@ public void addLast(E e) { * @throws NullPointerException if {@code fromElement} is null * @throws IllegalArgumentException {@inheritDoc} */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty NavigableSet tailSet(@PolyGrowShrink @PolyNonEmpty ConcurrentSkipListSet this, E fromElement, boolean inclusive) { return new ConcurrentSkipListSet(m.tailMap(fromElement, inclusive)); } @@ -492,6 +509,7 @@ public void addLast(E e) { * {@code toElement} is null * @throws IllegalArgumentException {@inheritDoc} */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty NavigableSet subSet(@PolyGrowShrink @PolyNonEmpty ConcurrentSkipListSet this, E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); } @@ -501,6 +519,7 @@ public void addLast(E e) { * @throws NullPointerException if {@code toElement} is null * @throws IllegalArgumentException {@inheritDoc} */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty NavigableSet headSet(@PolyGrowShrink @PolyNonEmpty ConcurrentSkipListSet this, E toElement) { return headSet(toElement, false); } @@ -510,6 +529,7 @@ public void addLast(E e) { * @throws NullPointerException if {@code fromElement} is null * @throws IllegalArgumentException {@inheritDoc} */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty NavigableSet tailSet(@PolyGrowShrink @PolyNonEmpty ConcurrentSkipListSet this, E fromElement) { return tailSet(fromElement, true); } diff --git a/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java b/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java index eb3e90f088df8..00dc6b4eecd45 100644 --- a/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java +++ b/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java @@ -50,6 +50,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.VarHandle; import java.lang.reflect.Field; @@ -260,6 +261,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { /** * {@inheritDoc} */ + @Pure public int indexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { Object[] es = getArray(); return indexOfRange(o, es, 0, es.length); @@ -280,6 +282,7 @@ public int indexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * {@code -1} if the element is not found. * @throws IndexOutOfBoundsException if the specified index is negative */ + @Pure public int indexOf(E e, int index) { Object[] es = getArray(); return indexOfRange(e, es, index, es.length); @@ -288,6 +291,7 @@ public int indexOf(E e, int index) { /** * {@inheritDoc} */ + @Pure public int lastIndexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { Object[] es = getArray(); return lastIndexOfRange(o, es, 0, es.length); @@ -309,6 +313,7 @@ public int lastIndexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * @throws IndexOutOfBoundsException if the specified index is greater * than or equal to the current size of this list */ + @Pure public int lastIndexOf(E e, int index) { Object[] es = getArray(); return lastIndexOfRange(e, es, 0, index + 1); @@ -320,6 +325,7 @@ public int lastIndexOf(E e, int index) { * * @return a clone of this list */ + @SideEffectFree public Object clone() { try { @SuppressWarnings("unchecked") @@ -349,6 +355,7 @@ public Object clone() { * * @return an array containing all the elements in this list */ + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(CopyOnWriteArrayList<@PolyNull @PolySigned E> this) { return getArray().clone(); } @@ -408,6 +415,7 @@ public Object clone() { // Positional Access Operations @SuppressWarnings("unchecked") + @Pure static E elementAt(Object[] a, int index) { return (E) a[index]; } @@ -421,6 +429,7 @@ static String outOfBounds(int index, int size) { * * @throws IndexOutOfBoundsException {@inheritDoc} */ + @Pure public E get(int index) { return elementAt(getArray(), index); } @@ -431,6 +440,7 @@ public E get(int index) { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure public E getFirst() { Object[] es = getArray(); if (es.length == 0) @@ -445,6 +455,7 @@ public E getFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + @Pure public E getLast() { Object[] es = getArray(); if (es.length == 0) @@ -459,6 +470,8 @@ public E getLast() { * * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { synchronized (lock) { Object[] es = getArray(); @@ -481,6 +494,8 @@ public E set(int index, E element) { * @return {@code true} (as specified by {@link Collection#add}) */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { synchronized (lock) { Object[] es = getArray(); @@ -499,6 +514,8 @@ public boolean add(E e) { * * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { synchronized (lock) { Object[] es = getArray(); @@ -525,6 +542,8 @@ public void add(int index, E element) { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { add(0, e); } @@ -534,6 +553,8 @@ public void addFirst(E e) { * * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { synchronized (lock) { add(getArray().length, e); @@ -547,6 +568,8 @@ public void addLast(E e) { * * @throws IndexOutOfBoundsException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @CanShrink CopyOnWriteArrayList this, int index) { synchronized (lock) { Object[] es = getArray(); @@ -573,6 +596,8 @@ public E remove(@GuardSatisfied @CanShrink CopyOnWriteArrayList this, int ind * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { synchronized (lock) { if (getArray().length == 0) @@ -588,6 +613,8 @@ public E removeFirst() { * @throws NoSuchElementException {@inheritDoc} * @since 21 */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { synchronized (lock) { int size = getArray().length; @@ -610,6 +637,8 @@ public E removeLast() { * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink CopyOnWriteArrayList this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { Object[] snapshot = getArray(); int index = indexOfRange(o, snapshot, 0, snapshot.length); @@ -690,6 +719,8 @@ void removeRange(@GuardSatisfied @CanShrink CopyOnWriteArrayList this, int fr * @param e element to be added to this list, if absent * @return {@code true} if the element was added */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addIfAbsent(E e) { Object[] snapshot = getArray(); return indexOfRange(e, snapshot, 0, snapshot.length) < 0 @@ -758,6 +789,8 @@ public boolean containsAll(Collection c) { * or if the specified collection is null * @see #remove(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@CanShrink CopyOnWriteArrayList this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -779,6 +812,8 @@ public boolean removeAll(@CanShrink CopyOnWriteArrayList this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); @@ -795,6 +830,8 @@ public boolean retainAll(@GuardSatisfied @CanShrink CopyOnWriteArrayList this * @throws NullPointerException if the specified collection is null * @see #addIfAbsent(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int addAllAbsent(Collection c) { Object[] cs = c.toArray(); if (c.getClass() != ArrayList.class) { @@ -826,6 +863,8 @@ public int addAllAbsent(Collection c) { * Removes all of the elements from this list. * The list will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink CopyOnWriteArrayList this) { synchronized (lock) { setArray(new Object[0]); @@ -842,6 +881,8 @@ public void clear(@GuardSatisfied @CanShrink CopyOnWriteArrayList this) { * @throws NullPointerException if the specified collection is null * @see #add(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ? ((CopyOnWriteArrayList)c).getArray() : c.toArray(); @@ -879,6 +920,8 @@ public boolean addAll(Collection c) { * @throws NullPointerException if the specified collection is null * @see #add(int,Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { Object[] cs = c.toArray(); synchronized (lock) { @@ -919,6 +962,8 @@ public void forEach(Consumer action) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink CopyOnWriteArrayList this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -976,6 +1021,7 @@ boolean bulkRemove(Predicate filter, int i, int end) { } } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { synchronized (lock) { replaceAllRange(operator, 0, getArray().length); @@ -991,6 +1037,8 @@ void replaceAllRange(UnaryOperator operator, int i, int end) { setArray(es); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { synchronized (lock) { sortRange(c, 0, getArray().length); @@ -1064,6 +1112,7 @@ private void readObject(java.io.ObjectInputStream s) * * @return a string representation of this list */ + @SideEffectFree public String toString() { return Arrays.toString(getArray()); } @@ -1115,6 +1164,7 @@ private static int hashCodeOfRange(Object[] es, int from, int to) { * * @return the hash code value for this list */ + @Pure public int hashCode() { Object[] es = getArray(); return hashCodeOfRange(es, 0, es.length); @@ -1130,6 +1180,7 @@ public int hashCode() { * * @return an iterator over the elements in this list in proper sequence */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty CopyOnWriteArrayList this) { return new COWIterator(getArray(), 0); } @@ -1179,6 +1230,7 @@ public int hashCode() { * @return a {@code Spliterator} over the elements in this list * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return Spliterators.spliterator (getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED); @@ -1208,6 +1260,7 @@ public boolean hasPrevious() { @SuppressWarnings("unchecked") // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty COWIterator this) { if (! hasNext()) throw new NoSuchElementException(); @@ -1237,6 +1290,8 @@ public int previousIndex() { * @throws UnsupportedOperationException always; {@code remove} * is not supported by this iterator. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { throw new UnsupportedOperationException(); } @@ -1246,6 +1301,8 @@ public void remove() { * @throws UnsupportedOperationException always; {@code set} * is not supported by this iterator. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { throw new UnsupportedOperationException(); } @@ -1255,6 +1312,8 @@ public void set(E e) { * @throws UnsupportedOperationException always; {@code add} * is not supported by this iterator. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { throw new UnsupportedOperationException(); } @@ -1285,6 +1344,7 @@ public void forEachRemaining(Consumer action) { * @return a view of the specified range within this list * @throws IndexOutOfBoundsException {@inheritDoc} */ + @SideEffectFree public @PolyGrowShrink List subList(@PolyGrowShrink CopyOnWriteArrayList this, int fromIndex, int toIndex) { synchronized (lock) { Object[] es = getArray(); @@ -1337,6 +1397,7 @@ private void rangeCheckForAdd(int index) { throw new IndexOutOfBoundsException(outOfBounds(index, size)); } + @SideEffectFree public Object[] toArray() { final Object[] es; final int offset; @@ -1370,6 +1431,7 @@ public Object[] toArray() { } } + @Pure public int indexOf(Object o) { final Object[] es; final int offset; @@ -1383,6 +1445,7 @@ public int indexOf(Object o) { return (i == -1) ? -1 : i - offset; } + @Pure public int lastIndexOf(Object o) { final Object[] es; final int offset; @@ -1424,10 +1487,12 @@ public boolean isEmpty() { return size() == 0; } + @SideEffectFree public String toString() { return Arrays.toString(toArray()); } + @Pure public int hashCode() { final Object[] es; final int offset; @@ -1440,6 +1505,7 @@ public int hashCode() { return hashCodeOfRange(es, offset, offset + size); } + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -1462,6 +1528,8 @@ public boolean equals(Object o) { return !it.hasNext(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { synchronized (lock) { rangeCheck(index); @@ -1472,6 +1540,7 @@ public E set(int index, E element) { } } + @Pure public E get(int index) { synchronized (lock) { rangeCheck(index); @@ -1490,6 +1559,7 @@ public E getFirst() { } } + @Pure public E getLast() { synchronized (lock) { if (size == 0) @@ -1499,6 +1569,7 @@ public E getLast() { } } + @Pure public int size() { synchronized (lock) { checkForComodification(); @@ -1507,6 +1578,8 @@ public int size() { } @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E element) { synchronized (lock) { checkForComodification(); @@ -1517,6 +1590,8 @@ public boolean add(E element) { return true; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { synchronized (lock) { checkForComodification(); @@ -1527,16 +1602,22 @@ public void add(int index, E element) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { add(0, e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { synchronized (lock) { add(size, e); } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { synchronized (lock) { final Object[] oldArray = getArrayChecked(); @@ -1547,6 +1628,8 @@ public boolean addAll(Collection c) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { synchronized (lock) { rangeCheckForAdd(index); @@ -1558,6 +1641,8 @@ public boolean addAll(int index, Collection c) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { synchronized (lock) { checkForComodification(); @@ -1567,6 +1652,8 @@ public void clear() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { synchronized (lock) { rangeCheck(index); @@ -1578,6 +1665,8 @@ public E remove(int index) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { synchronized (lock) { if (size == 0) @@ -1587,6 +1676,8 @@ public E removeFirst() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { synchronized (lock) { if (size == 0) @@ -1596,6 +1687,8 @@ public E removeLast() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@Nullable @UnknownSignedness Object o) { synchronized (lock) { checkForComodification(); @@ -1607,6 +1700,7 @@ public boolean remove(@Nullable @UnknownSignedness Object o) { } } + @SideEffectFree public Iterator iterator() { return listIterator(0); } @@ -1624,6 +1718,7 @@ public ListIterator listIterator(int index) { } } + @SideEffectFree public List subList(int fromIndex, int toIndex) { synchronized (lock) { checkForComodification(); @@ -1645,6 +1740,7 @@ public void forEach(Consumer action) { action.accept(elementAt(es, i)); } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { synchronized (lock) { checkForComodification(); @@ -1653,6 +1749,8 @@ public void replaceAll(UnaryOperator operator) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { synchronized (lock) { checkForComodification(); @@ -1661,16 +1759,22 @@ public void sort(Comparator c) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1686,6 +1790,7 @@ private boolean bulkRemove(Predicate filter) { } } + @SideEffectFree public Spliterator spliterator() { synchronized (lock) { return Spliterators.spliterator( @@ -1717,6 +1822,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty COWSubListIterator this) { if (hasNext()) return it.next(); @@ -1724,6 +1830,7 @@ public E next(@NonEmpty COWSubListIterator this) { throw new NoSuchElementException(); } + @Pure public boolean hasPrevious() { return previousIndex() >= 0; } @@ -1735,10 +1842,12 @@ public E previous() { throw new NoSuchElementException(); } + @Pure public int nextIndex() { return it.nextIndex() - offset; } + @Pure public int previousIndex() { return it.previousIndex() - offset; } @@ -1798,8 +1907,13 @@ class DescendingIterator implements Iterator { it = base.listIterator(base.size()); } } + @Pure public boolean hasNext() { return it.hasPrevious(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next() { return it.previous(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { it.remove(); } } @@ -1816,14 +1930,18 @@ class DescendingListIterator implements ListIterator { } } + @Pure public boolean hasNext() { return it.hasPrevious(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next() { return it.previous(); } + @Pure public boolean hasPrevious() { return it.hasNext(); } @@ -1832,22 +1950,30 @@ public E previous() { return it.next(); } + @Pure public int nextIndex() { return size - it.nextIndex(); } + @Pure public int previousIndex() { return nextIndex() - 1; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void set(E e) { throw new UnsupportedOperationException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(E e) { throw new UnsupportedOperationException(); } @@ -1860,10 +1986,12 @@ public void forEach(Consumer action) { action.accept(e); } + @SideEffectFree public Iterator iterator() { return new DescendingIterator(); } + @SideEffectFree public Spliterator spliterator() { // TODO can probably improve this return Spliterators.spliteratorUnknownSize(new DescendingIterator(), 0); @@ -1871,11 +1999,15 @@ public Spliterator spliterator() { // ========== Collection ========== + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { base.add(0, e); return true; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { @SuppressWarnings("unchecked") E[] es = (E[]) c.toArray(); @@ -1888,19 +2020,24 @@ public boolean addAll(Collection c) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { base.clear(); } + @Pure public boolean contains(Object o) { return base.contains(o); } + @Pure public boolean containsAll(Collection c) { return base.containsAll(c); } // copied from AbstractList + @Pure public boolean equals(Object o) { if (o == this) return true; @@ -1919,6 +2056,7 @@ public boolean equals(Object o) { } // copied from AbstractList + @Pure public int hashCode() { int hashCode = 1; for (E e : this) @@ -1926,6 +2064,7 @@ public int hashCode() { return hashCode; } + @Pure public boolean isEmpty() { return base.isEmpty(); } @@ -1934,6 +2073,8 @@ public Stream parallelStream() { return StreamSupport.stream(spliterator(), true); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(Object o) { synchronized (lock) { int index = indexOf(o); @@ -1944,14 +2085,19 @@ public boolean remove(Object o) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return base.removeAll(c); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean retainAll(Collection c) { return base.retainAll(c); } + @Pure public int size() { return base.size(); } @@ -1960,6 +2106,7 @@ public Stream stream() { return StreamSupport.stream(spliterator(), false); } + @SideEffectFree public Object[] toArray() { return ArraysSupport.reverse(base.toArray()); } @@ -1975,6 +2122,7 @@ public T[] toArray(IntFunction generator) { } // copied from AbstractCollection + @SideEffectFree public String toString() { Iterator it = iterator(); if (! it.hasNext()) @@ -1993,20 +2141,28 @@ public String toString() { // ========== List ========== + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void add(int index, E element) { synchronized (lock) { base.add(base.size() - index, element); } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { base.add(e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { base.add(0, e); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(int index, Collection c) { @SuppressWarnings("unchecked") E[] es = (E[]) c.toArray(); @@ -2021,12 +2177,14 @@ public boolean addAll(int index, Collection c) { } } + @Pure public E get(int i) { synchronized (lock) { return base.get(base.size() - i - 1); } } + @Pure public E getFirst() { synchronized (lock) { int size = base.size(); @@ -2037,6 +2195,7 @@ public E getFirst() { } } + @Pure public E getLast() { synchronized (lock) { if (base.size() == 0) @@ -2046,6 +2205,7 @@ public E getLast() { } } + @Pure public int indexOf(Object o) { synchronized (lock) { int i = base.lastIndexOf(o); @@ -2053,6 +2213,7 @@ public int indexOf(Object o) { } } + @Pure public int lastIndexOf(Object o) { synchronized (lock) { int i = base.indexOf(o); @@ -2068,12 +2229,16 @@ public ListIterator listIterator(int index) { return new DescendingListIterator(index); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(int index) { synchronized (lock) { return base.remove(base.size() - index - 1); } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst() { synchronized (lock) { int size = base.size(); @@ -2084,6 +2249,8 @@ public E removeFirst() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast() { synchronized (lock) { if (base.size() == 0) @@ -2093,24 +2260,32 @@ public E removeLast() { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { return base.removeIf(filter); } + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(UnaryOperator operator) { base.replaceAll(operator); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void sort(Comparator c) { base.sort(Collections.reverseOrder(c)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E set(int index, E element) { synchronized (lock) { return base.set(base.size() - index - 1, element); } } + @SideEffectFree public List subList(int fromIndex, int toIndex) { synchronized (lock) { int size = base.size(); diff --git a/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArraySet.java b/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArraySet.java index b0078296d26ef..487944eed2ea7 100644 --- a/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArraySet.java +++ b/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArraySet.java @@ -49,6 +49,7 @@ import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.AbstractSet; import java.util.Collection; @@ -193,6 +194,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * * @return an array containing all the elements in this set */ + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(CopyOnWriteArraySet<@PolyNull @PolySigned E> this) { return al.toArray(); } @@ -246,6 +248,8 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * Removes all of the elements from this set. * The set will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { al.clear(); } @@ -261,6 +265,8 @@ public void clear() { * @param o object to be removed from this set, if present * @return {@code true} if this set contained the specified element */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object o) { return al.remove(o); } @@ -278,6 +284,8 @@ public boolean remove(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * element */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return al.addIfAbsent(e); } @@ -348,6 +356,8 @@ private static int compareSets(Object[] snapshot, Set set) { * @throws NullPointerException if the specified collection is null * @see #add(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { return al.addAllAbsent(c) > 0; } @@ -369,6 +379,8 @@ public boolean addAll(Collection c) { * or if the specified collection is null * @see #remove(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(Collection c) { return al.removeAll(c); } @@ -392,6 +404,8 @@ public boolean removeAll(Collection c) { return al.retainAll(c); } @@ -407,6 +421,7 @@ public boolean retainAll(Collection iterator(@PolyGrowShrink @PolyNonEmpty CopyOnWriteArraySet this) { return al.iterator(); } @@ -438,6 +453,8 @@ public boolean equals(@Nullable Object o) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(Predicate filter) { return al.removeIf(filter); } @@ -464,6 +481,7 @@ public void forEach(Consumer action) { * @return a {@code Spliterator} over the elements in this set * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return Spliterators.spliterator (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT); diff --git a/src/java.base/share/classes/java/util/concurrent/DelayQueue.java b/src/java.base/share/classes/java/util/concurrent/DelayQueue.java index 76bdfef2c6f53..4463a14ba3b2d 100644 --- a/src/java.base/share/classes/java/util/concurrent/DelayQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/DelayQueue.java @@ -47,6 +47,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import static java.util.concurrent.TimeUnit.NANOSECONDS; import org.checkerframework.checker.nonempty.qual.PolyNonEmpty; @@ -170,6 +171,8 @@ public DelayQueue(Collection c) { * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return offer(e); } @@ -181,6 +184,8 @@ public boolean add(E e) { * @return {@code true} * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { final ReentrantLock lock = this.lock; lock.lock(); @@ -203,6 +208,8 @@ public boolean offer(E e) { * @param e the element to add * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) { offer(e); } @@ -217,6 +224,8 @@ public void put(E e) { * @return {@code true} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) { return offer(e); } @@ -229,6 +238,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) { * @return the expired head of this queue, or {@code null} if this * queue has no elements with an expired delay */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink DelayQueue this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -250,6 +261,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) { * @return the expired head of this queue * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink DelayQueue this) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); @@ -295,6 +308,8 @@ public E take(@GuardSatisfied @CanShrink DelayQueue this) throws InterruptedE * an expired delay becomes available * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink DelayQueue this, long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; @@ -345,6 +360,8 @@ public E take(@GuardSatisfied @CanShrink DelayQueue this) throws InterruptedE * @throws NoSuchElementException if this queue has no elements with an * expired delay */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove() { return super.remove(); } @@ -386,6 +403,8 @@ public int size() { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink DelayQueue this, Collection c) { return drainTo(c, Integer.MAX_VALUE); } @@ -396,6 +415,8 @@ public int drainTo(@GuardSatisfied @CanShrink DelayQueue this, Collection this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) @@ -426,6 +447,8 @@ public int drainTo(@GuardSatisfied @CanShrink DelayQueue this, Collection this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -505,7 +528,6 @@ public int remainingCapacity() { * this queue * @throws NullPointerException if the specified array is null */ - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { final ReentrantLock lock = this.lock; lock.lock(); @@ -520,6 +542,8 @@ public int remainingCapacity() { * Removes a single instance of the specified element from this * queue, if it is present, whether or not it has expired. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink DelayQueue this, @UnknownSignedness Object o) { final ReentrantLock lock = this.lock; lock.lock(); @@ -584,12 +608,15 @@ public boolean hasNext() { @SuppressWarnings("unchecked") // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { if (cursor >= array.length) throw new NoSuchElementException(); return (E)array[lastRet = cursor++]; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastRet < 0) throw new IllegalStateException(); diff --git a/src/java.base/share/classes/java/util/concurrent/LinkedBlockingDeque.java b/src/java.base/share/classes/java/util/concurrent/LinkedBlockingDeque.java index 186e62bcf1813..f928042e2d12e 100644 --- a/src/java.base/share/classes/java/util/concurrent/LinkedBlockingDeque.java +++ b/src/java.base/share/classes/java/util/concurrent/LinkedBlockingDeque.java @@ -48,8 +48,10 @@ import org.checkerframework.checker.signedness.qual.PolySigned; import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.AbstractQueue; import java.util.Collection; @@ -331,6 +333,8 @@ void unlink(@CanShrink LinkedBlockingDeque this, Node x) { * @throws IllegalStateException if this deque is full * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addFirst(E e) { if (!offerFirst(e)) throw new IllegalStateException("Deque full"); @@ -340,6 +344,8 @@ public void addFirst(E e) { * @throws IllegalStateException if this deque is full * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void addLast(E e) { if (!offerLast(e)) throw new IllegalStateException("Deque full"); @@ -348,6 +354,8 @@ public void addLast(E e) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerFirst(E e) { if (e == null) throw new NullPointerException(); Node node = new Node(e); @@ -363,6 +371,8 @@ public boolean offerFirst(E e) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerLast(E e) { if (e == null) throw new NullPointerException(); Node node = new Node(e); @@ -379,6 +389,8 @@ public boolean offerLast(E e) { * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putFirst(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); Node node = new Node(e); @@ -396,6 +408,8 @@ public void putFirst(E e) throws InterruptedException { * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putLast(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); Node node = new Node(e); @@ -413,6 +427,8 @@ public void putLast(E e) throws InterruptedException { * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); @@ -436,6 +452,8 @@ public boolean offerFirst(E e, long timeout, TimeUnit unit) * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); @@ -458,6 +476,8 @@ public boolean offerLast(E e, long timeout, TimeUnit unit) /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque this) { E x = pollFirst(); if (x == null) throw new NoSuchElementException(); @@ -467,12 +487,16 @@ public E removeFirst(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E removeLast(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque this) { E x = pollLast(); if (x == null) throw new NoSuchElementException(); return x; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst(@GuardSatisfied @CanShrink LinkedBlockingDeque this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -483,6 +507,8 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast(@GuardSatisfied @CanShrink LinkedBlockingDeque this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -493,6 +519,8 @@ public E removeLast(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E takeFirst(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lock(); @@ -506,6 +534,8 @@ public E takeFirst(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throw } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E takeLast(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lock(); @@ -519,6 +549,8 @@ public E takeLast(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollFirst(@GuardSatisfied @CanShrink LinkedBlockingDeque this, long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); @@ -537,6 +569,8 @@ public E takeLast(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E pollLast(@GuardSatisfied @CanShrink LinkedBlockingDeque this, long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); @@ -558,6 +592,7 @@ public E takeLast(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E getFirst(@NonEmpty LinkedBlockingDeque this) { E x = peekFirst(); if (x == null) throw new NoSuchElementException(); @@ -567,6 +602,7 @@ public E getFirst(@NonEmpty LinkedBlockingDeque this) { /** * @throws NoSuchElementException {@inheritDoc} */ + @Pure public E getLast(@NonEmpty LinkedBlockingDeque this) { E x = peekLast(); if (x == null) throw new NoSuchElementException(); @@ -595,6 +631,8 @@ public E getLast(@NonEmpty LinkedBlockingDeque this) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeFirstOccurrence(@CanShrink LinkedBlockingDeque this, Object o) { if (o == null) return false; final ReentrantLock lock = this.lock; @@ -612,6 +650,8 @@ public boolean removeFirstOccurrence(@CanShrink LinkedBlockingDeque this, Obj } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeLastOccurrence(@CanShrink LinkedBlockingDeque this, Object o) { if (o == null) return false; final ReentrantLock lock = this.lock; @@ -642,6 +682,8 @@ public boolean removeLastOccurrence(@CanShrink LinkedBlockingDeque this, Obje * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { addLast(e); return true; @@ -650,6 +692,8 @@ public boolean add(E e) { /** * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { return offerLast(e); } @@ -658,6 +702,8 @@ public boolean offer(E e) { * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) throws InterruptedException { putLast(e); } @@ -666,6 +712,8 @@ public void put(E e) throws InterruptedException { * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { return offerLast(e, timeout, unit); @@ -681,18 +729,26 @@ public boolean offer(E e, long timeout, TimeUnit unit) * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E remove(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque this) { return removeFirst(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink LinkedBlockingDeque this) { return pollFirst(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws InterruptedException { return takeFirst(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink LinkedBlockingDeque this, long timeout, TimeUnit unit) throws InterruptedException { return pollFirst(timeout, unit); } @@ -707,6 +763,7 @@ public E take(@GuardSatisfied @CanShrink LinkedBlockingDeque this) throws Int * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ + @Pure public E element(@NonEmpty LinkedBlockingDeque this) { return getFirst(); } @@ -743,6 +800,8 @@ public int remainingCapacity() { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingDeque this, Collection c) { return drainTo(c, Integer.MAX_VALUE); } @@ -753,6 +812,8 @@ public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingDeque this, Colle * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingDeque this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) @@ -779,6 +840,8 @@ public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingDeque this, Colle * @throws IllegalStateException if this deque is full * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void push(E e) { addFirst(e); } @@ -786,6 +849,8 @@ public void push(E e) { /** * @throws NoSuchElementException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E pop(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque this) { return removeFirst(); } @@ -806,6 +871,8 @@ public E pop(@GuardSatisfied @NonEmpty @CanShrink LinkedBlockingDeque this) { * @param o element to be removed from this deque, if present * @return {@code true} if this deque changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink LinkedBlockingDeque this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { return removeFirstOccurrence(o); } @@ -864,6 +931,8 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * @throws IllegalStateException if this deque is full * @see #add(Object) */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean addAll(Collection c) { if (c == this) // As historically specified in AbstractQueue#addAll @@ -924,6 +993,7 @@ public boolean addAll(Collection c) { * @return an array containing all of the elements in this deque */ @SuppressWarnings("unchecked") + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(LinkedBlockingDeque<@PolyNull @PolySigned E> this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -993,6 +1063,7 @@ public boolean addAll(Collection c) { } } + @SideEffectFree public String toString() { return Helpers.collectionToString(this); } @@ -1001,6 +1072,8 @@ public String toString() { * Atomically removes all of the elements from this deque. * The deque will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink LinkedBlockingDeque this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -1041,6 +1114,7 @@ Node succ(Node p) { * * @return an iterator over the elements in this deque in proper sequence */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty LinkedBlockingDeque this) { return new Itr(); } @@ -1110,6 +1184,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty AbstractItr this) { Node p; if ((p = next) == null) @@ -1170,6 +1245,8 @@ public void forEachRemaining(Consumer action) { } while (n > 0 && p != null); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { Node n = lastRet; if (n == null) @@ -1308,6 +1385,7 @@ public int characteristics() { * @return a {@code Spliterator} over the elements in this deque * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return new LBDSpliterator(); } @@ -1357,6 +1435,8 @@ void forEachFrom(Consumer action, Node p) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink LinkedBlockingDeque this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1365,6 +1445,8 @@ public boolean removeIf(@CanShrink LinkedBlockingDeque this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1373,6 +1455,8 @@ public boolean removeAll(@CanShrink LinkedBlockingDeque this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); diff --git a/src/java.base/share/classes/java/util/concurrent/LinkedBlockingQueue.java b/src/java.base/share/classes/java/util/concurrent/LinkedBlockingQueue.java index dd3c0dacf9991..5faa7a4ad1e82 100644 --- a/src/java.base/share/classes/java/util/concurrent/LinkedBlockingQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/LinkedBlockingQueue.java @@ -47,8 +47,10 @@ import org.checkerframework.checker.signedness.qual.PolySigned; import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.AbstractQueue; import java.util.Collection; @@ -340,6 +342,8 @@ public int remainingCapacity() { * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); final int c; @@ -379,6 +383,8 @@ public void put(E e) throws InterruptedException { * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { @@ -417,6 +423,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; @@ -441,6 +449,8 @@ public boolean offer(E e) { return true; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink LinkedBlockingQueue this) throws InterruptedException { final E x; final int c; @@ -463,6 +473,8 @@ public E take(@GuardSatisfied @CanShrink LinkedBlockingQueue this) throws Int return x; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink LinkedBlockingQueue this, long timeout, TimeUnit unit) throws InterruptedException { final E x; final int c; @@ -488,6 +500,8 @@ public E take(@GuardSatisfied @CanShrink LinkedBlockingQueue this) throws Int return x; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink LinkedBlockingQueue this) { final AtomicInteger count = this.count; if (count.get() == 0) @@ -552,6 +566,8 @@ void unlink(Node p, Node pred) { * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink LinkedBlockingQueue this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o == null) return false; fullyLock(); @@ -606,6 +622,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * * @return an array containing all of the elements in this queue */ + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(LinkedBlockingQueue<@PolyNull @PolySigned E> this) { fullyLock(); try { @@ -675,6 +692,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { } } + @SideEffectFree public String toString() { return Helpers.collectionToString(this); } @@ -683,6 +701,8 @@ public String toString() { * Atomically removes all of the elements from this queue. * The queue will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink LinkedBlockingQueue this) { fullyLock(); try { @@ -705,6 +725,8 @@ public void clear(@GuardSatisfied @CanShrink LinkedBlockingQueue this) { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingQueue this, Collection c) { return drainTo(c, Integer.MAX_VALUE); } @@ -715,6 +737,8 @@ public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingQueue this, Colle * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink LinkedBlockingQueue this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) @@ -775,6 +799,7 @@ Node succ(Node p) { * * @return an iterator over the elements in this queue in proper sequence */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty LinkedBlockingQueue this) { return new Itr(); } @@ -809,6 +834,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E next(@NonEmpty Itr this) { Node p; if ((p = next) == null) @@ -867,6 +893,8 @@ public void forEachRemaining(Consumer action) { } while (n > 0 && p != null); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { Node p = lastRet; if (p == null) @@ -992,6 +1020,7 @@ public int characteristics() { * @return a {@code Spliterator} over the elements in this queue * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return new LBQSpliterator(); } @@ -1040,6 +1069,8 @@ void forEachFrom(Consumer action, Node p) { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink LinkedBlockingQueue this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1048,6 +1079,8 @@ public boolean removeIf(@CanShrink LinkedBlockingQueue this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1056,6 +1089,8 @@ public boolean removeAll(@CanShrink LinkedBlockingQueue this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); diff --git a/src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java b/src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java index 1526ac207fd3f..ce7772bf79352 100644 --- a/src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java @@ -48,7 +48,9 @@ import org.checkerframework.checker.signedness.qual.PolySigned; import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; @@ -751,6 +753,7 @@ private int countOfMode(boolean data) { } } + @SideEffectFree public String toString() { String[] a = null; restartFromHead: for (;;) { @@ -826,6 +829,7 @@ else if (a != null && size <= a.length) { * * @return an array containing all of the elements in this queue */ + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(LinkedTransferQueue<@PolyNull @PolySigned E> this) { return toArrayInternal(null); } @@ -924,6 +928,7 @@ public final boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final E next(@NonEmpty Itr this) { final Node p; if ((p = nextNode) == null) throw new NoSuchElementException(); @@ -941,6 +946,8 @@ public void forEachRemaining(Consumer action) { lastRet = q; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public final void remove() { final Node lastRet = this.lastRet; if (lastRet == null) @@ -1096,6 +1103,7 @@ public int characteristics() { * @return a {@code Spliterator} over the elements in this queue * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return new LTQSpliterator(); } @@ -1202,6 +1210,8 @@ public LinkedTransferQueue(Collection c) { * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) { xfer(e, true, ASYNC, 0L); } @@ -1215,6 +1225,8 @@ public void put(E e) { * {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) { xfer(e, true, ASYNC, 0L); return true; @@ -1227,6 +1239,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) { * @return {@code true} (as specified by {@link Queue#offer}) * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { xfer(e, true, ASYNC, 0L); return true; @@ -1241,6 +1255,8 @@ public boolean offer(E e) { * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { xfer(e, true, ASYNC, 0L); return true; @@ -1256,6 +1272,8 @@ public boolean add(E e) { * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean tryTransfer(@GuardSatisfied @CanShrink LinkedTransferQueue this, E e) { return xfer(e, true, NOW, 0L) == null; } @@ -1271,6 +1289,8 @@ public boolean tryTransfer(@GuardSatisfied @CanShrink LinkedTransferQueue thi * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void transfer(@GuardSatisfied @CanShrink LinkedTransferQueue this, E e) throws InterruptedException { if (xfer(e, true, SYNC, 0L) != null) { Thread.interrupted(); // failure possible only due to interrupt @@ -1292,6 +1312,8 @@ public void transfer(@GuardSatisfied @CanShrink LinkedTransferQueue this, E e * * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean tryTransfer(@GuardSatisfied @CanShrink LinkedTransferQueue this, E e, long timeout, TimeUnit unit) throws InterruptedException { if (xfer(e, true, TIMED, unit.toNanos(timeout)) == null) @@ -1301,6 +1323,8 @@ public boolean tryTransfer(@GuardSatisfied @CanShrink LinkedTransferQueue thi throw new InterruptedException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink LinkedTransferQueue this) throws InterruptedException { E e = xfer(null, false, SYNC, 0L); if (e != null) @@ -1309,6 +1333,8 @@ public E take(@GuardSatisfied @CanShrink LinkedTransferQueue this) throws Int throw new InterruptedException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll(@GuardSatisfied @CanShrink LinkedTransferQueue this, long timeout, TimeUnit unit) throws InterruptedException { E e = xfer(null, false, TIMED, unit.toNanos(timeout)); if (e != null || !Thread.interrupted()) @@ -1316,6 +1342,8 @@ public E poll(@GuardSatisfied @CanShrink LinkedTransferQueue this, long timeo throw new InterruptedException(); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll(@GuardSatisfied @CanShrink LinkedTransferQueue this) { return xfer(null, false, NOW, 0L); } @@ -1324,6 +1352,8 @@ public E poll(@GuardSatisfied @CanShrink LinkedTransferQueue this) { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink LinkedTransferQueue this, Collection c) { Objects.requireNonNull(c); if (c == this) @@ -1338,6 +1368,8 @@ public int drainTo(@GuardSatisfied @CanShrink LinkedTransferQueue this, Colle * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink LinkedTransferQueue this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) @@ -1357,6 +1389,7 @@ public int drainTo(@GuardSatisfied @CanShrink LinkedTransferQueue this, Colle * * @return an iterator over the elements in this queue in proper sequence */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty LinkedTransferQueue this) { return new Itr(); } @@ -1441,6 +1474,8 @@ public int getWaitingConsumerCount() { * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink LinkedTransferQueue this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o == null) return false; restartFromHead: for (;;) { @@ -1561,6 +1596,8 @@ private void readObject(java.io.ObjectInputStream s) /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink LinkedTransferQueue this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1569,6 +1606,8 @@ public boolean removeIf(@CanShrink LinkedTransferQueue this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1577,11 +1616,15 @@ public boolean removeAll(@CanShrink LinkedTransferQueue this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink LinkedTransferQueue this) { bulkRemove(e -> true); } diff --git a/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java b/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java index f369b7286da03..fe1a1cef9473f 100644 --- a/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java @@ -48,8 +48,10 @@ import org.checkerframework.checker.signedness.qual.PolySigned; import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; @@ -463,6 +465,8 @@ private void heapify() { * @throws NullPointerException if the specified element is null */ @EnsuresNonEmpty("this") + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean add(E e) { return offer(e); } @@ -478,6 +482,8 @@ public boolean add(E e) { * priority queue's ordering * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { if (e == null) throw new NullPointerException(); @@ -511,6 +517,8 @@ public boolean offer(E e) { * priority queue's ordering * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) { offer(e); // never need to block } @@ -530,10 +538,14 @@ public void put(E e) { * priority queue's ordering * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) { return offer(e); // never need to block } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink PriorityBlockingQueue this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -544,6 +556,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) { } } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink PriorityBlockingQueue this) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); @@ -557,6 +571,8 @@ public E take(@GuardSatisfied @CanShrink PriorityBlockingQueue this) throws I return result; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public @Nullable E poll(@GuardSatisfied @CanShrink PriorityBlockingQueue this, long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; @@ -591,6 +607,7 @@ public E take(@GuardSatisfied @CanShrink PriorityBlockingQueue this) throws I * or {@code null} if this queue uses the natural * ordering of its elements */ + @Pure public Comparator comparator() { return comparator; } @@ -615,6 +632,7 @@ public int remainingCapacity() { return Integer.MAX_VALUE; } + @Pure private int indexOf(@GuardSatisfied @Nullable @UnknownSignedness Object o) { if (o != null) { final Object[] es = queue; @@ -662,6 +680,8 @@ private void removeAt(int i) { * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink PriorityBlockingQueue this, @Nullable @UnknownSignedness Object o) { final ReentrantLock lock = this.lock; lock.lock(); @@ -717,6 +737,7 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { } } + @SideEffectFree public String toString() { return Helpers.collectionToString(this); } @@ -727,6 +748,8 @@ public String toString() { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink PriorityBlockingQueue this, Collection c) { return drainTo(c, Integer.MAX_VALUE); } @@ -737,6 +760,8 @@ public int drainTo(@GuardSatisfied @CanShrink PriorityBlockingQueue this, Col * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink PriorityBlockingQueue this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) @@ -761,6 +786,8 @@ public int drainTo(@GuardSatisfied @CanShrink PriorityBlockingQueue this, Col * Atomically removes all of the elements from this queue. * The queue will be empty after this call returns. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink PriorityBlockingQueue this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -787,6 +814,7 @@ public void clear(@GuardSatisfied @CanShrink PriorityBlockingQueue this) { * * @return an array containing all of the elements in this queue */ + @SideEffectFree public @PolyNull @PolySigned Object[] toArray(PriorityBlockingQueue<@PolyNull @PolySigned E> this) { final ReentrantLock lock = this.lock; lock.lock(); @@ -859,6 +887,7 @@ public void clear(@GuardSatisfied @CanShrink PriorityBlockingQueue this) { * * @return an iterator over the elements in this queue */ + @SideEffectFree public @PolyGrowShrink @PolyNonEmpty Iterator iterator(@PolyGrowShrink @PolyNonEmpty PriorityBlockingQueue this) { return new Itr(toArray()); } @@ -888,6 +917,8 @@ public E next(@NonEmpty Itr this) { return (E)array[lastRet = cursor++]; } + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (lastRet < 0) throw new IllegalStateException(); @@ -1026,6 +1057,7 @@ public int characteristics() { * @return a {@code Spliterator} over the elements in this queue * @since 1.8 */ + @SideEffectFree public Spliterator spliterator() { return new PBQSpliterator(); } @@ -1033,6 +1065,8 @@ public Spliterator spliterator() { /** * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeIf(@CanShrink PriorityBlockingQueue this, Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -1041,6 +1075,8 @@ public boolean removeIf(@CanShrink PriorityBlockingQueue this, Predicate this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -1049,6 +1085,8 @@ public boolean removeAll(@CanShrink PriorityBlockingQueue this, Collection this, Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); diff --git a/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java b/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java index b873fb89529b3..6015ffd22f0bd 100644 --- a/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java +++ b/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java @@ -43,6 +43,7 @@ import org.checkerframework.checker.signedness.qual.UnknownSignedness; import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; @@ -1023,6 +1024,7 @@ private void grow() { /** * Finds index of given object, or -1 if absent. */ + @Pure private int indexOf(Object x) { if (x != null) { if (x instanceof ScheduledFutureTask) { @@ -1352,6 +1354,7 @@ public boolean hasNext() { } // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Runnable next(@NonEmpty Itr this) { if (cursor >= array.length) throw new NoSuchElementException(); diff --git a/src/java.base/share/classes/java/util/concurrent/SynchronousQueue.java b/src/java.base/share/classes/java/util/concurrent/SynchronousQueue.java index e29caabac9291..45ee3254e1d17 100644 --- a/src/java.base/share/classes/java/util/concurrent/SynchronousQueue.java +++ b/src/java.base/share/classes/java/util/concurrent/SynchronousQueue.java @@ -49,6 +49,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; @@ -849,6 +850,8 @@ public SynchronousQueue(boolean fair) { * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); if (transferer.transfer(e, false, 0) == null) { @@ -866,6 +869,8 @@ public void put(E e) throws InterruptedException { * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); @@ -885,6 +890,8 @@ public boolean offer(E e, long timeout, TimeUnit unit) * {@code false} * @throws NullPointerException if the specified element is null */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean offer(E e) { if (e == null) throw new NullPointerException(); return transferer.transfer(e, true, 0) != null; @@ -897,6 +904,8 @@ public boolean offer(E e) { * @return the head of this queue * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E take(@GuardSatisfied @CanShrink SynchronousQueue this) throws InterruptedException { E e = transferer.transfer(null, false, 0); if (e != null) @@ -914,6 +923,8 @@ public E take(@GuardSatisfied @CanShrink SynchronousQueue this) throws Interr * specified waiting time elapses before an element is present * @throws InterruptedException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll(@GuardSatisfied @CanShrink SynchronousQueue this, long timeout, TimeUnit unit) throws InterruptedException { E e = transferer.transfer(null, true, unit.toNanos(timeout)); if (e != null || !Thread.interrupted()) @@ -928,6 +939,8 @@ public E poll(@GuardSatisfied @CanShrink SynchronousQueue this, long timeout, * @return the head of this queue, or {@code null} if no * element is available */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public E poll(@GuardSatisfied @CanShrink SynchronousQueue this) { return transferer.transfer(null, true, 0); } @@ -969,6 +982,8 @@ public int remainingCapacity() { * Does nothing. * A {@code SynchronousQueue} has no internal capacity. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear(@GuardSatisfied @CanShrink SynchronousQueue this) { } @@ -992,6 +1007,8 @@ public boolean contains(@GuardSatisfied @Nullable @UnknownSignedness Object o) { * @param o the element to remove * @return {@code false} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean remove(@CanShrink SynchronousQueue this, @GuardSatisfied @Nullable @UnknownSignedness Object o) { return false; } @@ -1015,6 +1032,8 @@ public boolean containsAll(Collection c) { * @param c the collection * @return {@code false} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public boolean removeAll(@CanShrink SynchronousQueue this, Collection c) { return false; } @@ -1026,6 +1045,8 @@ public boolean removeAll(@CanShrink SynchronousQueue this, Collection this, Collection c) { return false; } @@ -1082,7 +1103,6 @@ public Spliterator spliterator() { * @return the specified array * @throws NullPointerException if the specified array is null */ - @SideEffectFree public @Nullable T[] toArray(@PolyNull T[] a) { if (a.length > 0) a[0] = null; @@ -1093,6 +1113,7 @@ public Spliterator spliterator() { * Always returns {@code "[]"}. * @return {@code "[]"} */ + @SideEffectFree public String toString() { return "[]"; } @@ -1103,6 +1124,8 @@ public String toString() { * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink SynchronousQueue this, Collection c) { Objects.requireNonNull(c); if (c == this) @@ -1119,6 +1142,8 @@ public int drainTo(@GuardSatisfied @CanShrink SynchronousQueue this, Collecti * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public int drainTo(@GuardSatisfied @CanShrink SynchronousQueue this, Collection c, int maxElements) { Objects.requireNonNull(c); if (c == this) diff --git a/src/java.base/share/classes/java/util/jar/Attributes.java b/src/java.base/share/classes/java/util/jar/Attributes.java index 02c4f6c37edfb..6821c0c013f26 100644 --- a/src/java.base/share/classes/java/util/jar/Attributes.java +++ b/src/java.base/share/classes/java/util/jar/Attributes.java @@ -34,6 +34,7 @@ import org.checkerframework.dataflow.qual.Pure; import org.checkerframework.dataflow.qual.SideEffectFree; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; @@ -123,6 +124,7 @@ public Attributes(Attributes attr) { * @return the value of the specified attribute name, or null if * not found. */ + @Pure public Object get(Object name) { return map.get(name); } @@ -142,6 +144,7 @@ public Object get(Object name) { * not found. * @throws IllegalArgumentException if the attribute name is invalid */ + @Pure public String getValue(String name) { return (String)get(Name.of(name)); } @@ -159,6 +162,7 @@ public String getValue(String name) { * @return the String value of the specified Attribute.Name, or null if * not found. */ + @Pure public String getValue(Name name) { return (String)get(name); } @@ -174,6 +178,8 @@ public String getValue(Name name) { * @throws ClassCastException if the name is not a Attributes.Name * or the value is not a String */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Object put(Object name, Object value) { return map.put((Attributes.Name)name, (String)value); } @@ -205,6 +211,8 @@ public String putValue(String name, String value) { * @param name attribute name * @return the previous value of the attribute, or null if none */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public Object remove(@GuardSatisfied @Nullable @UnknownSignedness Object name) { return map.remove(name); } @@ -240,6 +248,8 @@ public boolean containsKey(@GuardSatisfied @Nullable @UnknownSignedness Object n * @param attr the Attributes to be stored in this map * @throws ClassCastException if attr is not an Attributes */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void putAll(Map attr) { // ## javac bug? if (!Attributes.class.isInstance(attr)) @@ -251,6 +261,8 @@ public void putAll(Map attr) { /** * Removes all attributes from this Map. */ + // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public void clear() { map.clear(); } @@ -275,6 +287,7 @@ public boolean isEmpty() { /** * Returns a Set view of the attribute names (keys) contained in this Map. */ + @SideEffectFree public Set keySet() { return map.keySet(); } @@ -282,6 +295,7 @@ public Set keySet() { /** * Returns a Collection view of the attribute values contained in this Map. */ + @SideEffectFree public Collection values() { return map.values(); } @@ -313,6 +327,7 @@ public boolean equals(@Nullable Object o) { /** * Returns the hash code value for this Map. */ + @Pure public int hashCode() { return map.hashCode(); } @@ -326,6 +341,7 @@ public int hashCode() { * the Attributes returned can be safely modified without affecting * the original. */ + @SideEffectFree public Object clone() { return new Attributes(this); } @@ -488,6 +504,7 @@ public static class Name { */ private static @Stable Map KNOWN_NAMES; + @SideEffectFree static final Name of(String name) { Name n = KNOWN_NAMES.get(name); if (n != null) { @@ -540,6 +557,7 @@ private final int hash(String name) { * @return true if this attribute name is equal to the * specified attribute object */ + @Pure public boolean equals(Object o) { if (this == o) { return true; @@ -551,6 +569,7 @@ public boolean equals(Object o) { /** * Computes the hash value for this attribute name. */ + @Pure public int hashCode() { return hashCode; } @@ -558,6 +577,7 @@ public int hashCode() { /** * Returns the attribute name as a String. */ + @SideEffectFree public String toString() { return name; } diff --git a/src/java.base/share/classes/java/util/regex/Matcher.java b/src/java.base/share/classes/java/util/regex/Matcher.java index 5377c2d28dd4e..85b57adefe8e6 100644 --- a/src/java.base/share/classes/java/util/regex/Matcher.java +++ b/src/java.base/share/classes/java/util/regex/Matcher.java @@ -37,6 +37,7 @@ import org.checkerframework.dataflow.qual.SideEffectFree; // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.IOException; import java.util.ConcurrentModificationException; @@ -1356,6 +1357,7 @@ class MatchResultIterator implements Iterator { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public MatchResult next(@NonEmpty MatchResultIterator this) { if (expectedCount >= 0 && expectedCount != modCount) throw new ConcurrentModificationException(); diff --git a/src/java.base/share/classes/java/util/regex/Pattern.java b/src/java.base/share/classes/java/util/regex/Pattern.java index ff9bff4f486d8..bdd31f0d613f4 100644 --- a/src/java.base/share/classes/java/util/regex/Pattern.java +++ b/src/java.base/share/classes/java/util/regex/Pattern.java @@ -38,6 +38,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.text.Normalizer; import java.text.Normalizer.Form; @@ -6086,6 +6087,7 @@ class MatcherIterator implements Iterator { private int emptyElementCount; // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public String next(@NonEmpty MatcherIterator this) { if (!hasNext()) throw new NoSuchElementException(); diff --git a/src/java.base/share/classes/java/util/stream/SpinedBuffer.java b/src/java.base/share/classes/java/util/stream/SpinedBuffer.java index 98bbc040a7b49..5ea5f3a3246cf 100644 --- a/src/java.base/share/classes/java/util/stream/SpinedBuffer.java +++ b/src/java.base/share/classes/java/util/stream/SpinedBuffer.java @@ -24,6 +24,8 @@ */ package java.util.stream; +import org.checkerframework.dataflow.qual.SideEffectFree; + import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -37,6 +39,7 @@ import java.util.function.IntConsumer; import java.util.function.IntFunction; import java.util.function.LongConsumer; +import org.checkerframework.dataflow.qual.Pure; /** * An ordered collection of elements. Elements can be added, but not removed. @@ -157,6 +160,7 @@ protected void increaseCapacity() { /** * Retrieve the element at the specified index. */ + @Pure public E get(long index) { // @@@ can further optimize by caching last seen spineIndex, // which is going to be right most of the time @@ -234,6 +238,7 @@ public void clear() { } @Override + @SideEffectFree public Iterator iterator() { return Spliterators.iterator(spliterator()); } @@ -264,6 +269,7 @@ public void accept(E e) { } @Override + @SideEffectFree public String toString() { List list = new ArrayList<>(); forEach(list::add); @@ -276,6 +282,7 @@ public String toString() { /** * Return a {@link Spliterator} describing the contents of the buffer. */ + @SideEffectFree public Spliterator spliterator() { class Splitr implements Spliterator { // The current spine index @@ -462,6 +469,7 @@ abstract static class OfPrimitive } @Override + @SideEffectFree public abstract Iterator iterator(); @Override @@ -770,6 +778,7 @@ public void accept(int i) { curChunk[elementIndex++] = i; } + @Pure public int get(long index) { // Casts to int are safe since the spine array index is the index minus // the prior element count from the current spine @@ -781,10 +790,12 @@ public int get(long index) { } @Override + @SideEffectFree public PrimitiveIterator.OfInt iterator() { return Spliterators.iterator(spliterator()); } + @SideEffectFree public Spliterator.OfInt spliterator() { @SuppressWarnings("overloads") class Splitr extends BaseSpliterator @@ -816,6 +827,7 @@ Spliterator.OfInt arraySpliterator(int[] array, int offset, int len) { } @Override + @SideEffectFree public String toString() { int[] array = asPrimitiveArray(); if (array.length < 200) { @@ -885,6 +897,7 @@ public void accept(long i) { curChunk[elementIndex++] = i; } + @Pure public long get(long index) { // Casts to int are safe since the spine array index is the index minus // the prior element count from the current spine @@ -896,11 +909,12 @@ public long get(long index) { } @Override + @SideEffectFree public PrimitiveIterator.OfLong iterator() { return Spliterators.iterator(spliterator()); } - + @SideEffectFree public Spliterator.OfLong spliterator() { @SuppressWarnings("overloads") class Splitr extends BaseSpliterator @@ -932,6 +946,7 @@ Spliterator.OfLong arraySpliterator(long[] array, int offset, int len) { } @Override + @SideEffectFree public String toString() { long[] array = asPrimitiveArray(); if (array.length < 200) { @@ -1002,6 +1017,7 @@ public void accept(double i) { curChunk[elementIndex++] = i; } + @Pure public double get(long index) { // Casts to int are safe since the spine array index is the index minus // the prior element count from the current spine @@ -1013,10 +1029,12 @@ public double get(long index) { } @Override + @SideEffectFree public PrimitiveIterator.OfDouble iterator() { return Spliterators.iterator(spliterator()); } + @SideEffectFree public Spliterator.OfDouble spliterator() { @SuppressWarnings("overloads") class Splitr extends BaseSpliterator @@ -1048,6 +1066,7 @@ Spliterator.OfDouble arraySpliterator(double[] array, int offset, int len) { } @Override + @SideEffectFree public String toString() { double[] array = asPrimitiveArray(); if (array.length < 200) { diff --git a/src/java.base/share/classes/java/util/stream/Stream.java b/src/java.base/share/classes/java/util/stream/Stream.java index 7193a7d7e86e5..4e592f283465f 100644 --- a/src/java.base/share/classes/java/util/stream/Stream.java +++ b/src/java.base/share/classes/java/util/stream/Stream.java @@ -922,7 +922,6 @@ default Stream dropWhile(Predicate predicate) { * stream is not assignable to the {@linkplain Class#getComponentType * runtime component type} of the generated array */ - @SideEffectFree A[] toArray(IntFunction generator); /** diff --git a/src/java.base/share/classes/java/util/zip/ZipFile.java b/src/java.base/share/classes/java/util/zip/ZipFile.java index 57d85f5f03bd4..715c037467e4e 100644 --- a/src/java.base/share/classes/java/util/zip/ZipFile.java +++ b/src/java.base/share/classes/java/util/zip/ZipFile.java @@ -39,6 +39,7 @@ // import org.checkerframework.dataflow.qual.SideEffectsOnly; import org.checkerframework.framework.qual.AnnotatedFor; import org.checkerframework.framework.qual.CFComment; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.io.Closeable; import java.io.InputStream; @@ -509,6 +510,7 @@ public ZipEntryIterator(int entryCount) { } @Override + @Pure @EnsuresNonEmptyIf(result = true, expression = "this") public boolean hasMoreElements() { return hasNext(); @@ -523,6 +525,7 @@ public boolean hasNext() { @Override // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public T nextElement(@NonEmpty ZipEntryIterator this) { return next(); } @@ -530,6 +533,7 @@ public T nextElement(@NonEmpty ZipEntryIterator this) { @Override @SuppressWarnings("unchecked") // @SideEffectsOnly("this") + @DoesNotUnrefineReceiver("modifiability") public T next(@NonEmpty ZipEntryIterator this) { synchronized (ZipFile.this) { ensureOpen(); @@ -1022,6 +1026,7 @@ public int available() { return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem; } + @Pure public long size() { return size; } @@ -1269,12 +1274,14 @@ public Key(File file, BasicFileAttributes attrs, ZipCoder zc) { this.utf8 = zc.isUTF8(); } + @Pure public int hashCode() { long t = utf8 ? 0 : Long.MAX_VALUE; t += attrs.lastModifiedTime().toMillis(); return ((int)(t ^ (t >>> 32))) + file.hashCode(); } + @Pure public boolean equals(Object obj) { if (obj instanceof Key key) { if (key.utf8 != utf8) { diff --git a/src/java.base/share/classes/jdk/internal/org/objectweb/asm/tree/InsnList.java b/src/java.base/share/classes/jdk/internal/org/objectweb/asm/tree/InsnList.java index c81d8775f6705..a6038701a7242 100644 --- a/src/java.base/share/classes/jdk/internal/org/objectweb/asm/tree/InsnList.java +++ b/src/java.base/share/classes/jdk/internal/org/objectweb/asm/tree/InsnList.java @@ -60,6 +60,7 @@ package jdk.internal.org.objectweb.asm.tree; import org.checkerframework.dataflow.qual.Pure; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.ListIterator; import java.util.NoSuchElementException; import jdk.internal.org.objectweb.asm.MethodVisitor; @@ -543,6 +544,7 @@ public boolean hasNext() { } @Override + @DoesNotUnrefineReceiver("modifiability") public Object next() { if (nextInsn == null) { throw new NoSuchElementException(); @@ -555,6 +557,7 @@ public Object next() { } @Override + @DoesNotUnrefineReceiver("modifiability") public void remove() { if (remove != null) { if (remove == nextInsn) { @@ -609,6 +612,7 @@ public int previousIndex() { } @Override + @DoesNotUnrefineReceiver("modifiability") public void add(final Object o) { if (nextInsn != null) { InsnList.this.insertBefore(nextInsn, (AbstractInsnNode) o); @@ -622,6 +626,7 @@ public void add(final Object o) { } @Override + @DoesNotUnrefineReceiver("modifiability") public void set(final Object o) { if (remove != null) { InsnList.this.set(remove, (AbstractInsnNode) o); diff --git a/src/java.base/share/classes/sun/net/www/HeaderParser.java b/src/java.base/share/classes/sun/net/www/HeaderParser.java index 61c08cfdb6d37..6e6fff53188fe 100644 --- a/src/java.base/share/classes/sun/net/www/HeaderParser.java +++ b/src/java.base/share/classes/sun/net/www/HeaderParser.java @@ -27,6 +27,7 @@ import org.checkerframework.dataflow.qual.Pure; // import org.checkerframework.dataflow.qual.SideEffectsOnly; +import org.checkerframework.framework.qual.DoesNotUnrefineReceiver; import java.util.Iterator; import java.util.Locale; @@ -209,9 +210,11 @@ public boolean hasNext () { return index>> entrySet() { } @Override + @DoesNotUnrefineReceiver("modifiability") public void replaceAll(BiFunction, ? extends List> function) { var f = function.andThen(values -> { Objects.requireNonNull(values);