18.7 Adding Dynamic Methods at Runtime

The Basics

Grails plugins let you register dynamic methods with any Grails-managed or other class at runtime. This work is done in a doWithDynamicMethods method.

Note that Grails 3.x features newer features such as traits that are usable from code compiled with CompileStatic. It is recommended that dynamic behavior is only added for cases that are not possible with traits.
  1. class ExamplePlugin extends Plugin {
  2. void doWithDynamicMethods() {
  3. for (controllerClass in grailsApplication.controllerClasses) {
  4. controllerClass.metaClass.myNewMethod = {-> println "hello world" }
  5. }
  6. }
  7. }

In this case we use the implicit application object to get a reference to all of the controller classes' MetaClass instances and add a new method called myNewMethod to each controller. If you know beforehand the class you wish the add a method to you can simply reference its metaClass property.

For example we can add a new method swapCase to java.lang.String:

  1. class ExamplePlugin extends Plugin {
  2. @Override
  3. void doWithDynamicMethods() {
  4. String.metaClass.swapCase = {->
  5. def sb = new StringBuilder()
  6. delegate.each {
  7. sb << (Character.isUpperCase(it as char) ?
  8. Character.toLowerCase(it as char) :
  9. Character.toUpperCase(it as char))
  10. }
  11. sb.toString()
  12. }
  13. assert "UpAndDown" == "uPaNDdOWN".swapCase()
  14. }
  15. }

Interacting with the ApplicationContext

The doWithDynamicMethods closure gets passed the Spring ApplicationContext instance. This is useful as it lets you interact with objects within it. For example if you were implementing a method to interact with Hibernate you could use the SessionFactory instance in combination with a HibernateTemplate:

  1. import org.springframework.orm.hibernate3.HibernateTemplate
  2. class ExampleHibernatePlugin extends Plugin{
  3. void doWithDynamicMethods() {
  4. for (domainClass in grailsApplication.domainClasses) {
  5. domainClass.metaClass.static.load = { Long id->
  6. def sf = applicationContext.sessionFactory
  7. def template = new HibernateTemplate(sf)
  8. template.load(delegate, id)
  9. }
  10. }
  11. }
  12. }

Also because of the autowiring and dependency injection capability of the Spring container you can implement more powerful dynamic constructors that use the application context to wire dependencies into your object at runtime:

  1. class MyConstructorPlugin {
  2. void doWithDynamicMethods()
  3. for (domainClass in grailsApplication.domainClasses) {
  4. domainClass.metaClass.constructor = {->
  5. return applicationContext.getBean(domainClass.name)
  6. }
  7. }
  8. }
  9. }

Here we actually replace the default constructor with one that looks up prototyped Spring beans instead!