groovy
  1. groovy
  2. GROOVY-5989 Inner Class Issues
  3. GROOVY-4473

Cannot access outer class getter from inner class GString with property syntax

    Details

    • Type: Sub-task Sub-task
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.7.5
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None
    • Number of attachments :
      0

      Description

      The following code...

      InnerClassGString.groovy:

      class Outer {  
        def foo() {
          def r = new Runnable() {
            public void run() {
              println "value of x is $x" // ${getX()} works, also $x with a field x
            }
          }       
          r.run()
        }  
        
        def getX() { 10 }     
      }         
      
      new Outer().foo()                    
      

      ...produces (when run via groovy command):

      Caught: groovy.lang.MissingFieldException: No such field: x for class: Outer
      	at Outer.this$dist$get$2(InnerClassGString.groovy)
      	at Outer$1.propertyMissing(InnerClassGString.groovy)
      	at Outer$1.run(InnerClassGString.groovy:5)
      	at Outer.foo(InnerClassGString.groovy:8)
      	at InnerClassGString.run(InnerClassGString.groovy:14)
      

        Activity

        Hide
        Roshan Dawrani added a comment -

        Doesn't have anything to do with GString, because the following version also fails with the same error

        class Outer {  
          def foo() {
            def r = new Runnable() {
              public void run() {
                def y = x
                println y
              }
            }       
            r.run()
          }  
          
          def getX() { 10 }     
        }         
        
        new Outer().foo()
        
        Show
        Roshan Dawrani added a comment - Doesn't have anything to do with GString, because the following version also fails with the same error class Outer { def foo() { def r = new Runnable () { public void run() { def y = x println y } } r.run() } def getX() { 10 } } new Outer().foo()
        Hide
        Matthias added a comment - - edited

        Can confirm this for Groovy 1.7.8. Here's another example where this breaks when inheritance is involved (without the inheritance, it works):

        class Outer {
          def myField = 42
         
          def run() {
            new Inner().run()
          }
         
          class Inner {
            def run() { println myField }
          }
        }
        
        new Outer().run() // OK
        
        class Inherited extends Outer {}
        
        new Inherited().run() // BOOM!
        
        groovy.lang.MissingFieldException: No such field: myField for class: Inherited
            at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2497)
            at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:3319) 
        ...
        
        Show
        Matthias added a comment - - edited Can confirm this for Groovy 1.7.8. Here's another example where this breaks when inheritance is involved (without the inheritance, it works): class Outer { def myField = 42 def run() { new Inner().run() } class Inner { def run() { println myField } } } new Outer().run() // OK class Inherited extends Outer {} new Inherited().run() // BOOM! groovy.lang.MissingFieldException: No such field: myField for class: Inherited at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2497) at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:3319) ...
        Hide
        Marcus Better added a comment -

        Still appears in 1.8.4. Another variation at http://groovyconsole.appspot.com/script/653003

        Show
        Marcus Better added a comment - Still appears in 1.8.4. Another variation at http://groovyconsole.appspot.com/script/653003

          People

          • Assignee:
            Unassigned
            Reporter:
            Peter Niederwieser
          • Votes:
            3 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: