Skip to content

Commit dbcb8de

Browse files
authored
Add annotations that were not copied from jdk8 (openjdk#7)
Note: the annotations added to src/java.base/share/classes/java/util/Properties.java are to methods that are new JDK 11.
1 parent 66c6a39 commit dbcb8de

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
*/
2525
package java.util;
2626

27+
import org.checkerframework.checker.nullness.qual.PolyNull;
28+
import org.checkerframework.checker.nullness.qual.Nullable;
29+
import org.checkerframework.checker.nullness.qual.NonNull;
2730
import org.checkerframework.framework.qual.AnnotatedFor;
31+
import org.checkerframework.framework.qual.Covariant;
2832
import org.checkerframework.framework.qual.CFComment;
2933

34+
import org.checkerframework.checker.optional.qual.Present;
35+
import org.checkerframework.framework.qual.EnsuresQualifierIf;
36+
3037
import java.util.function.Consumer;
3138
import java.util.function.Function;
3239
import java.util.function.Predicate;
@@ -71,8 +78,9 @@
7178
"meaning, but are unrelated by the Java type hierarchy.",
7279
"@Covariant makes Optional<@NonNull String> a subtype of Optional<@Nullable String>."
7380
})
74-
@AnnotatedFor({"lock", "nullness"})
75-
public final class Optional<T> {
81+
@AnnotatedFor({"lock", "nullness", "optional"})
82+
@Covariant(0)
83+
public final @NonNull class Optional<T> {
7684
/**
7785
* Common instance for {@code empty()}.
7886
*/
@@ -81,7 +89,7 @@ public final class Optional<T> {
8189
/**
8290
* If non-null, the value; if null, indicates no value is present
8391
*/
84-
private final T value;
92+
private final @Nullable T value;
8593

8694
/**
8795
* Constructs an empty instance.
@@ -118,7 +126,7 @@ public static<T> Optional<T> empty() {
118126
* @param value the non-{@code null} value to describe
119127
* @throws NullPointerException if value is {@code null}
120128
*/
121-
private Optional(T value) {
129+
private Optional(@NonNull T value) {
122130
this.value = Objects.requireNonNull(value);
123131
}
124132

@@ -131,7 +139,7 @@ private Optional(T value) {
131139
* @return an {@code Optional} with the value present
132140
* @throws NullPointerException if value is {@code null}
133141
*/
134-
public static <T> Optional<T> of(T value) {
142+
public static <T> @Present Optional<T> of(@NonNull T value) {
135143
return new Optional<>(value);
136144
}
137145

@@ -144,7 +152,7 @@ public static <T> Optional<T> of(T value) {
144152
* @return an {@code Optional} with a present value if the specified value
145153
* is non-{@code null}, otherwise an empty {@code Optional}
146154
*/
147-
public static <T> Optional<T> ofNullable(T value) {
155+
public static <T> Optional<T> ofNullable(@Nullable T value) {
148156
return value == null ? empty() : of(value);
149157
}
150158

@@ -158,7 +166,7 @@ public static <T> Optional<T> ofNullable(T value) {
158166
* @return the non-{@code null} value described by this {@code Optional}
159167
* @throws NoSuchElementException if no value is present
160168
*/
161-
public T get() {
169+
public @NonNull T get(@Present Optional<T> this) {
162170
if (value == null) {
163171
throw new NoSuchElementException("No value present");
164172
}
@@ -170,6 +178,7 @@ public T get() {
170178
*
171179
* @return {@code true} if a value is present, otherwise {@code false}
172180
*/
181+
@EnsuresQualifierIf(result = true, expression = "this", qualifier = Present.class)
173182
public boolean isPresent() {
174183
return value != null;
175184
}
@@ -272,7 +281,7 @@ public Optional<T> filter(Predicate<? super T> predicate) {
272281
* present, otherwise an empty {@code Optional}
273282
* @throws NullPointerException if the mapping function is {@code null}
274283
*/
275-
public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
284+
public <U> Optional<U> map(Function<? super T, ? extends @Nullable U> mapper) {
276285
Objects.requireNonNull(mapper);
277286
if (!isPresent()) {
278287
return empty();
@@ -366,7 +375,7 @@ public Stream<T> stream() {
366375
* May be {@code null}.
367376
* @return the value, if present, otherwise {@code other}
368377
*/
369-
public T orElse(T other) {
378+
public @PolyNull T orElse(@PolyNull T other) {
370379
return value != null ? value : other;
371380
}
372381

@@ -380,7 +389,7 @@ public T orElse(T other) {
380389
* @throws NullPointerException if no value is present and the supplying
381390
* function is {@code null}
382391
*/
383-
public T orElseGet(Supplier<? extends T> supplier) {
392+
public @PolyNull T orElseGet(Supplier<? extends @PolyNull T> supplier) {
384393
return value != null ? value : supplier.get();
385394
}
386395

@@ -416,7 +425,7 @@ public T orElseThrow() {
416425
* @throws NullPointerException if no value is present and the exception
417426
* supplying function is {@code null}
418427
*/
419-
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
428+
public <X extends Throwable> T orElseThrow(@Present Optional<T> this, Supplier<? extends X> exceptionSupplier) throws X {
420429
if (value != null) {
421430
return value;
422431
} else {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package java.util;
2727

2828
import org.checkerframework.checker.lock.qual.GuardSatisfied;
29+
import org.checkerframework.checker.nullness.qual.KeyFor;
2930
import org.checkerframework.checker.nullness.qual.Nullable;
3031
import org.checkerframework.checker.nullness.qual.PolyNull;
3132
import org.checkerframework.checker.propkey.qual.PropertyKey;
@@ -1347,7 +1348,7 @@ public synchronized String toString() {
13471348
}
13481349

13491350
@Override
1350-
public Set<Object> keySet() {
1351+
public Set<@KeyFor("this") Object> keySet() {
13511352
return Collections.synchronizedSet(map.keySet(), this);
13521353
}
13531354

@@ -1357,7 +1358,7 @@ public Collection<Object> values() {
13571358
}
13581359

13591360
@Override
1360-
public Set<Map.Entry<Object, Object>> entrySet() {
1361+
public Set<Map.Entry<@KeyFor("this") Object, Object>> entrySet() {
13611362
return Collections.synchronizedSet(new EntrySet(map.entrySet()), this);
13621363
}
13631364

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
package java.util;
4242

43+
import org.checkerframework.checker.index.qual.NonNegative;
4344
import org.checkerframework.checker.i18n.qual.LocalizableKey;
4445
import org.checkerframework.checker.i18n.qual.Localized;
4546
import org.checkerframework.checker.i18nformatter.qual.I18nMakeFormat;
@@ -3307,7 +3308,7 @@ public InputStream run() throws IOException {
33073308
* if <code>baseName</code> or <code>locale</code> is
33083309
* <code>null</code>
33093310
*/
3310-
public long getTimeToLive(String baseName, Locale locale) {
3311+
public @NonNegative long getTimeToLive(String baseName, Locale locale) {
33113312
if (baseName == null || locale == null) {
33123313
throw new NullPointerException();
33133314
}

0 commit comments

Comments
 (0)