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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With