0

如何正确调用method()from main(..)

class LockCheckerTest {
    static class Y {
        final Lock z = new ReentrantLock(true);
    }

    private final static Date x = new Date((long) (System.currentTimeMillis() * Math.random()));
    private final static Y y = new Y();

    @Holding({"x", "y.z"})
    @ReleasesNoLocks
    static void method() {
        System.out.println(x);
    }

    public static void main(String[] args) {
        synchronized (x) {  // acquire intrinsic lock of 'x' 
            synchronized (y) { // locking 'y' is not required, just trying to compile
                y.z.lock(); // acquire explicit lock 'y.z'
                method();  // ERROR
                y.z.unlock();
            }
        }
    }
}

错误:(37, 23) java: [contracts.precondition.not.satisfied] 对方法 'method()' 的无人看管的调用需要保持 'Holding.yz'

4

1 回答 1

1

看起来这是 Checker Framework 中的一个错误:它知道如何处理声明为 的变量ReentrantLock,但不知道如何处理声明为其 interface 的变量Lock

该错误已在Git 版本控制存储库中修复,但未在当前版本 2.1.1中修复。

使用 Git 的版本,在解决一个问题后,您的代码会为我检查类型:您的main方法需要注释为@MayReleaseLocks.

于 2016-08-18T03:14:55.203 回答