What’s next

July 19, 2008

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

Filed under: Java — stigl @ 3:58 pm
Tags: , , ,

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. Thanks for trying out and blogging about OVal.

    I have not used IntelliJ so I cannot comment on the AspectJ problems you had. Using OVal with AspectJ in Eclipse is quite smooth. Alternatively to compile-time weaving you could also use load-time weaving, this way you would not need any IDE plugins. I outlined this here http://sourceforge.net/forum/message.php?msg_id=4789479

    Post conditions can not only be written as scripts. All constraints you annotate to a method are evaluated when the method returns, so they are post-conditions. This includes the @ValidateWithMethod and @CheckWith annotations, which allow you to describe more complex validation logic in Java.


    Comment by Seb — July 21, 2008 @ 1:19 am | Reply

  2. Nice to get some feedback from an Oval committer 🙂
    Do you know if OVal has been used in a client-server setting, where calling methods on the server is avoided because the validation is done on the client, based on the validation rules that are exposed in the servers interface/public API?

    Comment by stigl — July 24, 2008 @ 8:03 am | Reply

  3. 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.


    Comment by Seb — August 2, 2008 @ 11:33 pm | Reply

  4. Architecture Bookmarks…

    Remmrit.com user has just tagged your post as architecture!…

    Trackback by Remmrit Bookmarking — August 12, 2008 @ 4:00 pm | Reply

  5. Perfect text!, brother

    Comment by Joustorry — October 6, 2008 @ 4:52 am | Reply

  6. 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).


    Comment by Maciek — January 17, 2009 @ 3:29 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: