OCL for Java

Last update: 2005-12-28
 

Home

Dokumentation

  - Introduction

  - Technology

  - Example

  - Assertion styles

  - OCL

  - Usage

  - Eclipse

  - FAQ

  - References

Download

Project

Overview

OCL4Java is made for Java developer that want to increase the code quality by utilizing the Design-By-Contract principles. Quality means in this context code that

  • actually reflects the requirements as expressed in the specification
  • is robust during runtime

By testing method parameters and return values against the specification (during runtime) a more robust application can be achieved, as invalid values are systematically avoided.

OCL4Java evaluates OCL expressions in the source code and enhances the code by

  1. Java assertions
  2. Java exceptions
  3. Logging of errors via System.err or commons-logging (Apache)
  4. Your own code

Which type of code enhancement actually is generated - the so called Assertion-Style - can be determined on project and method level.

The following small examples shows a method with a constraint (lines 1 and 2). OCL4Java generates to given an example an assertion method (lines 9-21) and the call of this new method on the original method (lines 4 and 5).

The generated code is marked with gray background.

1   @Constraint("context TestClassPerson::setAge(anAge:Integer)\n " 
2       "inv: age < 120"
3   public void setAge(int anAge){
4     assert assertPreCondition_45ae89dd_for_method_setAge(anAge
5       "OCL-Assertion violated: context TestClassPerson::setAge(anAge:Integer)\n inv: age < 120";
6     age = anAge;
7   }
8
9  * This method checks the OCL Precondition <b/>
10  * 'context TestClassPerson::setAge(anAge:Integer)
11  *  inv: age < 120'<b/> 
12  * for method setAge
13  */
14   boolean assertPreCondition_45ae89dd_for_method_setAge(  int anAge){
15     final OclAnyImpl tudOclNode0 = Ocl.toOclAnyImpl(Ocl.getFor(this));
16     final OclInteger tudOclOpPar0 = Ocl.toOclInteger(Ocl.getFor(anAge));
17     final OclInteger tudOclNode1 = Ocl.toOclInteger(tudOclNode0.getFeature("age"));
18     final OclInteger tudOclNode2 = new OclInteger(120);
19     final OclBoolean tudOclNode3 = tudOclNode1.isLessThan(tudOclNode2);
20     return tudOclNode3.isTrue();
21   }

However, not the original source code is changed, but for each source file a second enhanced source file is generated. A discussion and justification for this design you can find in the chapter "Technology->Remarks".