Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get properties from an object in Groovy, even when the object has an getProperties() method
def getPropertiesFrom(obj) {
obj.metaClass.properties.findAll { it.name != 'class' && it.name != 'metaClass' }.inject([:]) { acc, e -> acc[e.name] = e.getProperty(obj); acc }
}
class Foo {
int getBar() { 42; }
List getProperties() { [] }
}
def foo = new Foo();
foo.properties; //=> []
getPropertiesFrom(foo); //=> {bar=42, properties=[]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.