Supporting Design by Contract in Java.
Martin Lackner, Andreas Krall, Franz Puntigam · The Journal of Object Technology · 2002
Design by Contract is a valuable design method for trusted software components.Eiffel shows how to provide appropriate language support for it.However, no such concepts currently exist in Java.Full integration of them into Java may help to improve and guarantee the quality of Java classes.We briefly compare several approaches to extend Java in this way and present our model and a compiler that translates extended Java code into JVM byte code.Our Java extension integrates preconditions, postconditions, and invariants as in Eiffel while respecting the characteristics of Java.The evaluation shows that Design by Contract can be added efficiently to Java while keeping compatibility. APPROACHES TO DESIGN BY CONTRACT IN JAVAMost proposals use preprocessing techniques instead of a full language integration.We discuss some of them. JassJass [Ber99] supports different kinds of assertions like pre-and postconditions, class invariants, loop invariants, and checks at arbitrary locations in the code.Expressions in assertions must be purely declarative; no side effects such as assignment and instance creation are allowed.The programmer can use old field values and method results in postconditions.Fig. 1 shows an example of assertions in Jass.In the postcondition, Old.a is the old value of the field a.If an old value is used, the object is cloned at the beginning of the method.In the postcondition, the field value is compared to its counterpart in the clone.