Annotation Interface EnsuresPresentIf
@Documented
@Retention(RUNTIME)
@Target({METHOD,CONSTRUCTOR})
@ConditionalPostconditionAnnotation(qualifier=Present.class)
@InheritedAnnotation
public @interface EnsuresPresentIf
Indicates that the given expressions of type Optional<T> are present, if the method returns
the given result (either true or false).
Here are ways this conditional postcondition annotation can be used:
Method parameters: Suppose that a method has two arguments of type Optional<T> and returns true if they are both equal and present. You could annotate the method as follows:
@EnsuresPresentIf(expression="#1", result=true)
@EnsuresPresentIf(expression="#2", result=true)
public <T> boolean isPresentAndEqual(Optional<T> optA, Optional<T> optB) { ... }
because, if isPresentAndEqual
returns true, then the first (#1) argument to
isPresentAndEqual
was present, and so was the second (#2) argument. Note that you can write two
@EnsuresPresentIf
annotations on a single method.
Fields: The value expressions can refer to fields, even private ones. For example:
@EnsuresPresentIf(expression="this.optShape", result=true)
public boolean isShape() {
return (this.optShape != null && this.optShape.isPresent());
}
An @EnsuresPresentIf
annotation that refers to a private field is useful for verifying
that a method establishes a property, even though client code cannot directly affect the field.
Method postconditions: Suppose that if a method isRectangle()
returns true,
then getRectangle()
will return a present Optional. You an express this relationship as:
@EnsuresPresentIf(result=true, expression="getRectangle()")
public @Pure isRectangle() { ... }
- See Also:
- See the Checker Framework Manual:
- Optional Checker
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic @interface
A wrapper annotation that makes theEnsuresPresentIf
annotation repeatable. -
Required Element Summary
Modifier and TypeRequired ElementDescriptionString[]
Returns the Java expressions of typeOptional<T>
that are present after the method returns the given result.boolean
Returns the return value of the method under which the postcondition holds.
-
Element Details
-
result
boolean resultReturns the return value of the method under which the postcondition holds.- Returns:
- the return value of the method under which the postcondition holds
-
expression
String[] expressionReturns the Java expressions of typeOptional<T>
that are present after the method returns the given result.- Returns:
- the Java expressions of type
Optional<T>
that are present after the method returns the given result - See the Checker Framework Manual:
- Syntax of Java expressions
-