12.2 Intercepting Methods Using MetaClass
We used GroovyInterceptable
to intercept method calls in Section 12.1, Intercepting Methods Using GroovyInterceptable.
That approach is good if we’re the author of the class whose methods we want to intercept.
However, that approach won’t work if we don’t have the privileges to modify the class source code
or if it is a Java class. Furthermore, we may decide at runtime to start intercepting calls based on some condition or application
state. In these cases, we can intercept methods by implementing the
invokeMethod
method on the
MetaClass.
Let’s rewrite the example from Section 12.1, Intercepting Methods Using GroovyInterceptable, this time using
the MetaClass. In this version, the Car ...
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