How to iterate over groovy class non-static closures and optionally replace them?

How to iterate over groovy class non-static closures and optionally replace them?



MyClassName.metaClass

metaClassObject.properties

class Test {
  def name = 'tim'

  def processor = { str ->
    "Hello $name $str"
  }
}

Test t = new Test()

t.metaClass.properties.each {
  if( t[ it.name ].metaClass.respondsTo( it, 'doCall' ) ) {
    println "$it.name is a closure"
    def old = t[ it.name ]
    t.metaClass[ it.name ] = { str ->
      "WOO! ${old( str )}"
    }
  }
}

println t.processor( 'groovy!' ) // prints 'WOO! Hello tim groovy!'


Learn More :