Add Contracts to Your Code:
- Add JavaFinePrint.jar and log4J jar to your classpath.
- Add the proper import statements to the code you want to define contracts for.
- import net.sf.fineprint.Invar;
- import net.sf.fineprint.Pre;
- import net.sf.fineprint.Post;
- Add invariants above your class declaration.
- Add preconditions and postcondition above your method declarations.
- Example code:
Generate Contracted Code:
- Option 1) Use the Java Annotation Processing Tool.
- Launch the apt.exe tool bundled with your Java 5 JDK with the following options
- -nocompile : This is required because the generated file will have the same name as the original and you will get a build error for having two files of the same name in your path.
- -classpath [YOUR_CLASSPATH] : This is your classpath, including JavaFinePrint.jar.
- -s [YOUR_TARGET_DIRECTORY] : The directory where you want the generated files to be placed.
- -factory net.sf.fineprint.apt.ContractProcessorFactory : This tells the apt tool what annotation processor factory to use.
- [YOUR LIST OF FILES] : This is a list of .java files to be processed by the apt tool.
- Example launch from within Eclipse IDE.

- Option 2) Use the Java Fine Print driver.
- Call the Java Fine Print driver.
- java -classpath [YOUR_CLASSPATH] net.sf.fineprint.tool.Main -f [YOUR_FILTER] -d [DESTINATION] -s [SOURCE]
- -f [YOUR_FILTER] : This specifies the filter and is an optional parameter. Your can use the filter to exclude a class or package from contract generation by including it's fully qualified name here.
- Example: -f my.test.MyTestClass,my.other.test.*
- This filter will exclude the class MyTestClass and all classes in or below the my.other.test package.
- -d [DESTINATION] : This is the absolute path of the directory to place the generated files in.
- -s [SOURCE] : This is the source java file or directory. If a directory is provided, all java files in the directory and subdirectories are recursively processed.
- Example launch from within Eclipse IDE.

- Option 3) Create a builder for Eclipse.
- In Eclipse create an External Tool (Run->External Tools) to launch either the apt tool or the Java Fine Print driver as shown in the above examples.
- Select your project in the Package Explorer.
- From the menu click Project->Properties.
- Choose Builders in the list on the left and click the Import button on the right.
- Choose the external tool you created from the list and click OK.

- You can further refine your builder by selecting it from the list of builders and clicking the Edit button.
- Tip: You can use Eclipse variables to specify parameters and tune this custom builder to perform incremental builds as you save your files.
- Tip: Create a seperate Eclipse project to place your generated files in.
- Tip: Set your custom builder to refresh the destination project after it is run.