Annotation Interface AssertMethod


@Documented @Retention(RUNTIME) @Target(METHOD) public @interface AssertMethod
AssertMethod is a method annotation that indicates that a method throws an exception if the value of a boolean argument is false. This can be used to annotate methods such as JUnit's Assertions.assertTrue(...).

The annotation enables flow-sensitive type refinement to be more precise. For example, if Assertions.assertTrue is annotated as follows:

@AssertMethod(value = AssertionFailedError.class)
 public static void assertFalse(boolean condition);
 
Then, in the code below, the Optional Checker can determine that optional has a value and the call to Optional#get will not throw an exception.
 Assertions.assertTrue(optional.isPresent());
 Object o = optional.get();
 

This annotation is a trusted annotation, meaning that the Checker Framework does not check whether the annotated method really does throw an exception depending on the boolean expression.

See the Checker Framework Manual:
Automatic type refinement (flow-sensitive type qualifier inference)
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Returns whether this method asserts that the boolean expression is false.
    int
    The one-based index of the boolean parameter that is tested.
    The class of the exception thrown by this method.
  • Element Details

    • value

      Class<?> value
      The class of the exception thrown by this method. The default is AssertionError.
      Returns:
      class of the exception thrown by this method
      Default:
      java.lang.AssertionError.class
    • parameter

      int parameter
      The one-based index of the boolean parameter that is tested.
      Returns:
      the one-based index of the boolean parameter that is tested
      Default:
      1
    • isAssertFalse

      boolean isAssertFalse
      Returns whether this method asserts that the boolean expression is false.

      For example, Junit's Assertions.assertFalse(...) throws an exception if the first argument is false. So it is annotated as follows:

      @AssertMethod(value = AssertionFailedError.class, isAssertFalse = true)
       public static void assertFalse(boolean condition);
       
      Returns:
      the value for parameter() on which the method throws an exception
      Default:
      false