Search code examples
grailsgroovymetaprogramminggrails-ormabstract-syntax-tree

Why GORM is using methodMissing


As I read here, grails is using methodMissing to inject GORM methods to the domain classes, but having said that methodMissing are expensive to execute cause only occur in the case of failed dispatch, why those methods are not injected via metaClass or AST Transformations? Any clues?


Solution

  • Using methodMissing is not that expensive, since Grails creates a new methode in the MOP only at the first execution of a methode not found. The sub-sequente executions takes place on the newly created methode.

    Consider a domain class with many properties. If all findBy*, findAlLBy*, countBy* etc permutations should be created at compile time, the classes could easily become VERY large. By using the methodeMissing, only methodes actually used at runtime will be created.