views:

669

answers:

6

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?

A: 

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)
dfa
but SUN writes "Do not use assertions to check the parameters of a public method" in http://java.sun.com/javase/6/docs/technotes/guides/language/assert.html
Carlos Heuberger
More to the point, DbC should make it easy to add contracts to your code. If it's not easy, most developers won't do it. It's possible to implement class invariants using assertions and calls to a class invariant method, but it's clumsy.
Chris Jones
+1  A: 

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

Janusz
+2  A: 

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.

mattwright
The most recent version of JASS is from July 2005. I'm not going to speculate about what versions of the JDK it supports. JASS has a link to JML, though, which looks to be under active development.
Chris Jones
+2  A: 

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.

Verhagen
OVal looks impressive. I'd venture to say it's about as good as DbC in Java can get.
Chris Jones
A: 

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

reprogrammer
+1  A: 

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.

Andre Steingress