public class Nullness extends Object
The class enables to remove checker-qual runtime dependency, and helps IDEs to see
the resulting types of castNonNull better.
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
castNonNull(T ref)
Allows you to treat a nullable type as non-nullable with no assertions.
|
static <T> T[] |
castNonNullArray(T[] ts)
Allows you to treat an array of nullable values as an array of non-nullable
values.
|
static <T> List<T> |
castNonNullList(List<? extends T> ts)
Allows you to treat a list of nullable values as an list of non-nullable
values.
|
static <T> T |
castToInitialized(T ref)
Allows you to treat an uninitialized or under-initialization object as
initialized with no assertions.
|
@Pure @EnsuresNonNull(value="#1") public static <T> T castNonNull(T ref)
It is useful in the case you have a nullable lately-initialized field like the following:
class Wrapper<T> {
@Nullable T value;
}
That signature allows you to use Wrapper with both nullable or
non-nullable types: Wrapper<@Nullable Integer>
vs Wrapper<Integer>. Suppose you need to implement
T get() { return value; }
The issue is checkerframework does not permit that because T
has unknown nullability, so the following needs to be used:
T get() { return sneakyNull(value); }
T - the type of the referenceref - a reference of @Nullable type, that is non-null at run time@Pure public static <T> T[] castNonNullArray(T[] ts)
T - Type of the array elementsts - Array@Pure public static <T> List<T> castNonNullList(List<? extends T> ts)
T - Type of the list elementsts - List@Pure public static <T> T castToInitialized(T ref)
T - The type of the referenceref - A reference that was @Uninitialized at some point but is
now fully initializedCopyright © 2012-2023 Apache Software Foundation. All Rights Reserved.