Class Unsafe
These utilities should be used sparsely, only if no better solution is available.
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> @NonNull TassertNonNull(@Nullable T object) Performs an unchecked cast to the non-nullvariant of the specified type.static <T> TPerforms an unchecked cast to the specified target type.static <T> @NonNull TcastNonNull(@UnknownInitialization @Nullable Object object) Performs an unchecked cast to the specified non-nulltarget type.static <T> @Initialized @NonNull Tinitialized(@UnknownInitialization @Nullable T object) Performs an unchecked cast to theInitializednon-nullvariant of the specified type.static <T> @Nullable Tnullable(@Nullable T object) Casts the given object to the nullable variant of the specified type.static <T> @NonNull TnullableAsNonNull(@Nullable T object) Performs an unchecked cast to the non-nullvariant of the specified type.static <T> @NonNull TReturns anullvalue that bypasses compiler and tooling checks.
- 
Method Details- 
castPerforms 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-nulltarget 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- nulltype.- 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-nullvariant 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 nullvalue 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 theInitializednon-nullvariant 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
 
- 
nullablepublic 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- nulltyped object to its corresponding nullable type.- Type Parameters:
- T- the target type
- Parameters:
- object- the object to cast
- Returns:
- the casted object
 
- 
uncheckedNullpublic static <T> @NonNull T uncheckedNull()Returns anullvalue that bypasses compiler and tooling checks.This can be used in cases in which nullis known to be a valid value but is disallowed by the type system.Another use case is to initialize non- nulltyped fields that are known to be properly set to a non-nullvalue before they are accessed normally for the first time.Another use case are tests that may want to intentionally pass nullto a method that usually does not expectnullas an argument.- Type Parameters:
- T- the target type
- Returns:
- the nullvalue
 
- 
nullableAsNonNullpublic static <T> @NonNull T nullableAsNonNull(@Nullable T object) Performs an unchecked cast to the non-nullvariant 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 nullis 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
 
 
-