Annotation Type EnsuresNonNullIf


  • @Retention(CLASS)
    @Target(METHOD)
    public @interface EnsuresNonNullIf
    An annotation describing a nullability post-condition for an instance method. Each parameter to the annotation should be a field of the enclosing class. The method must ensure that the method returns true in case the fields are non-null. The method can also return false in case the fields are non-null (inverse logic), and in such case, you must set result to false. NullAway verifies that the property holds.

    Here is an example:

     class Foo {
         @Nullable Object theField;
    
         @EnsuresNonNullIf("theField", result=true) // "this.theField" is also valid
         boolean hasTheField() {
             return theField != null;
         }
    
         void bar() {
             if(!hasTheField()) {
                 return;
             }
    
             // No error, NullAway knows theField is non-null after call to hasTheField()
             theField.toString();
         }
     }
     
    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      java.lang.String[] value
      The list of fields that are non-null after the method returns the given result.
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      boolean result
      The return value of the method under which the postcondition holds.
    • Element Detail

      • value

        java.lang.String[] value
        The list of fields that are non-null after the method returns the given result.
        Returns:
        The list of field names
      • result

        boolean result
        The return value of the method under which the postcondition holds. The default is set to true, which means the method should return true in case fields are non-null.
        Returns:
        true or false
        Default:
        true