Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Local transformations are simple to debug: the IDE (at least IDEA) supports it with no extra effort. Global transformations are not so easy. To test this you might write a test harness that invoked LoggingASTTransformation on a file explicitly. The test harness source is available and could easily be modified to fit your needs. Let me know if you know an easier way to debug this!

Applying your transformation on custom file extensions

Since Groovy 1.7.5, you are allowed to add your own file types to the groovy compiler. This can be especially useful when you have written a DSL which requires extensive AST transformations and that you want to avoid your transformation to be applied on regular groovy files. For example, imagine your DSL specifies user stories :

Code Block

story "user enters invalid postcode" {

  given ....

  expect ...

}

Then you could allow this syntax in .story files only. To do this, you must first have registered your global AST transformation into the 'META-INF/services/org.codehaus.groovy.transform.ASTTransformation' file like explained before. The second step is to register your custom file extension into the 'META-INF/services/org.codehaus.groovy.source.Extensions' file :

Code Block

.story

The last step is to update your AST transformation so that it checks the file extension :

Code Block

void visit(ASTNode[] nodes, SourceUnit source) {
        if (!source.name.endsWith('.story')) {
            return
        }
        // start transforming the AST

    }

Now you have a global AST transformation that will only apply on .story files.