Holy smokes Batman!
I've always know that Groovy's disregard for private scope is a VERY BAD
THING.
But I've never bothered to work up some cases to demonstrate that fact.
The error on line 3 is that you're getting the private field 'value' of
a java.lang.String object. That field happens to be an array of char
and when you assign a new value to it then the value of the string
changes. Of course that doesn't have any effect on the length attribute
of the string.
This violation of private permits untold havoc and means all classes,
not just java.lang.String, are deprived of assurance that their API
contracts are enforced. But such a succinct case is quite compelling.
def s = '12'
s.value = 'abcd'
println s
==>
ab
As for how to change the id attribute of a groovy.util.Node, I'm not
sure, although there are some docs on it.
Jim
Gael Marziou wrote:
> Hello,
>
> I'm very new to Groovy so I suppose that I misunderstood something.
>
> I wrote a little test to update an attribute of an XML node and it does
> not work: new value gets truncated to the size of the previous value.
>
> def input = '''<tag id="12" />'''
> def tag = new XmlParser().parseText(input)
> tag.attribute("id").value = "ABCD"
> // id contains "AB" it has been truncated to original size
> assert tag.attribute("id").size() == 4 : tag.attribute("id")
>
> I suppose that the error is on line 3 but I don't understand it.
>
> Thanks,
>
> Gael
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email