Created
April 6, 2011 08:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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