Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created April 6, 2011 08:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rednaxelafx/905354 to your computer and use it in GitHub Desktop.
Save rednaxelafx/905354 to your computer and use it in GitHub Desktop.
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