Using Jcontract
You can use Jcontract to instrument and compile any .java file with or without Design by Contract comments.
Using Jcontract involves two main steps:
- Compiling your code with the special Jcontract compiler.
- Running the program in the normal manner.
These steps are described below.
Before you use Jcontract for the first time, you need to set the environment for Jcontract as described in Window Installation and Setup or UNIX Installation and Setup.
Adding Contracts to Your Code
Jcontract can be used to perform some checks on code without DbC contracts, but to get the full benefit of Jcontract, you should add DbC contracts to your code.
For a general introduction to Design by Contract, see About Design by Contract. For information on adding Design by Contract comments to your code, see The Design by Contract Specification Language.
Running Jcontract on Files With DbC Contracts
If you use Jcontract with code that contains DbC comments, it will instrument the comments and check the contracts.
To use Jcontract on code that contains contracts:
- Compile the program classes using the
dbc_javac command instead of the javac command.
The dbc_javac compiler will then instrument the DbC comments as it compiles the program's classes. When this process is completed, Jcontract will report the number of files compiled and the number of files instrumented.
For example, to compile and instrument the DbC comments in Example.java, you could enter the following command at the prompt:
dbc_javac Example.java
For more information about the dbc_javac command, see dbc_javac.
- Run the program in the normal manner.
The Jcontract runtime will check the contracts and report progress and contract violations in the Jcontract Monitor (or in any other output location you have selected).
For example, to run Example.class, you could enter the following command at the prompt:
java Example
Note: You must have the `java' program on the environment's path in order to run your program with `java'.
Running Jcontract on Files Without DbC Contracts
If you use Jcontract on code without DbC comments (i.e. code that doesn't yet contain @pre/@post/@invariant/@concurrency, etc. tags), you can have it check whether any methods are executed concurrently by more than one thread.
To use Jcontract on code that does not contain contracts:
- Compile the program classes using the following command instead of
javac :
dbc_javac -Zdefault_concurrency sequential
This will add code to check that no methods are executed concurrently by more than one thread.
- Run the program in the regular manner.
If more that one thread is executing a method at a given time, a contract violation will be reported in the Jcontract Monitor (or in any other output location you have selected).
|