In association with heise online

08 February 2011, 11:05

Google open sources Contracts for Java

  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit

Google has announced the open sourcing, under the LGPL, of Contracts for Java (cofoja), which implements a technique popularised by the Eiffel programming language. Design, or Programming, by Contract is a technique where the interfaces of software modules include contracts consisting of preconditions, postconditions and invariant expressions. These contracts can be checked at runtime and a contract violation is flagged as a runtime error. The aim is to make problems easier to locate as they occur at the problematic modules interface, rather than causing problems in other modules and corrupting their operation. An example using cofoja would be:

interface Time {


@Ensures({ "result >= 0", "result <= 23" })
int getHour();

@Requires({ "h >=0", "h <= 23" })
void setHour(int h)



This example defines a contract where the result of the getHour() method must be in the range 0 and 23 and that the setHour() method is not passed a value outside the same range; after execution on the method, getHour() will return the value that was passed to setHour().

The contracts should also provide the developer with more information about the operation of code. Cofoja is a 20% time Google project and has been implemented as Java assertions which specify contracts as boolean expressions. Cofoja contracts can be inherited from interfaces and classes and be enabled selectively at runtime; by default they are disabled and are turned on with a JVM argument. A paperPDF on the work involved in creating Cofoja is available, and the project's code can be found on the cofoja Google Code project page.


Print Version | Send by email | Permalink:

  • July's Community Calendar

The H Open

The H Security

The H Developer

The H Internet Toolkit