Class Unsafe

java.lang.Object
valorless.valorlessutils.json.Unsafe

public final class Unsafe extends Object
Utilities related to unchecked casts and nullness suppressions.

These utilities should be used sparsely, only if no better solution is available.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> @NonNull T
    assertNonNull(@Nullable T object)
    Performs an unchecked cast to the non-null variant of the specified type.
    static <T> T
    cast(@UnknownInitialization @Nullable Object object)
    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 the Initialized 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 a null value that bypasses compiler and tooling checks.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • cast

      public static <T> T cast(@UnknownInitialization @Nullable Object object)
      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), use uncheckedNull() or nullableAsNonNull(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 the Initialized 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 a null 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 expect null 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 be null, the object passed to this method may indeed be null.

      Type Parameters:
      T - the target type
      Parameters:
      object - the object to cast
      Returns:
      the casted object