|
| 1 | +package org.checkerframework.checker.optional.qual; |
| 2 | + |
| 3 | +import java.lang.annotation.Documented; |
| 4 | +import java.lang.annotation.ElementType; |
| 5 | +import java.lang.annotation.Retention; |
| 6 | +import java.lang.annotation.RetentionPolicy; |
| 7 | +import java.lang.annotation.Target; |
| 8 | +import org.checkerframework.framework.qual.ConditionalPostconditionAnnotation; |
| 9 | +import org.checkerframework.framework.qual.InheritedAnnotation; |
| 10 | + |
| 11 | +/** |
| 12 | + * Indicates that the given expressions of type Optional<T> are present, if the method returns |
| 13 | + * the given result (either true or false).FOO |
| 14 | + * |
| 15 | + * <p>Here are ways this conditional postcondition annotation can be used: |
| 16 | + * |
| 17 | + * <p><b>Method parameters:</b> Suppose that a method has two arguments of type Optional<T> |
| 18 | + * and returns true if they are both equal <i>and</i> present. You could annotate the method as |
| 19 | + * follows: |
| 20 | + * |
| 21 | + * <pre><code> @EnsuresPresentIf(expression="#1", result=true) |
| 22 | + * @EnsuresPresentIf(expression="#2", result=true) |
| 23 | + * public <T> boolean isPresentAndEqual(Optional<T> optA, Optional<T> optB) { ... }</code> |
| 24 | + * </pre> |
| 25 | + * |
| 26 | + * because, if {@code isPresentAndEqual} returns true, then the first (#1) argument to {@code |
| 27 | + * isPresentAndEqual} was present, and so was the second (#2) argument. Note that you can write two |
| 28 | + * {@code @EnsurePresentIf} annotations on a single method. |
| 29 | + * |
| 30 | + * <p><b>Fields:</b> The value expressions can refer to fields, even private ones. For example: |
| 31 | + * |
| 32 | + * <pre><code> @EnsuresPresentIf(expression="this.optShape", result=true) |
| 33 | + * public boolean isShape() { |
| 34 | + * return (this.optShape != null && this.optShape.isPresent()); |
| 35 | + * }</code></pre> |
| 36 | + * |
| 37 | + * An {@code @EnsuresPresentIf} annotation that refers to a private field is useful for verifying |
| 38 | + * that client code performs needed checks in the right order, even if the client code cannot |
| 39 | + * directly affect the field. |
| 40 | + * |
| 41 | + * <p><b>Method postconditions:</b> Suppose that if a method {@code isRectangle()} returns true, |
| 42 | + * then {@code getRectangle()} will return a present Optional. You an express this relationship as: |
| 43 | + * |
| 44 | + * <pre>{@code @EnsuresPresentIf(result=true, expression="getRectangle()") |
| 45 | + * public @Pure isRectangle() { ... }}</pre> |
| 46 | + * |
| 47 | + * @see Present |
| 48 | + * @see EnsuresPresent |
| 49 | + * @checker_framework.manual #optional-checker Optional Checker |
| 50 | + */ |
| 51 | +@Documented |
| 52 | +@Retention(RetentionPolicy.RUNTIME) |
| 53 | +@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) |
| 54 | +@ConditionalPostconditionAnnotation(qualifier = Present.class) |
| 55 | +@InheritedAnnotation |
| 56 | +public @interface EnsuresPresentIf { |
| 57 | + /** |
| 58 | + * Returns the Java expressions of type Optional<T> that are present after the method |
| 59 | + * returns the given result. |
| 60 | + * |
| 61 | + * @return the Java expressions of type Optional<T> that are present after the method |
| 62 | + * returns the given result. value {@link #result()} |
| 63 | + * @checker_framework.manual #java-expressions-as-arguments Syntax of Java expressions |
| 64 | + */ |
| 65 | + String[] expression(); |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the return value of the method under which the postcondition holds. |
| 69 | + * |
| 70 | + * @return the return value of the method under which the postcondition holds. |
| 71 | + */ |
| 72 | + boolean result(); |
| 73 | + |
| 74 | + /** |
| 75 | + * A wrapper annotation that makes the {@link EnsuresPresentIf} annotation repeatable. |
| 76 | + * |
| 77 | + * <p>Programmers generally do not need to write this. It is created by Java when a programmer |
| 78 | + * writes more than one {@link EnsuresPresentIf} annotation at the same location. |
| 79 | + */ |
| 80 | + @Retention(RetentionPolicy.RUNTIME) |
| 81 | + @Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) |
| 82 | + @ConditionalPostconditionAnnotation(qualifier = Present.class) |
| 83 | + public static @interface List { |
| 84 | + /** |
| 85 | + * Returns the repeatable annotations. |
| 86 | + * |
| 87 | + * @return the repeatable annotations. |
| 88 | + */ |
| 89 | + EnsuresPresentIf[] value(); |
| 90 | + } |
| 91 | +} |
0 commit comments