TX T
TX T

Reputation: 817

Groovy Meta Programming with getter method

Would someone help me figure out why this added "get" method works with one class(String) but not the other class(Node)?

String.metaClass.getFoo = { "string foo" }
s = "test"
println s.foo // WORKS: get "string foo"

Node.metaClass.getFoo = { "node foo" }
xml = "<test><body>test</body></test>"
nodes = new XmlParser().parseText(xml)
println nodes.foo // NOT WORK: gets []

How do I make calling the "foo" resulting the same as getFoo() for class Node?

Upvotes: 0

Views: 52

Answers (1)

dmahapatro
dmahapatro

Reputation: 50275

nodes.foo will try to find an element in the parsed tree of nodes. Directly using getFoo() would be the only option AFAIK.

Upvotes: 1

Related Questions