Search code examples
javaunit-testingannotationsinterfaceinvariants

Adding Invariants to Interfaces in Java


I've been thinking about creating a Java framework that would allow programmers to specify invariants (pre- and post-conditions) on interfaces. The purpose would be to make code more robust and reduce the number of unit tests that would need to be written for different implementations of the same interface.

I envisage creating some way of annotating methods with invariants that the programmer would also write. E.G.

interface Sort {
    int [] sort(int [] nums);
}

would be decorated with an annotation to ensure that any implementations return a sorted list. This annotation would be linked to a unit test that could be run at compile time against any implementation.

Is this a crazy idea or would this be useful to the wider programming community?


Solution

  • This sounds like it could be related to JML and ESC/Java, both of which have found reasonably wide-spread adoption within the kinds of projects that need a little more software quality than is offered by the usual set of techniques.