Contracts for Java (Cofoja) V1.0 in Eclipse

84 downloads 3087 Views 745KB Size Report
Contracts for Java (Cofoja) V1.0 in Eclipse. Disclaimer: • This tutorial is based on a tutorial by Fabian Steeg presented in his blog. • Tested on Eclipse Helios ...
Contracts for Java (Cofoja) V1.0 in Eclipse Disclaimer:  

This tutorial is based on a tutorial by Fabian Steeg presented in his blog. Tested on Eclipse Helios (3.6).

Dependencies 1. Latest cofoja snapshot - can be downloaded from here 2. The ASM package is also required and can be downloaded from: http://asm.ow2.org/ (Cofoja is being developed against version 3.3; A version higher than 3.2 is required.) 3. Java Development Toolkit (JDK) 6 or higher

1

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

Step by step tutorial 1. Make sure Eclipse is configured to run with JDK (Not JRE!) Edit eclipse.ini file to include the path of the JDK bin folder (must be before the -vmargs line). Add the following two lines: -vm

2

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

2. Start a new Java project (or import an existing one).

3

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

3. Create a new lib folder and place the cofoja-.jar & asm.jar in it. In our example, we’re using cofoja-1.0-r139.jar & asm-all-3.3.jar

4. Add the two .jar files to the build path by right-clicking the .jar files: Build Path→Add to Build Path

4

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

5. In order to enable Annotation Processing in Eclipse we will need to configure the project preferences. Right click the project: Properties→Java Compiler→Annotation Processing→Enable project specific settings

5

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

6. The annotation processor will need to be configures with the following parameters (Use the New… button to add these). You can use the predefined %PROJECT.DIR% variable which is always defined, and points to the project folder.  com.google.java.contract.classpath – Should point to the location of the cofoja-.jar file (In our example: %PROJECT.DIR%/lib/cofoja-1.0r139.jar)  com.google.java.contract.classoutput – Should point to the location of the .class files folder (Usually: %PROJECT.DIR%/bin)1  com.google.java.contract.sourcepath – Should point to the location of the source folders using cofoja annotations. (Usually: %PROJECT.DIR%/src)1

1

In cases where there are multiple folders use the “;” delimiter in Windows or “:” in Unix to separate the values.

6

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

7. Next the location of the annotation factory has to be defined. Under Java Compiler→Annotation Processing→Factory Path, you should enable the specific project settings and add the cofoja-.jar file.

8. Eclipse can now process the Annotations and alert syntactic errors

In this example we can see the “c” variable is not defined in our class or in the method.

7

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)

9. After fixing the errors, and writing some code (e.g. test cases, main, etc.) we need to tell eclipse to run them with Contracts enabled. For this we need to add –javaagent: to the VM arguments of our configuration.

10. Now when running, we’re made aware of pre/post-conditions violations

We can now fix our contracts / code accordingly.

8

Contracts for Java (Cofoja) V1.0 in Eclipse | Assaf Israel, Technion – Institute of Technology (2012)