Groovy: how to print all object properties
This commit is contained in:
parent
d9f9d706a5
commit
3f9a80889d
@ -5,3 +5,19 @@
|
|||||||
`println new Person().metaClass.methods*.name.sort().unique()`
|
`println new Person().metaClass.methods*.name.sort().unique()`
|
||||||
|
|
||||||
Source http://groovy-almanac.org/list-the-methods-of-a-groovy-class/
|
Source http://groovy-almanac.org/list-the-methods-of-a-groovy-class/
|
||||||
|
|
||||||
|
# Print all object properties
|
||||||
|
|
||||||
|
## Method 1 ([src](https://gist.github.com/HopefulLlama/edecc82e9e6145544f34))
|
||||||
|
```
|
||||||
|
println object.properties.sort{it.key}
|
||||||
|
.collect{it}
|
||||||
|
.findAll{!['class', 'active'].contains(it.key)}
|
||||||
|
.join('\n')
|
||||||
|
```
|
||||||
|
|
||||||
|
## Method 2 ([src](https://stackoverflow.com/questions/3069687/printing-out-variables-and-values-in-a-groovy-object))
|
||||||
|
`object.properties.each { println "$it.key -> $it.value" }`
|
||||||
|
|
||||||
|
## Method 3 ([src](https://stackoverflow.com/questions/3069687/printing-out-variables-and-values-in-a-groovy-object))
|
||||||
|
`object.dump().println()`
|
||||||
|
Loading…
Reference in New Issue
Block a user