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 2 Next »

ExpandoMetaClass - Adding Methods to a Single Instance

Normally when you add a MetaMethod, it is added for all instances of that class. However, for GroovyObjects, you can dynamically add methods to individual instances by giving that instance its own MetaClass:

Note that you cannot do this:

because you must call emc.initialize() before making any method calls on the instance. But you can't add MetaMethods after calling initialize()! This is bit of a catch 22 because the ExpandoMetaClass is intercepting methods to itself. The solution is (as shown in the first example) to simply add the MetaMethods before assigning the new MetaClass to your instance.

The other option is to set the set emc.allowChangesAfterInit = true.

  • No labels