Looking in the GString implementation I noticed that it call the InvokeHelper.write <https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java#L500> method.
I've tried implementing a DelegatingMetaClass <https://objectpartners.com/2013/07/30/customizing-mop-in-groovy/> but it does not work. At the end of the day it should to be possible to use meta-programming to override Java compiled code. Maybe the trick you are mentioning is ExpondoMetaClass.enableGlobally <http://docs.groovy-lang.org/latest/html/api/groovy/lang/ExpandoMetaClass.html#enableGlobally%28%29> ? Is there any other possible workaround ? Cheers, Paolo On Thu, May 26, 2016 at 11:02 PM, Emmanuel Rosa <[email protected]> wrote: > The rendering is actually produced by InvokerHelper: > https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java#L123 > > I tried overriding it with a category and also via the metaClass, but > neither worked. I recall some kind of trick that’s needed when using the > metaClass with some classes, to get it to take, but I don’t remember the > details. :( > > On May 26, 2016, at 2:06 PM, Paolo Di Tommaso <[email protected]> > wrote: > > Dear all, > > I have a use case in which I need to override the `toString` method of a > custom List class and use it when interpolating that list object in a > GString. > > It turns out the Groovy uses its own formatting methods for List and other > structures, so the my custom `toString` is simply ignored. > > To explain it better take in consideration this example: > > class MyList extends ArrayList { > > String toString() { > this.join('-') > } > > } > > > def x = new MyList() > x << 1 << 2 << 3 > > assert x.toString() == '1-2-3' > assert "$x" == '1-2-3' > > Assertion failed: > assert "$x" == '1-2-3' > | | > | false > [1, 2, 3] > > The second assertion fails for the reason said above. Now I'm wondering it > there's any way to override somehow the GString default rendering mechanism > in such a way I can inject my own formatting rule for my custom class. > > Any suggestion is appreciated. > > > > Cheers, > Paolo > > > > >
