java - JML 后置条件包含类方法调用

标签 java contracts jml post-conditions

类方法的 JML 后置条件是否可以包含对另一个方法调用的调用

例如我有这个类:

public class A
{
    public int doA(x)
    { ... }

    public int doB(int x, int y)
    { ... }
}

对于 doB 的后置条件,我可以:确保 doA(x) = doA(y)

最佳答案

是的,前提是调用的方法不包含副作用并且声明为纯方法:

The /@ pure @/ comment indicates that peek() is a pure method. A pure method is one that doesn't have side effects. JML only allows assertions to use pure methods. We declare peek() to be pure so it can be used in the postcondition of pop(). If JML allowed non-pure methods in assertions then we could inadvertently write specifications that had side effects. This could result in code that works when compiled with assertion checking enabled but doesn't work when assertion checking is disabled.

http://www.ibm.com/developerworks/java/library/j-jml/index.html

public class A
{
    public /*@ pure @*/ int doA(int x)
    { ... }

    //@ requires ...
    //@ ensures doA(x) == doA(y)
    public int doB(int x, int y)
    { ... }
}

关于java - JML 后置条件包含类方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13602989/

相关文章:

Java BigInteger分解: division and multiplication differ

interface - 如何在 D 接口(interface)中有意义地使用前置条件契约?

java - 我们如何将 JML (openJML) 应用于 Java 代码?

java - OpenJML 中的编程静态检查

app-store - 公司是否应该阻止员工在空闲时间在应用商店发布应用?

java - JML 的简单解析器

java - Facebook:自动在其他人的墙上发帖

java - 在java中按优先级队列对学生的分数进行排序

java - 使用Java实现堆栈

java - 将 Cofoja 与 Wicket 一起使用(甚至仅与 Maven 一起使用)