Search code examples
grailsgroovyabstract-syntax-tree

Why is this Groovy AST transformation within a Grails app only being applied during class reloading?


In an attempt to develop a global AST transformation to add a new method to an Enum class within my app, I've set up this example project using Grails 5.

https://github.com/davebrown1975/grails_enum_experiment

The expected behaviour is that on compilation/building, the AST transform will be applied to the single enum class in the project ('tst.ExampleEnum'). I can see the 'visit' method being called in my Transformer ('tst.EnumTranslationTransformation'), however the sourceunit ast classes passed as a parameter to the visit method never include my Enum class.

Once the app is running however, if I make the simplest change to the Enum class, e.g. pressing space somewhere and saving it to trigger compilation and reloading, then this time I will see output in the console informing me the visit method was called AND the enum class was detected and AST has been applied.

Things I've tried, as per Grails docs, established the transforming class in it's own 'plugin', putting the class into a sub package of org.grails.compiler. Neither of these made a difference and I didn't see the AST being called at ever until I referenced the EnumTranslatorTransformation class from within a new file META-INF/services/org.codehaus.groovy.transform.ASTTransformation

Any thoughts as to what I'm missing here would be greatly appreciated.


Solution

  • The solution I found that ensures the AST is compiled and applied to the Enums at build time was to place the transformation into an inline plugin. What was critical at this point was to ensure that the META-INF/services/org.codehaus.groovy.transform.ASTTransformation file was moved to the plugin and not the main app. Only then would I see the transformation applied after a clean and build of the project.