In the previous example, we used methods on the AST nodes themselves such as
addMethod to build up the new code in the AST. This can get laborious if we try and add any more sophisticated code. Even the simple
prettyPrint method would be quite difficult to implement with this mechanism. Fortunately, there are other options that will make our lives a bit easier.
Let's build another AST transformation, which uses a useful helper class
ASTBuilder to add the
prettyPrint method to our class. Once again we will need to define an interface for our annotation class and the AST transformation class itself:
@Target([ElementType.TYPE]) @Retention(RetentionPolicy.SOURCE) @GroovyASTTransformationClass(["PrettySimpleASTTransformation"]) ...