Printing out variables and values in a Groovy object

Solution 1:

dump()

For example

println "ddd".dump()

Prints:

java.lang.String@2ef900 value=dddd offset=0 count=4 hash=3078400

Solution 2:

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...