Debian Codegraph @

checker/tests/nullness/java8/Issue1633.java

raw
1 // Test case for Issue 1633:
2 // https://github.com/typetools/checker-framework/issues/1633
3
4 import java.util.function.Supplier;
5 import org.checkerframework.checker.nullness.qual.*;
6 import org.checkerframework.checker.nullness.qual.NonNull;
7 import org.checkerframework.checker.nullness.qual.Nullable;
8 import org.checkerframework.framework.qual.Covariant;
9
10 class Issue1633 {
11
12 // supplyNullable is a supplier that may return null.
13 // supplyNonNull is a supplier that does not return null.
14
15 void foo1(Optional1633<String> o, Supplier<@Nullable String> supplyNullable) {
16 // :: error: (argument.type.incompatible)
17 @Nullable String str = o.orElseGetUnannotated(supplyNullable);
18 }
19
20 void foo2(Optional1633<String> o, Supplier<@Nullable String> supplyNullable) {
21 @Nullable String str1 = o.orElseGetNullable(supplyNullable);
22 }
23
24 void foo2nw(Optional1633<String> o, Supplier<@Nullable String> supplyNullable) {
25 @Nullable String str1 = o.orElseGetNullableNoWildcard(supplyNullable);
26 }
27
28 void foo3(Optional1633<String> o, Supplier<@Nullable String> supplyNullable) {
29 // :: error: (argument.type.incompatible)
30 @Nullable String str2 = o.orElseGetNonNull(supplyNullable);
31 }
32
33 void foo4(Optional1633<String> o, Supplier<@Nullable String> supplyNullable) {
34 @Nullable String str3 = o.orElseGetPolyNull(supplyNullable);
35 }
36
37 void foo4nw(Optional1633<String> o, Supplier<@Nullable String> supplyNullable) {
38 @Nullable String str3 = o.orElseGetPolyNullNoWildcard(supplyNullable);
39 }
40
41 void foo41(Optional1633<String> o) {
42 @SuppressWarnings("return.type.incompatible") // https://tinyurl.com/cfissue/979
43 @Nullable String str3 = o.orElseGetPolyNull(() -> null);
44 }
45
46 void foo41nw(Optional1633<String> o) {
47 @SuppressWarnings("return.type.incompatible") // https://tinyurl.com/cfissue/979
48 @Nullable String str3 = o.orElseGetPolyNullNoWildcard(() -> null);
49 }
50
51 void foo5(Optional1633<String> o, Supplier<@NonNull String> supplyNonNull) {
52 @NonNull String str = o.orElseGetUnannotated(supplyNonNull);
53 }
54
55 void foo6(Optional1633<String> o, Supplier<@NonNull String> supplyNonNull) {
56 // :: error: (argument.type.incompatible) :: error: (assignment.type.incompatible)
57 @NonNull String str1 = o.orElseGetNullable(supplyNonNull);
58 }
59
60 void foo6nw(Optional1633<String> o, Supplier<@NonNull String> supplyNonNull) {
61 // :: error: (assignment.type.incompatible)
62 @NonNull String str1 = o.orElseGetNullableNoWildcard(supplyNonNull);
63 }
64
65 void foo7(Optional1633<String> o, Supplier<@NonNull String> supplyNonNull) {
66 @NonNull String str2 = o.orElseGetNonNull(supplyNonNull);
67 }
68
69 void foo8(Optional1633<String> o, Supplier<@NonNull String> supplyNonNull) {
70 @NonNull String str3 = o.orElseGetPolyNull(supplyNonNull);
71 }
72
73 void foo8nw(Optional1633<String> o, Supplier<@NonNull String> supplyNonNull) {
74 @NonNull String str3 = o.orElseGetPolyNullNoWildcard(supplyNonNull);
75 }
76 }
77
78 // From the JDK
79 @Covariant(0)
80 final @NonNull class Optional1633<T extends @Nullable Object> {
81
82 /** If non-null, the value; if null, indicates no value is present. */
83 private final @Nullable T value = null;
84
85 // TODO: there are conceptually two versions of this method:
86 // public @Nullable T orElseGet(Supplier<? extends @Nullable T> other) {
87 // public @NonNull T orElseGet(Supplier<? extends @NonNull T> other) {
88 // Issue #1633 says that this annotation doesn't help at all:
89 // public @PolyNull T orElseGet(Supplier<? extends @PolyNull T> other) {
90 // but it does seem to work in this test case.
91 public T orElseGetUnannotated(Supplier<? extends T> other) {
92 return value != null ? value : other.get();
93 }
94
95 public @Nullable T orElseGetNullable(Supplier<@Nullable ? extends @Nullable T> other) {
96 return value != null ? value : other.get();
97 }
98
99 public @Nullable T orElseGetNullableNoWildcard(Supplier<? extends @Nullable T> other) {
100 // The commented-out line fails to typecheck due to issue #979
101 // return value != null ? value : other.get();
102 if (value != null) {
103 return value;
104 } else {
105 return other.get();
106 }
107 }
108
109 public @NonNull T orElseGetNonNull(Supplier<@NonNull ? extends @NonNull T> other) {
110 return value != null ? value : other.get();
111 }
112
113 public @PolyNull T orElseGetPolyNull(Supplier<@PolyNull ? extends @PolyNull T> other) {
114 return value != null ? value : other.get();
115 }
116
117 public @PolyNull T orElseGetPolyNullNoWildcard(Supplier<? extends @PolyNull T> other) {
118 // The commented-out line fails to typecheck due to issue #979
119 // return value != null ? value : other.get();
120 if (value != null) {
121 return value;
122 } else {
123 return other.get();
124 }
125 }
126 }