Skip to content

Commit 962f4d5

Browse files
committed
Merge commit '1973fa0811588dd0bb025fdc99345cdb887b3b52' of https://github.com/typetools/jdk into typetools-3.40.0-fixes
2 parents 1fc2ede + 1973fa0 commit 962f4d5

File tree

6 files changed

+79
-34
lines changed

6 files changed

+79
-34
lines changed

src/java.base/share/classes/java/util/Map.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? s
641641
* @since 17
642642
*/
643643
@SuppressWarnings("unchecked")
644-
public static <K, V> Map.Entry<K, V> copyOf(Map.Entry<? extends K, ? extends V> e) {
644+
public static <K extends @NonNull Object, V extends @NonNull Object> Map.Entry<K, V> copyOf(Map.Entry<? extends K, ? extends V> e) {
645645
Objects.requireNonNull(e);
646646
if (e instanceof KeyValueHolder) {
647647
return (Map.Entry<K, V>) e;
@@ -1395,7 +1395,7 @@ static <K, V> Map<K, V> of() {
13951395
*
13961396
* @since 9
13971397
*/
1398-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1) {
1398+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1) {
13991399
return new ImmutableCollections.Map1<>(k1, v1);
14001400
}
14011401

@@ -1415,7 +1415,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1) {
14151415
*
14161416
* @since 9
14171417
*/
1418-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2) {
1418+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2) {
14191419
return new ImmutableCollections.MapN<>(k1, v1, k2, v2);
14201420
}
14211421

@@ -1437,7 +1437,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2)
14371437
*
14381438
* @since 9
14391439
*/
1440-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
1440+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
14411441
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3);
14421442
}
14431443

@@ -1461,7 +1461,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
14611461
*
14621462
* @since 9
14631463
*/
1464-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
1464+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
14651465
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4);
14661466
}
14671467

@@ -1487,7 +1487,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
14871487
*
14881488
* @since 9
14891489
*/
1490-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
1490+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
14911491
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
14921492
}
14931493

