tags:

views:

91

answers:

3

AST Transformations are implemented in Groovy. What's a practical example of using an AST Transformation?

A: 

Most of the practical examples of using the AST transformations are provided on that page. I've often used @Delegate to delegate to another class or @Lazy for lazy loading. @Grab is great for pulling in dependencies from a Maven/ivy repository. All of those are based on AST transformations and are part of the core language.

You can use transformations directly too but most of the stuff you would want them for is already built. You can do things that you might want to do with AOP in other languages.

Chris Dail
+2  A: 

This page has practical examples of how to use: @Singleton, @Lazy, @Immutable, @Delegate< @Newify, @Category, @Mixin, @PackageScope

Don
+1  A: 

Scenarios like:

  • Authorization Checking - Security by checking role from context
  • Print Parameter values with which the method is called
  • Asserts Parameters are not null or any validation
  • Check various entry-conditions/Pre-Conditions of the method
  • Generic AOP style BeforeMethod() implementation
  • Create a method and mark it to run as runnable or main method

Take a look at my blog post at AST AOP and AST Param not null

Hope this helps!

peacefulfire