O'Reilly logo

Groovy for Domain-specific Languages - Second Edition by Fergal Dearle

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using ASTBuilder

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.

Build from code

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"]) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required