Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Using invokeMethod & getProperty

Since 1.0, Groovy supports the ability to intercept all method and property access via the invokeMethod and get/setProperty hooks. If you only want to intercept failed method/property access take a look at Using methodMissing and propertyMissing.

Overriding invokeMethod

In any Groovy class you can override invokeMethod which will essentially intercept all method calls (including calls to existing methods). 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):

Overriding getProperty and setProperty

You can also override property access using the getProperty and setProperty property access hooks. For example it is possible to write a trival "Expandable" object using this technique:

  • No labels