Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4121

Compiler fails to do assignment-to-final-fields checks on fields made final by @Immutable (was: @Immutable does not make fields immutable)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.7.1
    • 1.7.2, 1.8-beta-1
    • None
    • None
    • osx, groovyConsole

    Description

      @Immutable annotation does not prevent changing fields:

      @Immutable
      class Account {
          BigDecimal balance
          String customer
          
          Account deposit(amount) {
              balance = balance + amount
              this
          }
          
          String toString() {
              "Account[balance: $balance, customer: $customer]"   
          }
      }
      
      def acc = new Account(0.0, "Test")
      acc.deposit(3.1) //should raise exception, shouldn't it?
      assert 3.1 == acc.balance 
      

      Maybe I got the description in http://groovy.codehaus.org/api/groovy/lang/Immutable.html wrong:
      "Properties automatically have private, final backing fields with getters. Attempts to update the property will result in a ReadOnlyPropertyException." ?

      Attachments

        1. 4121_v18x_Patch.txt
          10 kB
          Roshan Dawrani

        Activity

          People

            roshandawrani Roshan Dawrani
            johanneslink Johannes Link
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: