Since I just did this, I wanted to quickly share how to apply @AutoFinal to all project files (effectively making all method/closure parameters final) using a Groovy configuration file. Due to a current restriction in the AutoFinal implementation, we have to explictly exclude interfaces from having the annotation applied.
Here is the relevant code:

// groovycConfig.groovy import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer import org.codehaus.groovy.control.customizers.CompilationCustomizer import org.codehaus.groovy.control.customizers.SourceAwareCustomizer import org.codehaus.groovy.ast.ClassNode import groovy.transform.AutoFinal import org.codehaus.groovy.transform.trait.Traits //configuration.addCompilationCustomizers(new ASTTransformationCustomizer(AutoFinal)) // Does not worrk: Groovyc: Error processing interface 'at.gv.bmlv.groovyutil.compare.IdentityValuesToDebugStringTrait'. @AutoFinal only allowed for classes. final autoFinal =new SourceAwareCustomizer(new ASTTransformationCustomizer(AutoFinal))
autoFinal.setClassValidator{ ClassNode n-> !n.isInterface()} 
configuration.addCompilationCustomizers(autoFinal)
// groovycConfig.groovy - END


If you do not already have a Groovy configuration in your project: Just save as groovycConfig.groovy (e.g. in the project root), and e.g. in IntelliJ do File\Settings\Groovy Compiler\Path to configscript: <put path to groovycConfig.groovy file here>
and rebuild your project.

Cheers,
mg


Reply via email to