@@ -1515,7 +1515,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
15151515
*
15161516
* @since 9
15171517
*/
1518-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
1518+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
15191519
K k6, V v6) {
15201520
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
15211521
k6, v6);
@@ -1547,7 +1547,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
15471547
*
15481548
* @since 9
15491549
*/
1550-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
1550+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
15511551
K k6, V v6, K k7, V v7) {
15521552
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
15531553
k6, v6, k7, v7);
@@ -1581,7 +1581,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
15811581
*
15821582
* @since 9
15831583
*/
1584-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
1584+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
15851585
K k6, V v6, K k7, V v7, K k8, V v8) {
15861586
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
15871587
k6, v6, k7, v7, k8, v8);
@@ -1617,7 +1617,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
16171617
*
16181618
* @since 9
16191619
*/
1620-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
1620+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
16211621
K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
16221622
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
16231623
k6, v6, k7, v7, k8, v8, k9, v9);
@@ -1655,7 +1655,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
16551655
*
16561656
* @since 9
16571657
*/
1658-
static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
1658+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,
16591659
K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
16601660
return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,
16611661
k6, v6, k7, v7, k8, v8, k9, v9, k10, v10);
@@ -1694,7 +1694,7 @@ static <K extends Object, V extends Object> Map<K, V> of(K k1, V v1, K k2, V v2,
16941694
*/
16951695
@SafeVarargs
16961696
@SuppressWarnings("varargs")
1697-
static <K extends Object, V extends Object> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
1697+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
16981698
if (entries.length == 0) { // implicit null check of entries array
16991699
@SuppressWarnings("unchecked")
17001700
var map = (Map<K,V>) ImmutableCollections.EMPTY_MAP;
@@ -1750,7 +1750,7 @@ static <K extends Object, V extends Object> Map<K, V> ofEntries(Entry<? extends
17501750
* @see Map#ofEntries Map.ofEntries()
17511751
* @since 9
17521752
*/
1753-
static <K extends Object, V extends Object> Entry<K, V> entry(K k, V v) {
1753+
static <K extends @NonNull Object, V extends @NonNull Object> Entry<K, V> entry(@NonNull K k, @NonNull V v) {
17541754
// KeyValueHolder checks for nulls
17551755
return new KeyValueHolder<>(k, v);
17561756
}
@@ -1773,7 +1773,7 @@ static <K extends Object, V extends Object> Entry<K, V> entry(K k, V v) {
17731773
* @since 10
17741774
*/
17751775
@SuppressWarnings({"rawtypes","unchecked"})
1776-
static <K extends Object, V extends Object> Map<K, V> copyOf(Map<? extends K, ? extends V> map) {
1776+
static <K extends @NonNull Object, V extends @NonNull Object> Map<K, V> copyOf(Map<? extends K, ? extends V> map) {
17771777
if (map instanceof ImmutableCollections.AbstractImmutableMap) {
17781778
return (Map<K,V>)map;
17791779
} else {

src/java.base/share/classes/java/util/Optional.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ public Stream<T> stream() {
434434
* @throws NullPointerException if no value is present and the exception
435435
* supplying function is {@code null}
436436
*/
437+
@CFComment({"optional: orElseThrow(Supplier) does not throw NoSuchElementException, so its receiver is @MaybePresent.",
438+
"Contrast with orElseThrow(), defined just above, whose receiver is @Present."})
437439
@EnsuresQualifier(expression = "this", qualifier = Present.class)
438-
public <X extends Throwable> @NonNull T orElseThrow(@Present Optional<T> this, Supplier<? extends X> exceptionSupplier) throws X {
440+
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
439441
if (value != null) {
440442
return value;
441443
} else {

src/java.base/share/classes/java/util/OptionalDouble.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
2828
import org.checkerframework.checker.nullness.qual.NonNull;
2929
import org.checkerframework.checker.nullness.qual.Nullable;
30+
import org.checkerframework.checker.optional.qual.Present;
3031
import org.checkerframework.dataflow.qual.Pure;
3132
import org.checkerframework.dataflow.qual.SideEffectFree;
3233
import org.checkerframework.framework.qual.AnnotatedFor;
34+
import org.checkerframework.framework.qual.EnsuresQualifier;
35+
import org.checkerframework.framework.qual.EnsuresQualifierIf;
36+
3337

3438
import java.util.function.DoubleConsumer;
3539
import java.util.function.DoubleSupplier;
@@ -62,9 +66,9 @@
6266
*
6367
* @since 1.8
6468
*/
65-
@AnnotatedFor({"lock", "nullness"})
69+
@AnnotatedFor({"lock", "nullness", "optional"})
6670
@jdk.internal.ValueBased
67-
public final class OptionalDouble {
71+
public final @NonNull class OptionalDouble {
6872
/**
6973
* Common instance for {@code empty()}.
7074
*/
@@ -120,7 +124,8 @@ private OptionalDouble(double value) {
120124
* @param value the value to describe
121125
* @return an {@code OptionalDouble} with the value present
122126
*/
123-
public static OptionalDouble of(double value) {
127+
@SideEffectFree
128+
public static @Present OptionalDouble of(double value) {
124129
return new OptionalDouble(value);
125130
}
126131

@@ -134,7 +139,8 @@ public static OptionalDouble of(double value) {
134139
* @return the value described by this {@code OptionalDouble}
135140
* @throws NoSuchElementException if no value is present
136141
*/
137-
public double getAsDouble() {
142+
@Pure
143+
public double getAsDouble(@Present OptionalDouble this) {
138144
if (!isPresent) {
139145
throw new NoSuchElementException("No value present");
140146
}
@@ -146,6 +152,8 @@ public double getAsDouble() {
146152
*
147153
* @return {@code true} if a value is present, otherwise {@code false}
148154
*/
155+
@Pure
156+
@EnsuresQualifierIf(result = true, expression = "this", qualifier = Present.class)
149157
public boolean isPresent() {
150158
return isPresent;
151159
}
@@ -158,6 +166,7 @@ public boolean isPresent() {
158166
* @since 11
159167
*/
160168
@Pure
169+
@EnsuresQualifierIf(result = false, expression = "this", qualifier = Present.class)
161170
public boolean isEmpty() {
162171
return !isPresent;
163172
}
@@ -212,6 +221,7 @@ public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
212221
* @return the optional value as a {@code DoubleStream}
213222
* @since 9
214223
*/
224+
@SideEffectFree
215225
public DoubleStream stream() {
216226
if (isPresent) {
217227
return DoubleStream.of(value);
@@ -253,7 +263,9 @@ public double orElseGet(DoubleSupplier supplier) {
253263
* @throws NoSuchElementException if no value is present
254264
* @since 10
255265
*/
256-
public double orElseThrow() {
266+
@Pure
267+
@EnsuresQualifier(expression = "this", qualifier = Present.class)
268+
public double orElseThrow(@Present OptionalDouble this) {
257269
if (!isPresent) {
258270
throw new NoSuchElementException("No value present");
259271
}
@@ -277,6 +289,7 @@ public double orElseThrow() {
277289
* @throws NullPointerException if no value is present and the exception
278290
* supplying function is {@code null}
279291
*/
292+
@EnsuresQualifier(expression = "this", qualifier = Present.class)
280293
public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
281294
if (isPresent) {
282295
return value;
@@ -320,6 +333,7 @@ public boolean equals(@Nullable Object obj) {
320333
* @return hash code value of the present value or {@code 0} if no value is
321334
* present
322335
*/
336+
@Pure
323337
@Override
324338
public int hashCode() {
325339
return isPresent ? Double.hashCode(value) : 0;

src/java.base/share/classes/java/util/OptionalInt.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;
2828
import org.checkerframework.checker.nullness.qual.NonNull;
2929
import org.checkerframework.checker.nullness.qual.Nullable;
30+
import org.checkerframework.checker.optional.qual.Present;
3031
import org.checkerframework.dataflow.qual.Pure;
3132
import org.checkerframework.dataflow.qual.SideEffectFree;
3233
import org.checkerframework.framework.qual.AnnotatedFor;
34+
import org.checkerframework.framework.qual.EnsuresQualifier;
35+
import org.checkerframework.framework.qual.EnsuresQualifierIf;
3336

3437
import java.util.function.IntConsumer;
3538
import java.util.function.IntSupplier;
@@ -62,9 +65,9 @@
6265
*
6366
* @since 1.8
6467
*/
65-
@AnnotatedFor({"lock", "nullness"})
68+
@AnnotatedFor({"lock", "nullness", "optional"})
6669
@jdk.internal.ValueBased
67-
public final class OptionalInt {
70+
public final @NonNull class OptionalInt {
6871
/**
6972
* Common instance for {@code empty()}.
7073
*/
@@ -120,7 +123,8 @@ private OptionalInt(int value) {
120123
* @param value the value to describe
121124
* @return an {@code OptionalInt} with the value present
122125
*/
123-
public static OptionalInt of(int value) {
126+
@SideEffectFree
127+
public static @Present OptionalInt of(int value) {
124128
return new OptionalInt(value);
125129
}
126130

@@ -134,7 +138,8 @@ public static OptionalInt of(int value) {
134138
* @return the value described by this {@code OptionalInt}
135139
* @throws NoSuchElementException if no value is present
136140
*/
137-
public int getAsInt() {
141+
@Pure
142+
public int getAsInt(@Present OptionalInt this) {
138143
if (!isPresent) {
139144
throw new NoSuchElementException("No value present");
140145
}
@@ -146,6 +151,8 @@ public int getAsInt() {
146151
*
147152
* @return {@code true} if a value is present, otherwise {@code false}
148153
*/
154+
@Pure
155+
@EnsuresQualifierIf(result = true, expression = "this", qualifier = Present.class)
149156
public boolean isPresent() {
150157
return isPresent;
151158
}
@@ -158,6 +165,7 @@ public boolean isPresent() {
158165
* @since 11
159166
*/
160167
@Pure
168+
@EnsuresQualifierIf(result = false, expression = "this", qualifier = Present.class)
161169
public boolean isEmpty() {
162170
return !isPresent;
163171
}
@@ -211,6 +219,7 @@ public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) {
211219
* @return the optional value as an {@code IntStream}
212220
* @since 9
213221
*/
222+
@SideEffectFree
214223
public IntStream stream() {
215224
if (isPresent) {
216225
return IntStream.of(value);
@@ -252,7 +261,9 @@ public int orElseGet(IntSupplier supplier) {
252261
* @throws NoSuchElementException if no value is present
253262
* @since 10
254263
*/
255-
public int orElseThrow() {
264+
@Pure
265+
@EnsuresQualifier(expression = "this", qualifier = Present.class)
266+
public int orElseThrow(@Present OptionalInt this) {
256267
if (!isPresent) {
257268
throw new NoSuchElementException("No value present");
258269
}
@@ -276,6 +287,7 @@ public int orElseThrow() {
276287
* @throws NullPointerException if no value is present and the exception
277288
* supplying function is {@code null}
278289
*/
290+
@EnsuresQualifier(expression = "this", qualifier = Present.class)
279291
public<X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
280292
if (isPresent) {
281293
return value;
@@ -318,6 +330,7 @@ public boolean equals(@Nullable Object obj) {
318330
* @return hash code value of the present value or {@code 0} if no value is
319331
* present
320332
*/
333+
@Pure
321334
@Override
322335
public int hashCode() {
323336
return isPresent ? Integer.hashCode(value) : 0;

0 commit comments

Comments
 (0)