Versions Compared

Key

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

...

The AST transformation customizer is meant to apply AST transformations transparently. Unlike global AST transformations that apply on every class beeing compiled as long as the transform is found on classpath (which has drawbacks like increasing the compilation time or side effects due to transformations applied where they should not), the customizer will allow you to selectively apply a transform only for specific scripts or classes.

As an example, let's say you want to be able to use @Log in a script. The problem is that @Log is normally applied on a class node and a script, by definition, doesn't require one. But implementation wise, scripts are classes, it's just that you cannot annotate this implicit class node with @Log. Using the AST customizer, you have a workaround to do it:

...

As the AST transformation customizers works with objects instead of AST nodes, not all values can be converted to AST transformation parameters. For example, primitive types are converted to ConstantExpression (that is 'LOGGER' is converted to new ConstantExpression('LOGGER'), but if you your AST transformation takes a closure as an argument, then you have to give it a ClosureExpression, like in the following example:

...