O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Intercepting Methods Using MetaClass

We used GroovyInterceptable to intercept method calls in 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 Intercepting Methods Using GroovyInterceptable, this time using the MetaClass. In this version, the Car does not implement GroovyInterceptable and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required