I have a path for an object within an object within an object and I want to set it using Groovy\'s dynamic abilities. Usually you can do so just by doing the following:
Following works correctly.
foo."bar"."setMe" = 'This is the string I set into Bar';
Without getProperty overriding you can achieve the same result using "${}" syntax for GString as the below code demonstrates
class Baz {
String something
}
class Bar {
Baz baz
}
class Foo {
Bar bar
}
def foo = new Foo()
foo.bar = new Bar()
foo.bar.baz = new Baz()
def target = foo
def path = ["bar", "baz"]
for (value in path) {
target = target."${value}"
}
target."something" = "someValue"
println foo.bar.baz.something
final println prints "someValue" as expected