Skip to content
This repository

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 projcet
  • Github Repository with Lighthouse Bug Tracking

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.1,)')
import org.gcontracts.annotations.*

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

    def elements

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

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

    boolean is_empty()  {
        elements.isEmpty()
    }

    @Requires({ !is_empty() })
    def last_item()  {
        elements.last()
    }

    def count() {
        elements.size()
    }

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

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

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

    @Requires({ !is_empty() })
    @Ensures({ result != null })
    def remove()  {
        elements.pop()
    }

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

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.0 runs with Groovy 1.7 and 1.8.

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.

Something went wrong with that request. Please try again.