How can I print out the (public and internal) variables and their values of a Groovy object?
I.e
class X { def X = 10 def Y = 5 private void doPrivate() { def Z = 3 } }
Should give
X, 10 Y, 5 Z, 3
This has to be a dynamic solution, i.e at runtime.
You can print the current value of a variable with the println function.
The last line of a method in Groovy is automatically considered as the return statement. For this reason, an explicit return statement can be left out. To return a value that is not on the last line, the return statement has to be declared explicitly.
Note that echo does not work in Groovy itself - only in Jenkins. println works in both.
print() method is used. The System. out. println() method, in Java, prints the value passed as the parameter to it, on the console screen and the changes the cursor to the next line on the console.
dump()
For example
println "ddd".dump()
Prints:
java.lang.String@2ef900 value=dddd offset=0 count=4 hash=3078400
You mean like this?
def a = "Hi" a.properties.each { println "$it.key -> $it.value" }
Gives:
class -> class java.lang.String bytes -> [72, 105] empty -> false
[edit]
With your edited question, this would give you:
class -> class X y -> 5 metaClass -> org.codehaus.groovy.runtime.HandleMetaClass@16de4e1[groovy.lang.MetaClassImpl@16de4e1[class X]] x -> 10
I don't think it's possible to get the Z value at runtime... The only way I can think of to do it is via the AST...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With