Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
c089 edited this page Apr 22, 2013 · 44 revisions

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


Flattr this
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.

package acme

import org.gcontracts.annotations.*

@Invariant({ speed() >= 0 })
class Rocket { 
    
    @Requires({ isStarted() })
    @Ensures({ old.speed < speed })
    def accelerate() { ... }

    boolean isStarted() { ... } 
    def speed() { ... }
}

More Features

GContracts supports the following feature set:

  • definition of class invariants, pre- and post-conditions via @Invariant, @Requires and @Ensures
  • inheritance of class invariants, pre- and post-conditions of concrete predecessor classes
  • inheritance of class invariants, pre- and post-conditions in implemented interfaces
  • usage of old and result variable in post-condition assertions
  • custom implementation of the Groovydoc Ant task to generate Javadocs with contract information
  • assertion injection in Plain Old Groovy Objects (POGOs)
  • human-readable assertion messages, based on Groovy power asserts
  • enabling contracts at package- or class-level with @AssertionsEnabled
  • enable or disable contract checking with Java’s -ea and -da VM parameters
  • annotation contracts: a way to reuse reappearing contract elements in a project
  • domain model
  • detection of circular assertion method calls
  • multi-module Gradle project

If you want to know about more about some of these features, checkout the Wiki pages.

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:

@Grab(group='org.gcontracts', module='gcontracts-core', version='[1.2.12,)')
import org.gcontracts.annotations.*

@Invariant({ elements != null })
class Stack<T> {

    List<T> elements

    @Ensures({ is_empty() })
    def Stack()  {
        elements = []
    }

    @Requires({ preElements?.size() > 0 })
    @Ensures({ !is_empty() })
    def Stack(List<T> preElements)  {
        elements = preElements
    }

    boolean is_empty()  {
        elements.isEmpty()
    }

    @Requires({ !is_empty() })
    T last_item()  {
        elements.get(count() - 1)
    }

    def count() {
        elements.size()
    }

    @Ensures({ result == true ? count() > 0 : count() >= 0  })
    boolean has(T item)  {
        elements.contains(item)
    }

    @Ensures({ last_item() == item })
    def push(T item)  {
       elements.add(item)
    }

    @Requires({ !is_empty() })
    @Ensures({ last_item() == item })
    def replace(T item)  {
        remove()
        elements.add(item)
    }

    @Requires({ !is_empty() })
    @Ensures({ result != null })
    T remove()  {
        elements.remove(count() - 1)
    }

    String toString() { elements.toString() }
}

def stack = new Stack<Integer>()

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.