Groovy - overriding invokeMethod for a single instance

后端 未结 1 1389
暖寄归人
暖寄归人 2020-12-19 14:06

I have an instance of a java object, let\'s say an instance of ArrayList called myList.

For this particular instance, I want to override the invokeMethod method to (

相关标签:
1条回答
  • 2020-12-19 14:44

    There might be a shorter route to the original method, but this should work:

    def myList = [ 1, 2, 3 ]
    
    myList.metaClass.invokeMethod { name, args -> 
       println "Called ${name} with ${args}"
       delegate.class.metaClass.getMetaMethod( name, args )?.invoke( delegate, args )
    }
    
    myList.sum()
    
    0 讨论(0)
提交回复
热议问题