Groovy: how to print all object properties

This commit is contained in:
Morgan 2019-09-05 14:24:16 +02:00 committed by GitHub
parent d9f9d706a5
commit 3f9a80889d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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()`