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: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.5
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None

      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

        Peter Niederwieser created issue -
        Peter Niederwieser made changes -
        Field Original Value New Value
        Description The following code...

        InnerClassGString.groovy:
        {code}
        class Outer {
          def foo() {
            def r = new Runnable() {
              public void run() {
                println "value of x is ${x}"
              }
            }
            r.run()
          }
          
          def getX() { 10 }
        }

        new Outer().foo()
        {code}

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

        {noformat}
        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)
        {noformat}
        The following code...

        InnerClassGString.groovy:
        {code}
        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()
        {code}

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

        {noformat}
        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)
        {noformat}
        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
        Pascal Schumacher made changes -
        Parent GROOVY-5989 [ 144082 ]
        Issue Type Bug [ 1 ] Sub-task [ 7 ]
        Jochen Theodorou made changes -
        Component/s groovy-runtime [ 16250 ]
        Hide
        Stefan Leonhartsberger added a comment -

        Still appears in 2.1.3.

        Show
        Stefan Leonhartsberger added a comment - Still appears in 2.1.3.
        Hide
        Guillaume Delcroix added a comment -

        Works fine in 2.3.2.

        Show
        Guillaume Delcroix added a comment - Works fine in 2.3.2.
        Hide
        Guillaume Delcroix added a comment -

        Works in recent versions of Groovy

        Show
        Guillaume Delcroix added a comment - Works in recent versions of Groovy
        Guillaume Delcroix made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Guillaume Laforge [ guillaume ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12733409 ] Default workflow, editable Closed status [ 12745219 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12970984 ] Default workflow, editable Closed status [ 12978758 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        1351d 11h 31m 1 Guillaume Delcroix 24/Jun/14 04:52
        Resolved Resolved Closed Closed
        14d 11h 36m 1 Paul King 08/Jul/14 16:28

          People

          • Assignee:
            Guillaume Delcroix
            Reporter:
            Peter Niederwieser
          • Votes:
            4 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development