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

getProperty isn't delegated more than 1 level deep

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.6.4
    • None
    • groovy-runtime
    • None
    • Mac OSX, 10.5.8

    Description

      I have a type A that defines the getProperty magic method. It is declared as a delegate in a class called B. The getProperty method is not redefined in B so whenever getProperty is called on an instance of B it delegates the request to A which then handles the request as expected. If I then have a class C that declares a delegate of type B and also does not redefine the getProperty method, I would expect that when you call getProperty on an instance of C that it would delegate to its instance of type B, which would in turn delegate the request to its instance of type A and the request would be handled.

      Unfortunately, this does not happen. Class C says it does not have a property with the name requested. If I then define getProperty in C but simply return b.getProperty(somePropertyName) in the method (explicitly defining the delegation) then groovy handles the delegation. Does this mean that delegation only works 1 level deep?

      See example below where C does delegation of getProperty explicitly (this works)

      class C {
          @Delegate B b = new B()
      
          public getProperty(String name) {
              return b.getProperty(name)
          }
      
          static void main(args) {
              def c = new C()
      
              System.out.println(c.hello)
          }
      }
      
      class B {
          @Delegate A a = new A()
      }
      
      class A {
          public getProperty(String name) {
              return name
          }
      }
      

      ... and implicitly (this doesn't work)

      class C {
          @Delegate B b = new B()
      
          static void main(args) {
              def c = new C()
      
              System.out.println(c.hello) //throws an unknown property exception
          }
      }
      
      class B {
          @Delegate A a = new A()
      }
      
      class A {
          public getProperty(String name) {
              return name
          }
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            iouwon luke sydenham
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: