How do you use CodePro's contracts in Eclipse?

1.1k Views Asked by At

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?

2

There are 2 best solutions below

4
On

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/

2
On

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.