Saturday, July 19, 2008

Pre and post condition validation with OVal as intruments of software architecture

I have for the past couple of weeks been fiddling around with OVal - the object validation framework for Java™.
Setting up a project to utilize OVal for the first time wasn't easy, as you can read in my post "Problems with IntelliJ AspectJ plugin".
I've created a simple Mavenized project, which should ease setting up, testing and getting an impression of the potential of OVal.

Pre Conditions

OVal has three interesting ways of expressing preconditions; Single annotations, script statements and evaluating methods.
I would recommend (with no prior experience) single annotations, because they are simpler and easily readable. Several annotations can be placed on parameters, and all are evaluated before a ConstraintsViolatedException is thrown.

public void specificClass(@NotNull @InstanceOfAny(value = List.class) Object o) {

Preconditions can be written as scripts, where Groovy is my personal favorite. The scripts can be expressive, at the cost of readability, complexity and compilation time checking.

@Pre(expr = "_this.amount!=null && amount2add>_this.amount", lang = "groovy")
public void increase(int amount2add) {

OVal has annotations for calling Evaluating methods, which can be complex methods that can do the evaluating. They work similarly to scripts, but are written as normal java methods, and hence get the benefits of compilation time checking.

Post Conditions

Post conditions can check the returning objects or the state of the object.
From what I've read, postconditions can only be written as scripts, although I can't see why they cannot be written as evaluating methods.

@Post(expr = "_this.amount >= 0", lang = "groovy")
public int withdraw(int amount2withdraw) {
amount -= amount2withdraw;
return amount;

What's different when using OVal?

Code syntax
One thing that is quite apparent is that OVal is a different way of expressing constraints and validation, moving it outside of the main codeblock. Whether writing moving code into annotations is a good thing is a subject for much dispute.

If annotation based validation is utilized in a client-server environment, where the methods that contain the validation requirements are located on the server, and the client has access to the requirements, I believe that one can run the validation without making an expensive call to the server side. And why not at the same time create a validating framework that uses the validation to generate code for javascript validation in the web browser?

Allthough it's not rocket science, much nullpointerchecks can be avoided if the input is validated with @NotNull checks.


OVal is simple and powerful, and may prove to be something to check out for your next project. Setting up aspectJ to weave in pre and post conditions may be cumbersome, but the project which I created should take care getting started, which could hinder adoptation.

The code can be downloaded here.


  1. We implemented an Eclipse RCP based client/server application that uses OVal. Basically we use Validator instances on both sides (client and server). On the client side we are using a custom data binder that links business objects to fields in JFace forms and provide instant visual feedback in case invalid values are entered. When these objects are transferred back to the server their validity is checked on the server side before any further processing.

    The implementation was quite straight forward.


  2. Architecture Bookmarks... user has just tagged your post as architecture!...

  3. Perfect text!, brother

  4. Is it possible to write java method for precondition? I really like compile time checking of my code. You have written: "OVal has annotations for calling Evaluating methods, which can be complex methods that can do the evaluating". How can I do it? In documentation (or user guide) there is only an example of field validated with java method (or class using @CheckWith annotation).



Spam will be pruned