groovy
  1. groovy
  2. GROOVY-2774

Java final fields may be changed by Groovy

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.5.5
    • Fix Version/s: 1.6-beta-1, 1.5.7
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      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!

        Activity

        Hide
        David Smiley added a comment -

        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.

        Show
        David Smiley added a comment - 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.
        Hide
        blackdrag blackdrag added a comment -

        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

        Show
        blackdrag blackdrag added a comment - 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
        Hide
        Jim White added a comment -

        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.

        Show
        Jim White added a comment - 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.
        Hide
        blackdrag blackdrag added a comment -

        you should check that with a test. See my comment at GROOVY-1875

        Show
        blackdrag blackdrag added a comment - you should check that with a test. See my comment at GROOVY-1875
        Hide
        Jim White added a comment -

        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.

        Show
        Jim White added a comment - 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.
        Hide
        blackdrag blackdrag added a comment -

        fixed

        Show
        blackdrag blackdrag added a comment - fixed

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Victor Volle
          • Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: