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
|
import org.checkerframework.checker.lock.qual.EnsuresLockHeld;
import org.checkerframework.checker.lock.qual.EnsuresLockHeldIf;
import org.checkerframework.checker.lock.qual.GuardedBy;
import org.checkerframework.checker.lock.qual.Holding;
class MyReentrantLock {
final Object myfield = new Object();
@Holding("myfield")
@EnsuresLockHeld("this")
void lock() {
this.lock();
}
@EnsuresLockHeld("this")
void lock2() {
this.lock2();
}
@Holding("myfield")
void notLock() {}
boolean b = false;
@EnsuresLockHeldIf(expression = "this", result = true)
boolean tryLock() {
if (b) {
lock2();
return true;
}
return false;
}
}
class ThisPostCondition {
final MyReentrantLock myLock = new MyReentrantLock();
@GuardedBy("myLock") Bar bar = new Bar();
@Holding("myLock.myfield")
void lockTheLock() {
myLock.lock();
bar.field.toString();
}
void lockTheLock2() {
myLock.lock2();
bar.field.toString();
}
void doNotLock() {
// :: error: (lock.not.held)
bar.field.toString();
}
void tryTryLock() {
if (myLock.tryLock()) {
bar.field.toString();
} else {
// :: error: (lock.not.held)
bar.field.toString();
}
}
}
class Bar {
Object field;
}
|