I'm eager to use the @GrailsCompileStatic
annotation in many places of my grails 3.1.7 application. But the main problem is that it doesn't take into account that domains, commands and controllers are 'mixed' with additional methods (like save()
or validate()
) or variables (like g
).
I found a workaround for the mentioned methods: I inherit domains and commands from an abstract class that has an empty save()
method (it's implementation is overriden then by grails but static compilation doesn't fail). Also this abstract class implements grails.validation.Validateable
trait with validate()
method implementation.
But is there any better way to enable static compilation without these dirty tricks?
In many controllers I use the g.createLink()
method. How should I pass g
variable inside to avoid this during the static compilation?
Error:(37, 39) Groovyc: [Static type checking] - The variable [g] is undeclared.
Complementing what @andi answered, just to add a solution to the
g.createLink()
issue:You can inject a
LinkGenerator
bean and use static compilation as desired.Note: you'll have to inject the dependency like this:
And import the class from the package
grails.web.mapping