12.1 Intercepting Methods Using GroovyInterceptable

If a Groovy object implements ​GroovyInterceptable​, then its ​invokeMethod​ is called when ​any​ of its methods are called—both existing methods and nonexistent methods. That is, ​GroovyInterceptable​’s ​invokeMethod​ hijacks all calls to the object.

If we want to perform an ​around advice​, we simply implement our logic in this method, and we’re done. However, if we want to implement the ​before​ or ​after advice​ (or both), we first implement our before/after logic, then route the call to the actual method at the appropriate time. To route the call, we’ll use the ​MetaMethod​ for the method we can obtain from the ​MetaClass​ (see Section 11.2, Querying Methods and Properties).

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.