Annotation to automatically add final to various syntactic structures, saving you typing of some boilerplate code. Initially, only method and constructor parameters are supported. The annotation may be placed on any method or constructor. It can also be placed at the class level in which case it applies to all methods and constructors within the class.
Example usage:
@groovy.transform.AutoFinal
class Person {
final String first, last
Person(String first, String last) {
this.first = first
this.last = last
}
String fullName(boolean reversed = false, String separator = ' ') {
"${reversed ? last : first}$separator${reversed ? first : last}"
}
}
def js = new Person('John', 'Smith')
assert js.fullName() == 'John Smith'
assert js.fullName(true, ', ') == 'Smith, John'
for this case, the constructor for the Person
class will be
equivalent to the following code:
Person(final String first, final String last) { //... }And after normal default parameter processing takes place, the following overloaded methods will exist:
String fullName(final boolean reversed, final String separator) { ... } String fullName(final boolean reversed) { fullName(reversed, ' ') } String fullName() { fullName(false) }