Monday, June 16, 2008

Metawidget gets Groovy

Update: the APIs shown in this blog entry have changed slightly in newer releases of Metawidget. Specifically <propertystyle> now takes a PropertyStyle instance, not a class name. Please download the latest documentation from http://metawidget.org

The latest release of Metawidget adds supports for Groovy. This means Metawidget's Inspectors can now inspect GroovyBean properties for types and annotations.

GroovyBean properties are similar to JavaBean's getters/setters. Indeed, at compile time, GroovyBean properties try to 'look' like JavaBean properties by automatically generating a private member variable and a public getter/setter.

So, if your Groovy code says this...

class Booking {
   @NotNull
   String creditCard;
}

...at compile-time Groovy generates this:

class Booking {
   @NotNull
   private String mCreditCard;

   public String getCreditCard() {
      return mCreditCard;
   }

   public void setCreditCard(String creditCard) {
      mCreditCard = creditCard;
   }
}

Notice, however, that the annotation stays on the private member variable - it does not get applied to the getter. There are good reasons for this, but it means Metawidget's native JavaBeanPropertyStyle cannot be used to find those annotations.

So, instead, Metawidget now has a pluggable GroovyPropertyStyle that understands Groovy properties natively. To use it, you just need to update your inspector-config.xml...

<propertytypeinspector xmlns="java:org.metawidget.inspector.propertytype"
   config="org.metawidget.inspector.impl.BasePropertyInspectorConfig">
   <propertystyle>org.metawidget.inspector.impl.propertystyle.groovy.GroovyPropertyStyle</propertystyle>
</propertytypeinspector>

How does this work in practice? See for yourself! The Metawidget download includes a Seam Groovy Booking sample application, that showcases using Metawidget with Groovy. It's covered in detail in the reference guide.

2 comments:

Andres Almiray said...

And it gets even better when Groovy 1.6 is released, thanks to @Bindable

http://shemnon.com/speling/2008/04/bindable-observable-properties.html

Richard said...

Andres,

Wow - yes, I had not heard of @Bindable coming in Groovy 1.6, but that should be very useful indeed.

Hopefully it'll 'just work' using Metawidget's new GroovyPropertyStyle, but I look forward to testing it when it comes out!

Richard.