public final class NullnessUtil extends Object
To avoid the need to write the NullnessUtil class name, do:
import static org.checkerframework.checker.nullness.NullnessUtil.castNonNull;or
import static org.checkerframework.checker.nullness.NullnessUtil.*;
Runtime Dependency
Please note that using this class introduces a runtime dependency. This means that you need to distribute (or link to) the Checker Framework, along with your binaries.
To eliminate this dependency, you can simply copy this class into your own project.
Modifier and Type | Method and Description |
---|---|
static <T> T |
castNonNull(T ref)
A method that suppresses warnings from the Nullness Checker.
|
static <T> T[] |
castNonNullDeep(T[] arr)
Like castNonNull, but whereas that method only checks and casts the reference itself, this
traverses all levels of the argument array.
|
static <T> T[][] |
castNonNullDeep(T[][] arr)
Like castNonNull, but whereas that method only checks and casts the reference itself, this
traverses all levels of the argument array.
|
static <T> T[][][] |
castNonNullDeep(T[][][] arr)
Like castNonNull, but whereas that method only checks and casts the reference itself, this
traverses all levels of the argument array.
|
static <T> T[][][][] |
castNonNullDeep(T[][][][] arr)
Like castNonNull, but whereas that method only checks and casts the reference itself, this
traverses all levels of the argument array.
|
static <T> T[][][][][] |
castNonNullDeep(T[][][][][] arr)
Like castNonNull, but whereas that method only checks and casts the reference itself, this
traverses all levels of the argument array.
|
@EnsuresNonNull(value="#1") public static <T> T castNonNull(T ref)
The method takes a possibly-null reference, unsafely casts it to have the @NonNull type
qualifier, and returns it. The Nullness Checker considers both the return value, and also the
argument, to be non-null after the method call. Therefore, the castNonNull
method can
be used either as a cast expression or as a statement. The Nullness Checker issues no
warnings in any of the following code:
// one way to use as a cast: @NonNull String s = castNonNull(possiblyNull1); // another way to use as a cast: castNonNull(possiblyNull2).toString(); // one way to use as a statement: castNonNull(possiblyNull3); possiblyNull3.toString();`The
castNonNull
method is intended to be used in situations where the programmer
definitively knows that a given reference is not null, but the type system is unable to make
this deduction. It is not intended for defensive programming, in which a programmer cannot
prove that the value is not null but wishes to have an earlier indication if it is. See the
Checker Framework Manual for further discussion.
The method throws AssertionError
if Java assertions are enabled and the argument
is null
. If the exception is ever thrown, then that indicates that the programmer
misused the method by using it in a circumstance where its argument can be null.
ref
- a reference of @Nullable type@EnsuresNonNull(value="#1") public static <T> T[] castNonNullDeep(T[] arr)
castNonNull(Object)
@EnsuresNonNull(value="#1") public static <T> T[][] castNonNullDeep(T[][] arr)
castNonNull(Object)
@EnsuresNonNull(value="#1") public static <T> T[][][] castNonNullDeep(T[][][] arr)
castNonNull(Object)
@EnsuresNonNull(value="#1") public static <T> T[][][][] castNonNullDeep(T[][][][] arr)
castNonNull(Object)
@EnsuresNonNull(value="#1") public static <T> T[][][][][] castNonNullDeep(T[][][][][] arr)
castNonNull(Object)