OOSC - JMSAssert
Design By Contract A powerful technique for writing reliable software. Specifying the software purpose with the implementation. Key elements: Invariant Preconditions Postconditions
Design By Contract Precondition – The constraints under which the routine will function properly. Postconditions – The state of the class after the routine execution The Contract: If you call routine R() with the preconditions satisfied, R() will return satisfying the postconditions. Invariant – Always holds
When are condition checked?
JMS Syntax - Invariant Invariant May access all class members or its direct/indirect bases, including private members May appear in any JavaDoc comment Preferable in the class comment
JMS Syntax - Preconditions Precondition JavaDoc preceding the respective method May reference class members and arguments markers are conjugated (AND)
JMS Syntax - Postconditions Postconditions JavaDoc preceding the respective method May use $prev(expression) to access the value at the method entry. May use $ret to denote method’s return value markers are conjugated (AND)
Example /** !isEmpty() (top == $prev(top- 1)) $ret == elems[top] !isFull */ public synchronized Object pop() { return elems[--top]; } Precondition Postconditions
JMS Syntax - General Order has no – for complicated conditions Recursion – as expected, on every call Inner classes can access outer classes’ members Anonymous classes – specify invariant in one of its methods
JMSAssert Installation Run the jmssetup-1.02.exe installation file The following lines are added to the path: REM Next two lines are added by JMSAssert SET CLASSPATH=%CLASSPATH%;C:\PROGRA~1\JMSASS~1.0\ bin\mmsclasses.jar; SET PATH=%PATH%;C:\PROGRA~1\JMSASS~1.0\bin; Copy the “ classic ” directory from “..\jdk1.3.1\jre\bin\ ” to the directory: “C:\Program Files\JavaSoft\JRE\1.3\bin\”
Setup (cont.) Your JavaSoft directory should look like: If you use jdk 1.2.2, you will have a 1.2 directory
JMSAssert – how does it work Annotate source code with assertions Compile your code using javac (as usual) Preprocess the code using jmsassert: creates contract files ( *.jms ) and a Startup.jms file. *.jms files contain java code for the assertions. Execute using: jmsjava Startup to check assertions. jmsjava makes sure method assertions are called before/after the method invocation.
JMS Execution “ jmsassert ” – generates help text “ jmsassert –s ” – generate assertions for a class file “ jmsassert –r –s. ” – generate assertions for all class files in the directory and sub-dirs. (use for packages) “ javac ” – compile “ jmsjava Startup ” - execute and check assertions
Annotate source with assertions Preprocess to generate assertion files Jmsassert –s Compile Java file javac Execute using jmsjava MyStack.java Startup.jms default_MyStack.jms default_MyStack_StackEnum.jms MyStack.class MyStack$StackEnum.class Jmsjava Startup StackTest.class Stack Demo files Execution process
Notes Execute these steps form the command line! Make sure your CLASSPATH environment variable contains the current directory. Add “ CLASSPATH=%CLASSPATH%;.; ” to autoexec.bat.