Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Using methodMissing & propertyMissing

Since 1.15, Groovy supports the concept of "methodMissing". This differs from invokeMethod in that it is only invoked in the case of failed method dispatch.

...

Code Block
class GORM {

   def dynamicMethods = [...] // an array of dynamic methods that use regex
   def methodMissing(String name, args) {
       def method = dynamicMethods.find { it.match(name) }
       if(method) {
          GORM.metaClass."$name" = { Object[] varArgs ->
              method.invoke(delegate, name, varArgs)
           }
          return method.invoke(delegate,name, args)
       }
       else throw new MissingMethodException(name, delegate, args)
   }
}

...