Class Unsafe
These utilities should be used sparsely, only if no better solution is available.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NonNull T
assertNonNull
(@Nullable T object) Performs an unchecked cast to the non-null
variant of the specified type.static <T> T
Performs an unchecked cast to the specified target type.static <T> @NonNull T
castNonNull
(@UnknownInitialization @Nullable Object object) Performs an unchecked cast to the specified non-null
target type.static <T> @Initialized @NonNull T
initialized
(@UnknownInitialization @Nullable T object) Performs an unchecked cast to theInitialized
non-null
variant of the specified type.static <T> @Nullable T
nullable
(@Nullable T object) Casts the given object to the nullable variant of the specified type.static <T> @NonNull T
nullableAsNonNull
(@Nullable T object) Performs an unchecked cast to the non-null
variant of the specified type.static <T> @NonNull T
Returns anull
value that bypasses compiler and tooling checks.
-
Method Details
-
cast
Performs an unchecked cast to the specified target type.This can be used to suppress certain false-positive type checking warnings of the Java compiler or other tools when the object is known to be of the specified type.
- Type Parameters:
T
- the target type- Parameters:
object
- the object to cast- Returns:
- the casted object
-
castNonNull
@EnsuresNonNull("#1") public static <T> @NonNull T castNonNull(@UnknownInitialization @Nullable Object object) Performs an unchecked cast to the specified non-null
target type.This can be used to suppress certain false-positive type checking warnings of the Java compiler or other tools when the object is known to be of the specified non-
null
type.- Type Parameters:
T
- the target type- Parameters:
object
- the object to cast- Returns:
- the casted object
-
assertNonNull
@EnsuresNonNull("#1") public static <T> @NonNull T assertNonNull(@Nullable T object) Performs an unchecked cast to the non-null
variant of the specified type.This can be used to suppress certain false-positive type checking warnings of the Java compiler or other tools when the object is known to not be
null
.To intentionally 'disguise' a
null
value as non-null
(for example in tests), useuncheckedNull()
ornullableAsNonNull(Object)
instead.- Type Parameters:
T
- the target type- Parameters:
object
- the object to cast- Returns:
- the casted object
-
initialized
@EnsuresNonNull("#1") public static <T> @Initialized @NonNull T initialized(@UnknownInitialization @Nullable T object) Performs an unchecked cast to theInitialized
non-null
variant of the specified type.This can be used to suppress certain type checking warnings of compiler tools when it is safe to treat the given object as already fully initialized (e.g. when it is known that the object reference is only first used once the object has been fully initialized).
- Type Parameters:
T
- the target type- Parameters:
object
- the object to cast- Returns:
- the casted object
-
nullable
public static <T> @Nullable T nullable(@Nullable T object) Casts the given object to the nullable variant of the specified type.This can be used to cast a known to be falsely non-
null
typed object to its corresponding nullable type.- Type Parameters:
T
- the target type- Parameters:
object
- the object to cast- Returns:
- the casted object
-
uncheckedNull
public static <T> @NonNull T uncheckedNull()Returns anull
value that bypasses compiler and tooling checks.This can be used in cases in which
null
is known to be a valid value but is disallowed by the type system.Another use case is to initialize non-
null
typed fields that are known to be properly set to a non-null
value before they are accessed normally for the first time.Another use case are tests that may want to intentionally pass
null
to a method that usually does not expectnull
as an argument.- Type Parameters:
T
- the target type- Returns:
- the
null
value
-
nullableAsNonNull
public static <T> @NonNull T nullableAsNonNull(@Nullable T object) Performs an unchecked cast to the non-null
variant of the specified type.This can be used to suppress certain false-positive type checking warnings of the Java compiler or other tools, for example in cases in which
null
is known to be a valid value but is disallowed by the type system.Unlike
assertNonNull(Object)
, which is meant for cases in which the given value is asserted to not benull
, the object passed to this method may indeed benull
.- Type Parameters:
T
- the target type- Parameters:
object
- the object to cast- Returns:
- the casted object
-