Reputation: 3151
I am quite new to groovy, and I have found out that by making a field public, groovy provides getters and setters by default. Is there a way to have just the getters but not the setters by default? The reason behind this is that I have a Builder and I don't want to provide access to the object fields for modification.
Upvotes: 9
Views: 2786
Reputation: 37063
You can make the fields final
and add the Canonical
transform to get the c'tor created automatically for you. Or even easier use the Immutable
transform:
@groovy.transform.Immutable
class A {
String x
}
def a = new A("x")
assert a.x == "x"
// a.x = "will fail"
// a.setX("will fail")
In any case, you should take a look into the builder transforms
, what they have to offer for your use case.
Upvotes: 8