The map constructor comes from Groovy - not Grails in this case. I did some experiments, and this is what I came up with:
class Foo { String name = "bob" int num = 0 public Foo() { this([:]) } public Foo(Map map) { map?.each { k, v -> this[k] = v } name = name.toUpperCase() } public String toString() { "$name=$num" } } assert 'BOB=0' == new Foo().toString() assert 'JOE=32' == new Foo(name:"joe", num: 32).toString()
Basically, you will have to manually override the constructors if you need to process the property after building.
Alternatively, you can redefine individual setters, which are generally cleaner and safer:
class Foo { String name = "bob" int num = 0 public void setName(n) { name = n.toUpperCase() } public String toString() { "$name=$num" } } assert 'bob=0' == new Foo().toString() assert 'JOE=32' == new Foo(name:"joe", num: 32).toString()
Please note that the default value is not processed, but in most cases this should be fine.
source share