Thursday, February 2, 2012

Bean property expressions with Java and Tapestry

Tapestry has a couple of nice features for accessing bean properties in a loosely coupled way. One of them is a reflection-based PropertyAccess and the other one a runtime-generated proxy called PropertyConduit. Both of these are very comfortable to use - just specify a property expression and you're good to go. Unfortunately, in larger projects, these String-based property expressions tend to become a bit difficult to maintain as you're re-factoring the beans the expressions refer to.

So, just for fun, I decided to experiment with an alternative for PropertyConduit - to create the conduits during compilation and have an ability to chain these together into more complex expressions. For instance, if we would have two bean classes:

Then we could refer to and get the "" property as:

This would ideally be type-safe and the *Properties classes auto-generated.

The initial proof-of-concept that I came up with contains two classes describing the property expressions:

For testing purposes, three classes were created:
To automatically generate property expressions for a set of bean classes, an annotation processor was implemented, which yields the following result (for the bean classes specified above):

And a simple test scenario to check that everything works as expected:

Will need to test this out on a larger scale now, for possible performance and memory usage problems :)