Metaprogramming

A complete discussion of Groovy’s metaprogramming features would be beyond the scope of this primer, but it will be helpful to have some understanding of them as you begin to work with Grails. Groovy’s metaprogramming can make us much more productive as developers. With it, Grails adds methods to our objects that we are going to need and use but never write.

So here’s Groovy metaprogramming in a nutshell: Groovy provides mechanisms for adding methods and properties to classes at runtime, and/or without touching their code. This is done in several ways; we’ll discuss two that are used in Grails.

Every Groovy class has a metaClass, which can be written by someone else and assigned at runtime. Any methods or properties added in ...

Get Grails 2: A Quick-Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.