Skip to main content

Design by Contract, using annotations?

4 replies [Last post]
cowwoc
Offline
Joined: 2003-08-24
Points: 0

How hard would it be to implement "design by contract" using annotations? Wouldn't it be extremely easy (and useful) to do so?

I am proposing:

@Preconditions
@Postconditions
\-> Only applicable to methods, get converted into assert() statements at the beginning and end of the method, respectively.

@Invarients
\-> Only applicable to classes, get inserted into the beginning *and* end of methods in the form of assert() statements.

Theoretically speaking, this wouldn't even have to come from Sun, but rather can come from a 3rd-party library (Sun should integrate it in some future release)

What do you think?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cowwoc
Offline
Joined: 2003-08-24
Points: 0

Nevermind, just found http://en.wikipedia.org/wiki/Design_by_contract#Java

One question though, I'd want to get a compile-time error if my pre/post/invarient conditions contained an invalid expression (typos, etc). Do annotations provide this capability?

prunge
Offline
Joined: 2004-05-06
Points: 0

I'm pretty sure you can use APT to read annotations and validate their contents at compile time. Never tried this though.

I remember writing a @NotNull annotation for method parameters, adding the null check through an AspectJ aspect at the beginning of the method for every parameter that was marked with it. Not sure if aspects can read extra info from these annotations such as values to modify behaviour or whether they serve as markers only.

cowwoc
Offline
Joined: 2003-08-24
Points: 0

I honestly don't understand this anymore. Does the new annotation processor API in Mustang still offer a read-only AST? As far as I can see, there is no way to write a compile-time annotation processor which injects code into the beginning of a method depending upon the annotation you mark it with.

Are you saying that you can't do this sort of thing without AspectJ? Ideally I want this processing to take place as part of javac, like Mustang offers. Any ideas?

prunge
Offline
Joined: 2004-05-06
Points: 0

> I honestly don't understand this anymore. Does the
> new annotation processor API in Mustang still offer a
> read-only AST? As far as I can see, there is no way
> to write a compile-time annotation processor which
> injects code into the beginning of a method depending
> upon the annotation you mark it with.

The APT API seems to be improved, but as far as I can see the AST is still read only and therefore this is not possible. I've been wanting to do this ever since I first heard of APT with JDK 1.5.

> Are you saying that you can't do this sort of thing
> without AspectJ? Ideally I want this processing to
> take place as part of javac, like Mustang offers. Any
> ideas?

AspectJ was one option, writing some kind of runtime java.lang.instrument agent was another. Bytecode manipulation proved to be way more difficult than it was worth. JavaAssist (http://www.csg.is.titech.ac.jp/~chiba/javassist/) seemed the friendliest and easiest to use, although I only looked at it for a couple of days.