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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
import org.checkerframework.checker.lock.qual.GuardSatisfied;
import org.checkerframework.checker.lock.qual.GuardedBy;
import org.checkerframework.checker.lock.qual.GuardedByUnknown;
import org.checkerframework.checker.lock.qual.MayReleaseLocks;
public class GuardSatisfiedTest {
void testGuardSatisfiedIndexMatching(
@GuardSatisfied GuardSatisfiedTest this,
@GuardSatisfied(1) Object o,
@GuardSatisfied(2) Object p,
@GuardSatisfied Object q) {
methodToCall1(o, o);
methodToCall1(p, p);
// :: error: (guardsatisfied.parameters.must.match)
methodToCall1(o, p);
// :: error: (guardsatisfied.parameters.must.match)
methodToCall1(p, o);
}
// Test defaulting of parameters - they must default to @GuardedBy({}), not @GuardSatisfied
void testDefaulting(Object mustDefaultToGuardedByNothing, @GuardSatisfied Object p) {
// Must assign in this direction to test the defaulting because assigning a RHS of
// @GuardedBy({}) to a LHS @GuardSatisfied is legal.
// :: error: (assignment.type.incompatible)
mustDefaultToGuardedByNothing = p;
@GuardedBy({}) Object q = mustDefaultToGuardedByNothing;
}
void testMethodCall(
@GuardSatisfied GuardSatisfiedTest this,
@GuardedBy("lock1") Object o,
@GuardedBy("lock2") Object p,
@GuardSatisfied Object q) {
// Test matching parameters
// :: error: (lock.not.held)
methodToCall1(o, o);
// :: error: (lock.not.held) :: error: (guardsatisfied.parameters.must.match)
methodToCall1(o, p);
// :: error: (lock.not.held)
methodToCall1(p, p);
synchronized (lock2) {
// :: error: (lock.not.held)
methodToCall1(o, o);
// :: error: (guardsatisfied.parameters.must.match) :: error: (lock.not.held)
methodToCall1(o, p);
methodToCall1(p, p);
synchronized (lock1) {
methodToCall1(o, o);
// :: error: (guardsatisfied.parameters.must.match)
methodToCall1(o, p);
methodToCall1(p, p);
}
}
// Test a return type matching a parameter.
// :: error: (lock.not.held)
o = methodToCall2(o);
// :: error: (lock.not.held) :: error: (assignment.type.incompatible)
p = methodToCall2(o);
// :: error: (lock.not.held)
methodToCall2(o);
// :: error: (lock.not.held)
methodToCall2(p);
synchronized (lock2) {
// :: error: (lock.not.held)
o = methodToCall2(o);
// :: error: (lock.not.held) :: error: (assignment.type.incompatible)
p = methodToCall2(o);
// :: error: (lock.not.held)
methodToCall2(o);
methodToCall2(p);
}
synchronized (lock1) {
o = methodToCall2(o);
// :: error: (assignment.type.incompatible)
p = methodToCall2(o);
methodToCall2(o);
// :: error: (lock.not.held)
methodToCall2(p);
}
// Test the receiver type matching a parameter
// Two @GS parameters with no index are incomparable (as is the case for 'this' and 'q')
// :: error: (guardsatisfied.parameters.must.match)
methodToCall3(q);
// :: error: (guardsatisfied.parameters.must.match) :: error: (lock.not.held)
methodToCall3(p);
synchronized (lock1) {
// Two @GS parameters with no index are incomparable (as is the case for 'this' and 'q')
// :: error: (guardsatisfied.parameters.must.match)
methodToCall3(q);
// :: error: (guardsatisfied.parameters.must.match) :: error: (lock.not.held)
methodToCall3(p);
synchronized (lock2) {
// Two @GS parameters with no index are incomparable (as is the case for 'this' and
// 'q')
// :: error: (guardsatisfied.parameters.must.match)
methodToCall3(q);
// :: error: (guardsatisfied.parameters.must.match)
methodToCall3(p);
}
}
// Test the return type matching the receiver type
methodToCall4();
}
// Test the return type NOT matching the receiver type
void testMethodCall(@GuardedBy("lock1") GuardSatisfiedTest this) {
@GuardedBy("lock2") Object g;
// :: error: (lock.not.held)
methodToCall4();
// TODO: lock.not.held is getting swallowed below
// error (assignment.type.incompatible) error (lock.not.held)
// g = methodToCall4();
// Separate the above test case into two for now
// :: error: (lock.not.held)
methodToCall4();
// The following error is due to the fact that you cannot access "this.lock1" without first
// having acquired "lock1". The right fix in a user scenario would be to not guard "this"
// with "this.lock1". The current object could instead be guarded by "<self>" or by some
// other lock expression that is not one of its fields. We are keeping this test case here
// to make sure this scenario issues a warning.
// :: error: (lock.not.held)
synchronized (lock1) {
// :: error: (assignment.type.incompatible)
g = methodToCall4();
}
}
// :: error: (guardsatisfied.return.must.have.index)
@GuardSatisfied Object testReturnTypesMustMatchAndMustHaveAnIndex1(@GuardSatisfied Object o) {
// If the two @GuardSatisfied had an index, this error would not be issued:
// :: error: (guardsatisfied.assignment.disallowed)
return o;
}
@GuardSatisfied(1) Object testReturnTypesMustMatchAndMustHaveAnIndex2(@GuardSatisfied(1) Object o) {
return o;
}
@GuardSatisfied(0) Object testReturnTypesMustMatchAndMustHaveAnIndex3(@GuardSatisfied(0) Object o) {
return o;
}
// @GuardSatisfied is equivalent to @GuardSatisfied(-1).
// :: error: (guardsatisfied.return.must.have.index)
@GuardSatisfied Object testReturnTypesMustMatchAndMustHaveAnIndex4(@GuardSatisfied(-1) Object o) {
// If the two @GuardSatisfied had an index, this error would not be issued:
// :: error: (guardsatisfied.assignment.disallowed)
return o;
}
@GuardSatisfied(1) Object testReturnTypesMustMatchAndMustHaveAnIndex5(@GuardSatisfied(2) Object o) {
// :: error: (return.type.incompatible)
return o;
}
// :: error: (guardsatisfied.return.must.have.index)
@GuardSatisfied Object testReturnTypesMustMatchAndMustHaveAnIndex6(@GuardSatisfied(2) Object o) {
// :: error: (return.type.incompatible)
return o;
}
void testParamsMustMatch(@GuardSatisfied(1) Object o, @GuardSatisfied(2) Object p) {
// :: error: (assignment.type.incompatible)
o = p;
}
void methodToCall1(
@GuardSatisfied GuardSatisfiedTest this,
@GuardSatisfied(1) Object o,
@GuardSatisfied(1) Object p) {}
@GuardSatisfied(1) Object methodToCall2(@GuardSatisfied GuardSatisfiedTest this, @GuardSatisfied(1) Object o) {
return o;
}
void methodToCall3(@GuardSatisfied(1) GuardSatisfiedTest this, @GuardSatisfied(1) Object o) {}
@GuardSatisfied(1) Object methodToCall4(@GuardSatisfied(1) GuardSatisfiedTest this) {
return this;
}
final Object lock1 = new Object(), lock2 = new Object();
void testAssignment(@GuardSatisfied Object o) {
@GuardedBy({"lock1", "lock2"}) Object p = new Object();
// :: error: (lock.not.held)
o = p;
synchronized (lock1) {
// :: error: (lock.not.held)
o = p;
synchronized (lock2) {
o = p;
}
}
}
// Test disallowed @GuardSatisfied locations.
// Whenever a disallowed location can be located within a method return type,
// receiver or parameter, test it there, because it's important to check
// that those are not mistakenly allowed, since annotations
// on method return types, receivers and parameters are allowed.
// By definition, fields and non-parameter local variables cannot be
// in one of these locations on a method declaration, but other
// locations can be.
// :: error: (guardsatisfied.location.disallowed)
@GuardSatisfied Object field;
// :: error: (guardsatisfied.location.disallowed)
void testGuardSatisfiedOnArrayElementAndLocalVariable(@GuardSatisfied Object[] array) {
// :: error: (guardsatisfied.location.disallowed)
@GuardSatisfied Object local;
}
// :: error: (guardsatisfied.location.disallowed)
<T extends @GuardSatisfied Object> T testGuardSatisfiedOnBound(T t) {
return t;
}
class MyParameterizedClass1<T extends @GuardedByUnknown Object> {
void testGuardSatisfiedOnReceiverOfParameterizedClass(
@GuardSatisfied MyParameterizedClass1<T> this) {}
void testGuardSatisfiedOnArrayOfParameterizedType(
MyParameterizedClass1<T> @GuardSatisfied [] array) {}
void testGuardSatisfiedOnArrayComponentOfParameterizedType(
// :: error: (guardsatisfied.location.disallowed)
@GuardSatisfied MyParameterizedClass1<T>[] array) {}
};
void testGuardSatisfiedOnWildCardExtendsBound(
// :: error: (guardsatisfied.location.disallowed)
MyParameterizedClass1<? extends @GuardSatisfied Object> l) {}
void testGuardSatisfiedOnWildCardSuperBound(
// :: error: (guardsatisfied.location.disallowed)
MyParameterizedClass1<? super @GuardSatisfied Object> l) {}
@GuardSatisfied(1) Object testGuardSatisfiedOnParameters(
@GuardSatisfied GuardSatisfiedTest this,
Object @GuardSatisfied [] array,
@GuardSatisfied Object param,
@GuardSatisfied(1) Object param2) {
return param2;
}
void testGuardSatisfiedOnArray1(Object @GuardSatisfied [][][] array) {}
// :: error: (guardsatisfied.location.disallowed)
void testGuardSatisfiedOnArray2(@GuardSatisfied Object[][][] array) {}
// :: error: (guardsatisfied.location.disallowed)
void testGuardSatisfiedOnArray3(Object[] @GuardSatisfied [][] array) {}
// :: error: (guardsatisfied.location.disallowed)
void testGuardSatisfiedOnArray4(Object[][] @GuardSatisfied [] array) {}
}
class Foo {
@MayReleaseLocks
void m1() {}
@MayReleaseLocks
// :: error: (guardsatisfied.with.mayreleaselocks)
void m2(@GuardSatisfied Foo f) {
// :: error: (method.invocation.invalid)
f.m1();
}
@MayReleaseLocks
void m2_2(Foo f) {
f.m1();
}
void m3(@GuardSatisfied Foo f) {
// :: error: (method.guarantee.violated) :: error: (method.invocation.invalid)
f.m1();
}
@MayReleaseLocks
void m4(Foo f) {
f.m1();
}
@MayReleaseLocks
void m5(Foo f) {
m3(f);
}
}
|