150 likes | 275 Vues
This document presents a tool called "Design by Contract" developed for Java, authored by Jose Ohad Barzilay in April 2004. Intended exclusively for academic purposes, this resource remains in ALFA status and serves as an external tool for IDEs or command-line configurations. It encompasses a comprehensive framework employing AspectJ, Doclet, and ANTLR for contract specification and validation. The contents include practical examples such as stack implementations with assertions, enabling students to deepen their understanding of preconditions, postconditions, and invariants in software design.
E N D
Design by Contractusing Jose Ohad Barzilay April 2004, IDC
About Jose • A Design by Contract tool written for java • Still in ALFA status • Similar tools: • iContract • JContract • JMSAssert • JML
More About Jose • Uses AspectJ • Uses Doclet • Uses ANTLR
Getting Jose • For the use of this course students only and only for academic purposes • Command line configuration or as an IDE external tool • Executable jar • Get it this weekend from the course discussion group
Stack with Assertions (1) class STACK1 [G] feature -- Access count: INTEGER -- Number of stack elements item: G is -- Top element require not empty do ... end
Stack with Assertions (1) publicclassStack1 { publicintcount() { returnelements.size(); } /** *@require!empty(),"NotEmpty" */ publicObjectitem() { returnelements.firstElement(); } protectedVectorelements=null; }
Stack with Assertions (2) feature -- Status report empty: BOOLEAN is -- Is stack empty? do ...end full: BOOLEAN is -- Is stack representation full? do ...end
Stack with Assertions (2) publicbooleanempty() { returncount()==0; } publicbooleanfull() { returnelements.capacity()==count(); }
Stack with Assertions (3) feature -- Element change put (x: G) is -- Add x on top. require not full do ... ensure not empty item = x count = old count + 1 end
Stack with Assertions (3) /** *@require!full(),"NotFull" *@ensure!empty(),"NotEmpty" *@ensureitem()==x,"item()equalsx" *@ensurecount()==$prev(count())+1, *"count()incremented" */ publicvoidput(Objectx) { elements.add(0,x); }
Stack with Assertions (4) remove is -- Remove top element. require not empty do ... ensure not full count = old count – 1 end end
Stack with Assertions (4) /** *@require!empty(),"NotEmpty" *@ensure!full(),"NotFull" *@ensurecount()==$prev(count())-1, * "count()decremented" */ publicvoidremove() { elements.remove(0); }
Class level Assertions (5) /** *@invariantelements.size()>=0, * "nonnegativesize" */ publicclassStack1 { ... }
Jose keywords • Precondition • @precondition • @pre • @ require • Postcondition • @ postcondition • @ post • @ ensure • Invarinat • @ invarinat • @ inv
Jose keywords • Return value • $ret • Old value • $prev(<expr>) • $prev(<type>;<expr>)