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).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access