如何修复 Checker Framework 错误 java:[contracts.precondition.not.satisfied] 对方法 'method()' 的无人看守调用要求 'Holding.y.z' 保持

How to fix Checker Framework Error java: [contracts.precondition.not.satisfied] unguarded call to method 'method()' requiring 'Holding.y.z' to be held

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

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.y.z' 被保留

看起来这是 Checker Framework 中的一个错误:它知道如何处理声明为 ReentrantLock, but not how to handle variables declared as its interface Lock.

的变量

那个错误是 fixed in the Git version control repository but not in the current release, version 2.1.1

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