Class NullUtil

java.lang.Object
com.puppycrawl.tools.checkstyle.utils.NullUtil

public final class NullUtil extends Object
Utility methods to suppress Checker Framework nullness warnings for cases where null is theoretically possible but practically impossible due to Java grammar rules.

Checkstyle only processes compiled Java sources, so certain AST nodes are guaranteed to exist by Java Language Specification. For example, every METHOD_DEF must have a PARAMETERS child, every definition must have an IDENT child, etc.

Use this utility for such grammar-guaranteed cases. Do NOT use for genuinely nullable values where null should be handled with proper null checks.

  • Constructor Details

    • NullUtil

      private NullUtil()
      Stop instances being created.
  • Method Details

    • notNull

      public static <T> @NonNull T notNull(@Nullable T ref)
      Assert that a reference is non-null. The method suppresses nullness warnings from the Checker Framework and throws AssertionError if the argument is null when Java assertions are enabled.
      Type Parameters:
      T - the type of the reference
      Parameters:
      ref - a reference of @Nullable type, that is non-null at run time
      Returns:
      the argument, cast to have the type qualifier @NonNull
      Throws:
      AssertionError - if ref is null and assertions are enabled