2 open source projects that could be worth looking at…
beanutils from Jakarta contains heaps of useful utility methods for working with beans, introspection etc. There’s also something support for DynaBean objects, which are like dynamic beans that can be defined at runtime. They make a neat wrapper around regular java beans too so you can access them in a Map-like manner.
Jexl is cool, its a simple expression language for evaluating expressions on beans. Jexl is a little like the Velocity expression language and is a super set of the JSP Expression Language. So you can use it to navigate bean property trees, accessing maps, arrays, collections, indexed properties and ‘map properties’ (a term coined by the beanutils project for getter methods that take a String argument) as well as methods. So the following are valid Jexl expressions…
[James Strachan’s Radio Weblog]
I looked at the beanutils stuff. I’m actually working from the other direction, grabbing all the properties from a bean and iterating through them looking for placeholders in the HTML that match each property name. That seemed easier than parsing the HTML and looking up properties on the bean. It should also make it easier to add user-defined markup – the code just does indexOf(openingMarkup + propertyName + closingMarkup) and replaces it with the property value. I wanted something that was ‘developer-driven’ – so the natural direction to go in seemed to be to emphasise the model acting on the view, rather than the other way around. Performance may be an issue, I have no feel at all for the performance of introspection and repeated calls to indexOf. I’m deferring worrying about that!