Monday, July 5, 2010

Groovy tip: using methodMissing


There are 4 metaprogramming hooks in Groovy. These are:
  • invokeMehod: interceps method dispatch
  • methodMissing: intercepts fail method dispatch
  • get/setProperty: intercepts property access
  • propertyMissing: intercepts failed property access
Let's see how to use methodMissing to add dynamic finders (accessing data store) to a class.


If we run this code it will fail at runtime since we haven't defined the finder. In order to do this we need to add some metaprogramming.

All we're doing here is selecting the appropriate column in the database depending on the kind of finder invoked. This is working but it's not as efficient as it should be. What we can do is create a closure from that code and the cache it in the class. So now we have:

So instead of running through the whole methodMissing implementation every time the same finder method is called now we create a method dynamically and we add it to the class. This means that next time we call the same method it will be found (cached).

This is a trick that can be used to improve performance when using the metaprogramming hooks in Groovy.

No comments:

Post a Comment