Groovy adds each () and a number of other methods to java.lang.Object. I can't figure out how to use the Groovy metaclass to dynamically replace the default values โโof each () in a Java class.
I see how to add new methods:
MyJavaClass.metaClass.myNewMethod = { closure -> } new MyJavaClass().myNewMethod { item -> println item }
But it seems the same approach does not work when overriding methods:
MyJavaClass.metaClass.each = { closure -> } new MyJavaClass().each { item -> println item }
What am I doing wrong? How can I dynamically override each () in Groovy?
rewbs source share