Using Project Lombok with Naked Objects
Project Lombok is a recent project that exploits with Java 1.6′s APT (Annotation Processing Tool) API to generate boilerplate code from annotations. In this screencast I show how Lombok can be easily used to remove the boilerplate getters and setters from a pojo domain object.
http://content.screencast.com/users/danhaywood/folders/Jing/media/abd24c96-cd44-4771-aa3f-a95a6ff922ac/jingh264player.swf
At the time of writing Project Lombok is still very new. It doesn’t need any IDE, but if you are using an IDE then only Eclipse is supported (and it’s still relatively early days). But the project leads are also actively encouraging the community to experiment with new annotations and features (see for example the related Project Morbok). I can see several possibilities for Naked Objects though:
- currently we use bytecode enhancement (cglib or javassist) to override the getters for lazy loading, transparently calling the DomainObjectContainer#resolve(). We do something similar in the setters to mark the object as dirty, by transparently called DomainObjectContainer#objectChanged. An enhanced version of Lombok’s @Getters and @Setters, also applied to collections, might mean us being able to remove this bytecode enhancement
- an enhanced version could also generate the standard supporting methods, modifyXxx() and clearXxx() method for properties, and addToXxx() and removeFromXxx() for collections. These could then call hook methods (onModifyXxx() and so on) for any further business processing
- going further still, these standard supporting methods be generated the standard mutual registration pattern for bidirectional dependencies. This would need the annotation could specify whether the relationship is bidirectional, equivalent to JPA’s mappedBy or Hibernate’s “inverse” attributes. In fact, why not simply use JPA’s annotation…?
I suspect a few other of the conventions of the Naked Objects programming model could be ported over to Lombok… if you have any good ideas, post a comment.
Posted on September 13, 2009, in apache isis, random. Bookmark the permalink. 5 Comments.

IDEA integration is cool. Never thought about APT in this way.
I’d never seen the maven stuff actually work. Great, and thanks for crating this. One thing to mention is that people actually do need to execute the jar once so that Eclipse will be patched. Otherwise the code would run fine, the imports would be resolvable, but Eclipse would still not be able to see the generated methods.
@Roel,
I think I mention quickly that lombok.jar needs to be run to patch Eclipse, but thanks for emphasising this. For anyone else reading this that missed that point, check the video on the Lombok’s project website.
Keep up the good work, I’ll continue to track your progress (and might well end up contributing myself)
Dan
Pingback: Using Project Lombok with Naked Objects | Software Development Videos
Pingback: Using Project Lombok with Naked Objects | Java™ Software Development Videos and Tutorials Directory