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

Problem With MetaMethods and replacing MetaProperty

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.9, 2.4.0
    • Fix Version/s: 2.3.10, 2.4.1
    • Component/s: None
    • Labels:
      None

      Description

      The attached readonlyproperty.zip contains the following:

      src/main/groovy/demo/SomeTrait.groovy
      package demo
      
      trait SomeTrait {
          String title
      }
      
      src/test/groovy/demo/SomeTraitSpec.groovy
      package demo
      
      import spock.lang.Specification
      
      class SomeTraitSpec extends Specification {
      
          void 'test property access'() {
              given:
              Widget.metaClass.getTitle = {
                  'Some Default Title'
              }
              def w = new Widget()
      
              when:
              w.title = 'Some New Title'
      
              then:
              w.title == 'Some Default Title'
          }
      }
      
      class Widget implements SomeTrait {}
      

      That test will fail with the following:

      groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: title for class: demo.Widget
          at demo.SomeTraitSpec.test property access(SomeTraitSpec.groovy:15)
      

      If the trait is removed from the equation and the title property is declared directly in the Widget class then the test passes.

        Attachments

        1. readonlyproperty.zip
          50 kB
          Jeff Brown

          Issue Links

            Activity

              People

              • Assignee:
                melix C├ędric Champeau
                Reporter:
                brownj Jeff Brown
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: