You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Paolo Di Tommaso <pa...@gmail.com> on 2016/05/26 18:06:30 UTC
List and GString
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
Re: List and GString
Posted by Paolo Di Tommaso <pa...@gmail.com>.
Yes, but `toString` is called implicitly so there's no way to intercept it
(or at least I was unable to do that).
p
On Fri, May 27, 2016 at 12:22 AM, emmanuel r <go...@gmail.com> wrote:
> Doesn't GString resolve expressions when it's toString is called? If so,
> you may be able to override it at that point.
> On May 26, 2016 6:11 PM, "emmanuel r" <go...@gmail.com> wrote:
>
>> Yep, that's the trick I was thinking of.
>> On May 26, 2016 6:01 PM, "Paolo Di Tommaso" <pa...@gmail.com>
>> wrote:
>>
>>>
>>>> At the end of the day it should to be possible to use meta-programming
>>>> to override Java compiled code.
>>>>
>>>
>>>
>>> Oops, I meant: *it should *not* be possible to use meta-programming*
>>>
>>>
>>>
Re: List and GString
Posted by emmanuel r <go...@gmail.com>.
Doesn't GString resolve expressions when it's toString is called? If so,
you may be able to override it at that point.
On May 26, 2016 6:11 PM, "emmanuel r" <go...@gmail.com> wrote:
> Yep, that's the trick I was thinking of.
> On May 26, 2016 6:01 PM, "Paolo Di Tommaso" <pa...@gmail.com>
> wrote:
>
>>
>>> At the end of the day it should to be possible to use meta-programming
>>> to override Java compiled code.
>>>
>>
>>
>> Oops, I meant: *it should *not* be possible to use meta-programming*
>>
>>
>>
Re: List and GString
Posted by emmanuel r <go...@gmail.com>.
Yep, that's the trick I was thinking of.
On May 26, 2016 6:01 PM, "Paolo Di Tommaso" <pa...@gmail.com>
wrote:
>
>> At the end of the day it should to be possible to use meta-programming to
>> override Java compiled code.
>>
>
>
> Oops, I meant: *it should *not* be possible to use meta-programming*
>
>
>
Re: List and GString
Posted by Paolo Di Tommaso <pa...@gmail.com>.
>
>
> At the end of the day it should to be possible to use meta-programming to
> override Java compiled code.
>
Oops, I meant: *it should *not* be possible to use meta-programming*
Re: List and GString
Posted by Paolo Di Tommaso <pa...@gmail.com>.
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 <go...@gmail.com>
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 <pa...@gmail.com>
> 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
>
>
>
>
>
Re: List and GString
Posted by Emmanuel Rosa <go...@gmail.com>.
The rendering is actually produced by InvokerHelper: https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/runtime/InvokerHelper.java#L123 <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 <pa...@gmail.com> 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
>
>
>