Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use CodePro's contracts in Eclipse?

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?

like image 624
Kevin Avatar asked Nov 04 '22 11:11

Kevin


2 Answers

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.

like image 161
Tudor Avatar answered Nov 09 '22 08:11

Tudor


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/

like image 45
Alexis Dufrenoy Avatar answered Nov 09 '22 08:11

Alexis Dufrenoy