My Octopress Blog

A blogging framework for hackers.

How to Figure Out What the Grails Meta-methods Do

Since I code in Grails everyday, I periodically have friends and acquaintances that are learning the framework asking me questions. By far the most common one is something like: “where are all of these magic methods coming from!?!?!?!”

Grails heavily uses Groovy meta programming across every tier, but especially in controllers and domain objects. One example would be, another would be the render method on controllers. You can see a list of all these dynamic methods in the grails documentation: I can’t find a good way to link to just the dynamic methods, but on the right side of the documentation there is a quick reference section that contains a listing of all the available methods and brief descriptions if you click through.

While it’s definitely the best way to figure out what meta-methods are available to you, it doesn’t help you figure out exactly what those methods are doing. Furthermore, the documentation is generally pretty brief around how to use them. In my experience the best way to see all the configuration options and understand what’s happening is to look at the actual methods that are added to your controllers and domain objects. However, because they’re added to the ‘metaClass’ of these classes when Grails bootstraps it can be difficult to figure out where and how they’re added.

Fortunately, it’s not as bad as you think. Grails follows it’s own recommendations well and as a result all of these methods are added in the exact same fashion as they would be in a normal plugin. So, what you need to look for are the plugins that add them. I’ll start first with the Domain classes because they’re the most complicated and also the ones you will most likely need to understand. (More information on Grails plugins can be found here)

GORM Dynamic methods

The first thing to note when examining the GORM Methods is that it was designed to be plug and play from the get go. By default GORM is a hibernate implementation connecting to a standard RDBMS. However, that can be changed to use something like Mongo. To keep things simple I’ll assume the default.
In IntelliJ the hibernate plugin is included with all other project plugins
As you can see, just like any other plugin, the hibernate plugin adds the dynamic methods in doWithDynamicMethods. In this case it’s delegating to another class, likely to allow for easier testing. I’m not going to go through the entire class, but it’s fairly straightforward from here, as most of the dynamic methods are added directly by HibernatePluginSupport.

Controller Dynamic Methods

The controller methods are a little bit harder to find. Like the Hibernate plugin above, they are added via a plugin. However, you won’t find it with the normal plugin list, as it’s ‘core’. (Unless something massive has changed in 2.0) 

You’ll find when you look in this class that there’s a pretty natural separation of concerns, there’s a class for adding just the render methods, etc. In general, you can find most dynamic methods by searching for *GrailsPlugin.groovy.

Hopefully this helps you understand better how the Grails ‘magic’ works. Anyone with experience in Spring MVC or Hibernate will recognize most of what these methods are doing, but if not I suggest you do a quick look through those products to help you understand what’s happening.


This is true before 2.0, but in 2.0+ most of the domain class methods and controller methods are added directly to the bytecode via ASTs rather than dynamic metaprogramming. Real dynamic methods like dynamic finders (e.g. findByFooAndBar) have to be added at runtime, but methods with fixed signatures like save() are added by the compiler.

Also one small correction - GORM was not originally designed to be pluggable, it was retrofitted in 2.0 to be so; before that it was tightly coupled to Hibernate.