O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

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

Injecting Methods Using AST Transformations

We saw how to inject code into existing methods. We can also inject new methods and fields into classes by using AST transformations. This gives us the full power of aspect-oriented programming (AOP) at compile time without the need for any third-party libraries.

In the Execute Around Method pattern from Using Closures for Resource Cleanup, a static method helped create and clean up instances. This method yielded the instance for arbitrary use in between these two operations. Let’s implement that pattern using AST transformations. Rather than asking the programmer to implement the use method by hand, we can create it; the programmer just has to ask—nicely.

The AST transformations we’ve seen so ...

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