Groovy web console

subscribe to the feed Subscribe
to this
site
embed in your blog Embed
this
script
Overload your setter 2.0 (via #groovywebconsole)
tweet this snippet Tweet
this
script

To embed this script in your site, just drop the content below where you want to embed it.

Overload your setter 2.0

Published 1 month ago by Seb Blanc with tags dynamic metaClass overload
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class Foo {
String name
}

class Bar {
Foo foo
}
//we overload the setter setFoo(Foo foo) with setFoo(String name)
Bar.metaClass.setFoo= {String value->
 Foo foo = new Foo()
 foo.name = "dynamic" + value
 delegate.setFoo(foo)

}
Bar bar = new Bar()
//instantiate a new foo with a string
bar.foo = "test"
bar.foo.name 

Recent scripts