Since 1.0, Groovy supports the ability to intercept all method and property access via the
get/setProperty hooks. If you only want to intercept failed method/property access take a look at Using methodMissing and propertyMissing.
In any Groovy class you can override
invokeMethod which will essentially intercept all method calls (to intercept calls to existing methods, the class additionally has to implement the
GroovyInterceptable interface). This makes it possible to construct some quite interesting DSLs and builders.
For example a trivial
XmlBuilder could be written as follows (note Groovy ships with much richer XML APIs and this just serves as an example):
Another simple usage of invokeMethod is to provide simple AOP style around advice to existing methods. Here is a simple logging example implemented with invokeMethod:
You can also override property access using the
setProperty property access hooks. For example it is possible to write a trival "Expandable" object using this technique: