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

Overriding properties using metaClass won't work within class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 1.8.0
    • None
    • bytecode
    • None
    • Windows 7

    Description

      See the example

      class Book {
          String title
          def service
          
      String toString() {
       return "$service $title"
      }
      }
      
      Book.metaClass.getService = {-> return "hello"} 
      Book b = new Book()
      
      //This doesnt work either  - b. metaClass.getService = {-> return "hello"}  
      
      println b //prints null null
      assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 
      

      So apparently when property is referenced within class it doesn't use the getter provided by metaClass.

      This is really inconsistent behavior - how can the property be different when accessed from within and from using the reference of the class.

      I think the language should be consistent no matter what way the property is being accessed.

      Attachments

        Activity

          People

            Unassigned Unassigned
            snimavat Sudhir N
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: