Setting Final Fields In Groovy Validators

There is another update in Groovy Validators. I think I figured out how to get them to work to validate/constrain final fields in mutable objects.

Here is an object:

import groovy.transform.ToString
import validation.IntAnnotation
import validation.FinalFieldValidator

@ToString( includeNames = true )
@FinalFieldValidator
class Car {

    @IntAnnotation( minValue = 10, throwEx = false )
    int miles
    @IntAnnotation( minValue = 1990 )
    final int year
}

You can validate the final field (and the other one as well) by instantiating the object with a map for the fields, and a boolean to trigger the FinalFieldValidator annotation:

def car = new Car( [ miles: 50, year: 2008 ], true )

I am still working on the Spock tests. I hope to have things wrapped up this weekend.

You’re welcome.

There are 113 words in this article.