| 1 |
import org.checkerframework.checker.nullness.NullnessUtil; |
| 2 |
import org.checkerframework.checker.nullness.qual.*; |
| 3 |
|
| 4 |
/** Test class org.checkerframework.checker.nullness.NullnessUtil. */ |
| 5 |
class TestNullnessUtil { |
| 6 |
void testRef1(@Nullable Object o) { |
| 7 |
// one way to use as a cast: |
| 8 |
@NonNull Object l1 = NullnessUtil.castNonNull(o); |
| 9 |
} |
| 10 |
|
| 11 |
void testRef2(@Nullable Object o) { |
| 12 |
// another way to use as a cast: |
| 13 |
NullnessUtil.castNonNull(o).toString(); |
| 14 |
} |
| 15 |
|
| 16 |
void testRef3(@Nullable Object o) { |
| 17 |
// use as statement: |
| 18 |
NullnessUtil.castNonNull(o); |
| 19 |
o.toString(); |
| 20 |
} |
| 21 |
|
| 22 |
void testArr1(@Nullable Object @NonNull [] a) { |
| 23 |
// one way to use as a cast: |
| 24 |
@NonNull Object[] l2 = NullnessUtil.castNonNullDeep(a); |
| 25 |
// Careful, the non-deep version only casts the main modifier. |
| 26 |
// :: error: (assignment.type.incompatible) |
| 27 |
@NonNull Object[] l2b = NullnessUtil.castNonNull(a); |
| 28 |
// OK |
| 29 |
@Nullable Object[] l2c = NullnessUtil.castNonNull(a); |
| 30 |
} |
| 31 |
|
| 32 |
void testArr1b(@Nullable Object @Nullable [] a) { |
| 33 |
// one way to use as a cast: |
| 34 |
@NonNull Object[] l2 = NullnessUtil.castNonNullDeep(a); |
| 35 |
// Careful, the non-deep version only casts the main modifier. |
| 36 |
// :: error: (assignment.type.incompatible) |
| 37 |
@NonNull Object[] l2b = NullnessUtil.castNonNull(a); |
| 38 |
// OK |
| 39 |
@Nullable Object[] l2c = NullnessUtil.castNonNull(a); |
| 40 |
} |
| 41 |
|
| 42 |
void testArr2(@Nullable Object @NonNull [] a) { |
| 43 |
// another way to use as a cast: |
| 44 |
NullnessUtil.castNonNullDeep(a)[0].toString(); |
| 45 |
} |
| 46 |
|
| 47 |
void testArr3(@Nullable Object @NonNull [] a) { |
| 48 |
// use as statement: |
| 49 |
NullnessUtil.castNonNullDeep(a); |
| 50 |
a.toString(); |
| 51 |
// TODO: @EnsuresNonNull cannot express that |
| 52 |
// all the array components are non-null. |
| 53 |
// a[0].toString(); |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
// TODO: flow does not propagate component types. |
| 58 |
void testArr3(@Nullable Object @NonNull [] a) { |
| 59 |
// one way to use as a statement: |
| 60 |
NullnessUtil.castNonNull(a); |
| 61 |
a[0].toString(); |
| 62 |
} |
| 63 |
*/ |
| 64 |
|
| 65 |
void testMultiArr1(@Nullable Object @NonNull [] @Nullable [] a) { |
| 66 |
// :: error: (assignment.type.incompatible) :: error: (accessing.nullable) |
| 67 |
@NonNull Object l3 = a[0][0]; |
| 68 |
// one way to use as a cast: |
| 69 |
@NonNull Object[][] l4 = NullnessUtil.castNonNullDeep(a); |
| 70 |
} |
| 71 |
|
| 72 |
void testMultiArr2(@Nullable Object @NonNull [] @Nullable [] a) { |
| 73 |
// another way to use as a cast: |
| 74 |
NullnessUtil.castNonNullDeep(a)[0][0].toString(); |
| 75 |
} |
| 76 |
|
| 77 |
void testMultiArr3(@Nullable Object @Nullable [] @Nullable [] @Nullable [] a) { |
| 78 |
// :: error: (dereference.of.nullable) :: error: (accessing.nullable) |
| 79 |
a[0][0][0].toString(); |
| 80 |
// another way to use as a cast: |
| 81 |
NullnessUtil.castNonNullDeep(a)[0][0][0].toString(); |
| 82 |
} |
| 83 |
|
| 84 |
public static void main(String[] args) { |
| 85 |
Object[] @Nullable [] err = new Object[10][10]; |
| 86 |
Object[][] e1 = NullnessUtil.castNonNullDeep(err); |
| 87 |
e1[0][0].toString(); |
| 88 |
} |
| 89 |
} |