Are JSR-299 CDI Annotations too complicated?

After my talk at GeeCON, I got a few comments from folks who have the feeling that all those CDI annotations are a little bit too much.

So, is CDI really an annotation mess?

After reflecting this question, it became clear to me that this may seem so when looking at CDI for the first time. But on the other hand, what do you get from all those annotations? Let’s grab a bit deeper.

Ed Burns recently pointed me to a famous quote from an old computer science book [1] on how to build large software systems:
1.) Build Abstractions
2.) Establish conventional Interfaces
3.) Establish new descriptive languages

In fact, 1.) and 2.) are already well covered in most Java applications – but can we interpret annotations as kind of a ‘meta-language’?
I’d say yes – by writing your own set of annotations, you effectively create kind of your own domain specific language. Now, what is a ‘programming language’? In a (not so strict) academical sense the answer would probably be “a way to tell your computer what and how it should execute”. And that is exactly what annotations can do for you!

You could easily create annotations (interceptors) like:

@skipIf(test="param1==NULL OR today=Calendar.SUNDAY")
public void myMethod(String param1) { ... }

public List getCustomers(Partner p) { ... }

public Basket purchaseProduct(Product p) { ... }

Focusing back on CDI, it now becomes pretty clear that this really allows kind of ‘establishing a new descriptive language’. This isn’t only AOP in the sense of _only_ applying technical concerns anymore, but we really use lots of the annotations coming with CDI to create kind of language like semantic-rich execution instructions. This of course also includes self written ones, based on the CDI meta annotations.

Now, looking at all the CDI annotations (and mechanisms to interpret them) from _this_ perspective, I’d say you could gain a hell lot benefit from learning these few CDI annotations – if used wisely of course.


[1] Structure and Interpretation of Computer Programs, Abelson and Sussman, MIT Press 1996. Ed also cites this in his ‘Secrets of Rockstar Programmers’ book.


About struberg
I'm an Apache Software Foundation member blogging about Java, µC, TheASF, OpenWebBeans, Maven, MyFaces, CODI, GIT, OpenJPA, TomEE, DeltaSpike, ...

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: