From our friends from Google, there is a new opensource library for Java out there, Cofoja, Contracts for Java.
Here is a simple example of a Java class annotated with contracts:
interface Time {
...
@Ensures({
"result >= 0",
"result <= 23"<br/> })<br/> int getHour();</p><p>@Requires({<br/> "h >= 0",
"h <= 23"<br/> })<br/> @Ensures("getHour() == h")
void setHour(int h);
...
}
Here is what the contract for setHour regulates.
Calling method Satisfy precondition; Make sure 'h' is neither too small nor too large. (From postcondition) getHour() returns h.
Called method Satisfy postcondition; Must make getHour() return h. (From precondition) May assume h is neither too small nor too large.
Enjoy!
No comments:
Post a Comment