I want to get a map of all the fields in a class (aka value object) in a generic way. The following works fine for me:
class Baz {
String foo = "foo2"
int bar = 2
public Map asMap() {
def map = [:] as HashMap
this.class.getDeclaredFields().each {
if (it.modifier == java.lang.reflect.Modifier.PRIVATE) {
map.put(it.name, this[it.name])
}
}
return map
}
}
But this doesn't feel like the proper way. Is there a better approach?