Intercepting Method Calls Using AST Transformations

Imagine we’re in the middle of developing some banking software and the businesspeople throw a curveball at us. They want every $10K-plus deposit, withdrawal, or transfer involving a checking account to be audited. We want to respond to this request quickly, so let’s consider some options.

Our least favorable option would have us hunt for all places we perform a transaction on any checking account. Even with our favorite high-octane integrated development environment, finding and modifying the calls would not be fun. Furthermore, each time we call one of the methods, we would have to remember to perform the audit operation.

Another option is to modify the methods of the checking-account class ...

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.