Monday, April 16, 2012

A good Design-by-Contract library for Java?


A few years ago, I did a survey of DbC packages for Java, and I wasn't wholly satisfied with any of them. Unfortunately I didn't keep good notes on my findings, and I assume things have changed. Would anybody care to compare and contrast different DbC packages for Java?



Source: Tips4all

6 comments:

  1. There is a nice overview on
    WikiPedia about Design by Contract, at the end there is a section regarding languages with third party support libraries, which includes a nice serie of Java libraries. Most of these Java libraries are based on Java Assertions.

    In the case you only need Precondition Checking there is also a lightweight Validate Method Arguments solution, at SourceForge under Java Argument Validation (Plain Java implementation).

    Depending on your problem, maybe the OVal framework, for field/property Constraints validation is a good choice. This framework lets you place the Constraints in all kind of different forms (Annotations, POJO, XML). Create customer constraints through POJO or scripting languages (JavaScript, Groovy, BeanShell, OGNL, MVEL). And it also party implements Programming by Contract.

    ReplyDelete
  2. It's been a long time since I've looked at these, but found some old links. One was for JASS.

    The other one that I had used (and liked) was iContract by Reliable Systems. It had an ant task that you would run as a preprocessor. However, I can't seem to find it with some google searches, it looks like it has vanished. The original site is now a link farm. Check out this link for some possible ways to get to it.

    ReplyDelete
  3. I tested contract4J one time and found it usable but not perfect.
    You are creating contracts for for and after method calls and invars over the whole class.

    The contract is created as an assertion for the method. The Problem is that the contract itself is written in a string so you don't have IDE support for the contracts or compile time cheching if the contract still works.

    A link to the library

    ReplyDelete
  4. There is a Groovy extensions that enables Design by Contract(tm) in Groovy/Java code - GContracts. It uses so-called closure annotations to specify class invariants, pre- and postconditions. Examples can be found on the project's github wiki.

    Major advantage: it is only a single jar without external dependencies and it can be resolved via Maven compliant repositories since its been placed in the central Maven repo.

    ReplyDelete
  5. I think that many DbC libraries were surclassed by the builtin assert keyword, introduced since Java 1.4:


    it is a built-in, no other library is required
    it works with inheritance
    you can activate/deactivate on package basis
    easy to refactoring (e.g. no assertions in comments)

    ReplyDelete
  6. I'd highly recommend you to consider the Java Modeling Language (JML).

    ReplyDelete