16.3 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 Section 4.5, 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 ...
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.