Package groovy.contracts

# Annotation Interface Invariant

@Documented @Retention(RUNTIME) @Target(TYPE) @Incubating @ClassInvariant @Repeatable(Invariants.class) @AnnotationProcessorImplementation(ClassInvariantAnnotationProcessor.class) public @interface Invariant

Represents a class-invariant or a loop invariant.

When applied to a class, defines assertions holding during the entire object's life-time. Class-invariants are verified at runtime at the following pointcuts:

- after a constructor call
- before a method call
- after a method call

When applied to a `for`, `while`, or `do-while` loop, defines a loop invariant that is asserted at the start of each iteration:

     int sum = 0
     @Invariant({ 0 <= i && i <= 4 })
     for (int i in 0..4) {
         sum += i
     }

Whenever a class has a parent which itself specifies a class-invariant, that class-invariant expression is combined with the actual class's invariant (by using a logical AND).

- ## Required Element Summary

  Required Elements
  Modifier and Type
  Required Element
  Description
  `Class`
  `value`
  Returns the closure class that evaluates the invariant expression.

- ## Element Details

  - ### value

    Class value
    Returns the closure class that evaluates the invariant expression.
    Returns:
      the generated closure class backing the invariant