1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
import org.checkerframework.checker.index.qual.LTLengthOf;
import org.checkerframework.common.value.qual.MinLen;
import org.checkerframework.framework.qual.ConditionalPostconditionAnnotation;
import org.checkerframework.framework.qual.JavaExpression;
import org.checkerframework.framework.qual.PostconditionAnnotation;
import org.checkerframework.framework.qual.PreconditionAnnotation;
import org.checkerframework.framework.qual.QualifierArgument;
public class CustomContractWithArgs {
// Postcondition for MinLen
@PostconditionAnnotation(qualifier = MinLen.class)
@interface EnsuresMinLen {
public String[] value();
@QualifierArgument("value")
public int targetValue();
}
// Conditional postcondition for LTLengthOf
@ConditionalPostconditionAnnotation(qualifier = LTLengthOf.class)
@interface EnsuresLTLIf {
public boolean result();
public String[] expression();
@JavaExpression
@QualifierArgument("value")
public String[] targetValue();
@JavaExpression
@QualifierArgument("offset")
public String[] targetOffset();
}
// Precondition for LTLengthOf
@PreconditionAnnotation(qualifier = LTLengthOf.class)
@interface RequiresLTL {
public String[] value();
@JavaExpression
@QualifierArgument("value")
public String[] targetValue();
@JavaExpression
@QualifierArgument("offset")
public String[] targetOffset();
}
class Base {
@EnsuresMinLen(value = "#1", targetValue = 10)
void minLenContract(int[] a) {
if (a.length < 10) throw new RuntimeException();
}
@EnsuresMinLen(value = "#1", targetValue = 10)
// :: error: (contracts.postcondition.not.satisfied)
void minLenWrong(int[] a) {
if (a.length < 9) throw new RuntimeException();
}
void minLenUse(int[] b) {
minLenContract(b);
int @MinLen(10) [] c = b;
}
public int b, y;
@EnsuresLTLIf(
expression = "b",
targetValue = {"#1", "#1"},
targetOffset = {"#2 + 1", "10"},
result = true)
boolean ltlPost(int[] a, int c) {
if (b < a.length - c - 1 && b < a.length - 10) {
return true;
} else {
return false;
}
}
@EnsuresLTLIf(expression = "b", targetValue = "#1", targetOffset = "#3", result = true)
// :: error: (flowexpr.parse.error)
boolean ltlPostInvalid(int[] a, int c) {
return false;
}
@RequiresLTL(
value = "b",
targetValue = {"#1", "#1"},
targetOffset = {"#2 + 1", "-10"})
void ltlPre(int[] a, int c) {
@LTLengthOf(value = "a", offset = "c+1") int i = b;
}
void ltlUse(int[] a, int c) {
if (ltlPost(a, c)) {
@LTLengthOf(value = "a", offset = "c+1") int i = b;
ltlPre(a, c);
}
// :: error: (assignment.type.incompatible)
@LTLengthOf(value = "a", offset = "c+1") int j = b;
}
}
class Derived extends Base {
public int x;
@Override
@EnsuresLTLIf(
expression = "b ",
targetValue = {"#1", "#1"},
targetOffset = {"#2 + 1", "11"},
result = true)
boolean ltlPost(int[] a, int d) {
return false;
}
@Override
@RequiresLTL(
value = "b ",
targetValue = {"#1", "#1"},
targetOffset = {"#2 + 1", "-11"})
void ltlPre(int[] a, int d) {
@LTLengthOf(
value = {"a", "a"},
offset = {"d+1", "-10"})
// :: error: (assignment.type.incompatible)
int i = b;
}
}
class DerivedInvalid extends Base {
public int x;
@Override
@EnsuresLTLIf(
expression = "b ",
targetValue = {"#1", "#1"},
targetOffset = {"#2 + 1", "9"},
result = true)
// :: error: (contracts.conditional.postcondition.true.override.invalid)
boolean ltlPost(int[] a, int c) {
// :: error: (contracts.conditional.postcondition.not.satisfied)
return true;
}
@Override
@RequiresLTL(
value = "b ",
targetValue = {"#1", "#1"},
targetOffset = {"#2 + 1", "-9"})
// :: error: (contracts.precondition.override.invalid)
void ltlPre(int[] a, int d) {
@LTLengthOf(
value = {"a", "a"},
offset = {"d+1", "-10"})
int i = b;
}
}
}
|