This section details how to go about using the dynamic features of Groovy such as implementing the GroovyObject interface and using ExpandoMetaClass, an expandable MetaClass that allows adding of methods, properties and constructors.
- Using invokeMethod and getProperty
- Using methodMissing and propertyMissing
- Evaluating the MetaClass runtime
- Using ExpandoMetaClass to add behaviour
- Customizing MetaClass for a single instance
- Runtime mixins
Compile-time metaprogramming is also available using Compile-time Metaprogramming - AST Transformations
Dynamic Method Invocation
You can invoke a method even if you don't know the method name until it is invoked:
You can also "spread" the arguments in a method call, when you have a list of arguments:
This also works in combination of the invocation with a GString:
Labels
Page: Evaluating the MetaClass runtime
Page: ExpandoMetaClass
Page: Global AST Transformations
Page: Local AST Transformations
Page: Per-Instance MetaClass
Page: Runtime mixins
Page: Using invokeMethod and getProperty
Page: Using methodMissing and propertyMissing