2008-11-07

More fun with Groovy and Reflection API

This time in a TagLib I need to see all the properties of a domain class but I don't want to look at anything that isn't sent to hibernate.

import java.lang.reflect.Modifier
static getFields(obj) {
def names = []
def fields = obj.getClass().declaredFields
fields.each({ field ->
if(!field.synthetic)
if(!Modifier.isStatic(field.modifiers))
if(!Modifier.isTransient(field.modifiers)) {
names.add(field.name.toString())
}
})
return names
}

... in another method I'll filter out the Closures by checking against Closure.class and the property's class.