Home

Welcome to GContracts – A Programming by Contract Extension for Groovy!

This project aims to provide contract annotations that support the specification of class-invariants, pre- and post-conditions on Groovy classes and interfaces. In addition, GContracts supports postconditions with the old and result variable and inheritance of all assertion types.

Applying @Invariant, @Requires and @Ensures

With GContracts in your class-path, contracts can be applied on a Groovy class or interface by using one of the assertions found in package org.gcontracts.annotations.

The Stack Example

Up to now the project provides 3 annotations: @Invariant, @Requires and @Ensures – all of them work as annotations with closures, where closures allow to specify arbitrary code pieces as annotation parameters:

The example above specifies a class-invariant and methods with pre- and post-conditions. Note, that preconditions may reference method arguments and post-conditions have access to the method’s result with the result variable and old instance variables values with old.

Indeed, Groovy AST transformations change these assertion annotations into Java assertion statements (can be turned on and off with a JVM param) and inject them at appropriate places, e.g. class-invariants are used to check an object’s state before and after each method call.

Prerequisites

GContracts 1.2.x runs with Groovy 1.7, 1.8 and 2.0.

How to add GContracts to a project

In order to use GContracts in a Maven project, following this wiki’s guide (Wiki page: Maven).

Otherwise, download the GContracts binaries from the download section and throw it in your class-path. groovyc automatically detects the jar and triggers GContracts AST transformations.

Notice

The project is currently in beta phase and not ment to be used in production environments, but feel free if you want to contribute and improve things.