Building

Overview

When you start using JContractS you have to add a few additional steps to your build process. This is because JContractS and your other projects need differently built JAR files at different times. Below is a dataflow diagram of how your Java sources are transformed to, in this case, four sets of compiled classes. Each set has its own purpose:

Data flow from Java sources to multiple sets of compiled classes

The leftmost branch of the diagram represents the standard compilation of the Java sources. No changes are needed here. You can use the result of this branch if you do not want to use contracts at all.

As you can see each configuration of enabled contracts (preconditions and/or postconditions and/or invariants) needs its own branch of instrumented sources: the 2nd and fourth branches.

In addition a set of repository classes is generated. The repository classes are used by projects depending on this project to read the contracts of superclasses without needing access to the source code. The repository classes are the same for all configurations of enabled contracts.

Steps

The Java compiler and JContractS in the above diagram have to be applied in the following order:

  1. Java compiler: compile the Java sources to classes without contracts.
  2. JContractS config (n times): generate the instrumented sources and repository sources.
  3. Java compiler config (n times): compile the instrumented sources to classes with contracts.
  4. Java compiler repo: compile the repository sources to classes.

Classpaths

Each step needs its own specific classpath. What is needed on the classpath is detailed below.

Java compiler

The following items must be on the classpath passed to the Java compiler:

JContractS config

The following items must be on the classpath used to run JContractS:

Java compiler config

The following items must be on the classpath passed to the Java compiler:

Java compiler repo

The following items must be on the classpath passed to the Java compiler:

Example steps

Project without contract-enabled dependencies (prj1)

Java compiler:

javac -classpath somelib.jar;target\classes
    -sourcepath src
    -d target\classes
    src\com\acme\prj1\*.java

JContractS config:

java -classpath jcontracts.jar;log4j.jar;somelib.jar;target\classes
    net.sf.jcontracts.icontract.Tool
    -menabled-contracts
    -otarget\config-src\@p\@f.java
    -ktarget\repo-src\@p
    src\*.java

Java compiler config:

javac -classpath somelib.jar;target\config-classes
    -sourcepath target\config-src
    -d target\config-classes
    target\config-src\com\acme\prj1\*.java

Java compiler repo:

javac -classpath target\repo-classes
    -sourcepath target\repo-src
    -d target\repo-classes
    target\repo-src\com\acme\prj1\*.java

Project with contract-enabled dependencies (prj2)

Java compiler:

javac -classpath prj1.jar;somelib.jar;target\classes
    -sourcepath src
    -d target\classes
    src\com\acme\prj2\*.java

JContractS config:

java -classpath jcontract.jar;log4j.jar;prj1-config.jar;prj1-repo.jar;somelib.jar;target\classes
    net.sf.jcontracts.icontract.Tool
    -mconfig
    -otarget\config-src\@p\@f.java
    -ktarget\repo-src\@p
    src\*.java

Java compiler config:

javac -classpath prj1-config.jar;somelib.jar;target\config-classes
    -sourcepath target\config-src
    -d target\config-classes
    target\config-src\com\acme\prj2\*.java

Java compiler repo:

javac -classpath prj1-repo.jar;target\repo-classes
    -sourcepath target\repo-src
    -d target\repo-classes
    target\repo-src\com\acme\prj2\*.java

The project page of JContractS at SourceForge.net