History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-2774
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Jochen Theodorou
Reporter: Victor Volle
Votes: 2
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
groovy

Java final fields may be changed by Groovy

Created: 24/Apr/08 05:53 AM   Updated: 24/Apr/08 01:57 PM
Component/s: None
Affects Version/s: 1.5.5
Fix Version/s: 1.6-beta-1, 1.5.7

Time Tracking:
Not Specified


 Description  « Hide
From GROOVY-1875:

def s = '12'
s.value = 'ABCD'
println s
==>
AB

The 'value' field is a private final variable in class java.lang.String.
Therfore Groovy makes String mutable!



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
David Smiley - 24/Apr/08 07:10 AM
It's crazy world that it's come to this point. I wonder if the fact that this would be possible occurred to any of the groovy developers as groovy was being written, or if this is an innocent mistake. If it occurred to someone pre-release... I'd love to see the discussion on the matter on why on earth this should be permitted.

Jochen Theodorou - 24/Apr/08 07:23 AM
a I commented elsewhere.... the field is final, thus the change should not happen. If it where a property it would not happen. It only happens because it is a field and there is a check missing. that's nothing crazy

Jim White - 24/Apr/08 09:08 AM
That test case does not demonstrate modification of a final field and that is not what is happening. It reads a final field which returns a [C (array of char) then updates the element values in the array. That is completely valid from a 'final' aspect (that the field is private and should never have been readable in the first place is the bug as I reported using this test case).

Groovy does not modify final fields, but if you think it can, then submit a suitable test.


Jochen Theodorou - 24/Apr/08 09:42 AM
you should check that with a test. See my comment at GROOVY-1875

Jim White - 24/Apr/08 11:21 AM
As you said, the test belongs here.

The test I tried earlier was this:

class MyBean {
   final String foo = '123'
}
def b = new MyBean()
b.foo = 'ABC'
==>
groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: foo for class: MyBean

My new version that should fail, but does not:

String s = "123"
def x = s.value
s.value = "ABC"
assert !x.is(s.value)

The reason I put it in "should fail" form is because it should really fail before the assertion.


Jochen Theodorou - 24/Apr/08 01:57 PM
fixed