From 3f9a80889d73e66750d0425f424ada5f5842d979 Mon Sep 17 00:00:00 2001 From: Morgan Date: Thu, 5 Sep 2019 14:24:16 +0200 Subject: [PATCH] Groovy: how to print all object properties --- cheat/groovy.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cheat/groovy.md b/cheat/groovy.md index c9c9d5a..7c5014c 100644 --- a/cheat/groovy.md +++ b/cheat/groovy.md @@ -5,3 +5,19 @@ `println new Person().metaClass.methods*.name.sort().unique()` 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()`