You're changing the metaClass of A
with
A.metaClass.invokeMethod { name,args ->
println "Came here"
}
After you construct the variable
. If you put this block before the line
A varaiable = new A()
It should work as you'd expect.
To get round this, you can use:
ExpandoMetaClass.enableGlobally()
And instances will check back with the metaClass
every invocation, however as expected this can slow things down