5

I thought I understood CodePro's contracts, but they seem to have no effect. For example:

public class ContractTest {

    private int number;

    /**
     * @pre inputNumber > 0
     * 
     * Alternatively:
     * @post number > 0
     */
    public void setNumber(int inputNumber) {
        number = inputNumber;
    }

    public int getNumber() {
        return number;
    } 

    public static void main(String args[]) {
        ConditionsTest conditionsTest = new ConditionsTest();
        conditionsTest.setNumber(-5);
        System.out.println("Number: " + conditionsTest.getNumber());
    }
}

Running the main(String[]) method causes:

number: -5

to be printed. There were no compile warning (expected), and no exceptions thrown. Also, the junit test methods generated by CodePro were not affected by the contracts.

So how do you use CodePro's contracts?

Kevin
  • 4,070
  • 4
  • 45
  • 67
  • Not an answer to your question, but do you know that you can use asserts to achieve the same thing? See for example http://www.deitel.com/articles/java_tutorials/20060106/Assertions.html Best thing is that asserts are part of core java. – Fredrik Nov 22 '11 at 11:29
  • 1
    Asserts are runtime, and I hoping that the contracts would be compile time. The asserts will only alert the programmer if the code is run, and often not all paths of a program can be tested. Also, if the CodePro contracts are similar to cofaja contracts, then the contracts can be inherited. – Kevin Nov 23 '11 at 04:15
  • @Mowgli For most interesting contracts it's basically impossible (I'd say undecidable but I'm not 100% sure there) to find out if the conditions hold without running the actual code. And then there's the problem of input. Hence I can't imagine any library trying to do this as it would only work in toy example. (Well not strictly true, model checking does something somewhat similar to this actually) – Voo Nov 23 '11 at 13:37
  • I would have thought it would have been similar to type checking and static analysis. But I don't know, as there is little documentation and I can't get it working. – Kevin Nov 23 '11 at 23:44

2 Answers2

1

Are you sure you are supposed to get compilation warnings? From what I've seen, contracts in CodePro are only meant to generate JUnit test cases with the proper asserts, not to give warnings.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • No, I'm not sure if I should get compilation warnings. I can't get it working, and there is hardly any documentation. But even working for junit would be really useful, as the generated junit code often generates useless tests that would benifit from a little guidance. – Kevin Nov 23 '11 at 23:46
  • Yep. And they directly contradict the contracts :( – Kevin Nov 24 '11 at 10:47
1

If you want to include design by contracts in your java development, Cofoja is definitely a better choice:

http://code.google.com/p/cofoja/

Edit: Setting up Cofoja in Eclipse:

http://fsteeg.com/2011/02/07/setting-up-contracts-for-java-in-eclipse/

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124