Details

    • Type: Sub-task Sub-task
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.5.0-beta-1
    • Component/s: Compiler
    • Labels:
      None
    • Environment:
      Ubuntu 6.10 Edgy Eft + Groovy r4630
    • Number of attachments :
      0

      Description

      The following code appears to show that final is being applied inconsistently. A final list can be amended but an object that manipulates a lsit that is final cannot. In the former case the final is being applied to the reference and in the later, it is being applied to the object.

       class Blah {
        def list = []
         public plus ( item ) {
           list += [ item ]
           return this
         }
      }
      
      class Foobar {
         final static blah = new Blah ( )
      }
      
      final x = []
      x += [1]
      println ( x )
      
      Foobar.blah += 1
      println ( Foobar.blah.list ) 
      
      > groovy finalProblem.groovy
      [1]
      Caught: java.lang.IllegalAccessException: Field is final
      at finalProblem.run(finalProblem.groovy:17)
      at finalProblem.main(finalProblem.groovy)

        Issue Links

          Activity

          Hide
          Tord Alenljung added a comment -

          It seems like final on fields is not checked according to the following example:

          class C {
              final a = 0
              void changeA() {
                  a = a + 1
              }
              final b = 0
              final setB(val) {
                  b = val
              }
          }
          c = new C()
          c.changeA()
          c.changeA()
          assert c.a == 2
          c.b = 1
          assert c.b == 1
          
          Show
          Tord Alenljung added a comment - It seems like final on fields is not checked according to the following example: class C { final a = 0 void changeA() { a = a + 1 } final b = 0 final setB(val) { b = val } } c = new C() c.changeA() c.changeA() assert c.a == 2 c.b = 1 assert c.b == 1
          Hide
          Paul King added a comment -

          Just commenting on status of current implementation:
          Tord's example is checked - it is on fields and gives an appropriate error.
          Russel's example isn't checked yet - local variable case.

          Show
          Paul King added a comment - Just commenting on status of current implementation: Tord's example is checked - it is on fields and gives an appropriate error. Russel's example isn't checked yet - local variable case.
          Hide
          Paul King added a comment - - edited

          This is listed as a subtask of GROOVY-4681 but in reality, doesn't the description of GROOVY-4681 capture the remaining issue(s)? I.e. can we close one of them as a duplicate?

          Show
          Paul King added a comment - - edited This is listed as a subtask of GROOVY-4681 but in reality, doesn't the description of GROOVY-4681 capture the remaining issue(s)? I.e. can we close one of them as a duplicate?
          Hide
          Hendy Irawan added a comment -

          +1 for this

          Show
          Hendy Irawan added a comment - +1 for this
          Hide
          CÚdric Champeau added a comment -

          I have submitted a PR that demonstrates the first elements of final variable analysis (not limited to checking that the final modifier is respected):

          https://github.com/groovy/groovy-core/pull/565

          This visitor could be used by various transformations, in particular in type checking, but also in the compiler itself to make sure that a variable declared as final is not reassigned.

          Show
          CÚdric Champeau added a comment - I have submitted a PR that demonstrates the first elements of final variable analysis (not limited to checking that the final modifier is respected): https://github.com/groovy/groovy-core/pull/565 This visitor could be used by various transformations, in particular in type checking, but also in the compiler itself to make sure that a variable declared as final is not reassigned.

            People

            • Assignee:
              CÚdric Champeau
              Reporter:
              Russel Winder
            • Votes:
              4 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: