groovy
  1. groovy
  2. GROOVY-4692

Regression calling private fields from a Closure when subclassed

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.7.8, 2.4.0-beta-3
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Some code that was working in 1.7.5 (Grails pre-1.3.7) is failing now that we've upgraded to 1.7.8 for 1.3.7. Here's a simplified version:

      class Base {
      
         private String x = 'x'
      
         void theMethod() {
            println "outside closure, x == $x"
      
            callWithClosure { ->
               println "inside closure, x == $x"
            }
         }
      
         void callWithClosure(Closure c) { c() }
      }
      

      Running

      new Base().theMethod()
      

      prints the expected

      outside closure, x == x
      inside closure, x == x
      

      but if I call it on a subclass:

      class Super extends Base {}
      

      it fails:

      new Super().theMethod() 
      
      outside closure, x == x
      Exception thrown
      
      groovy.lang.MissingPropertyException: No such property: x for class: Super
         at Base$_theMethod_closure1.doCall(Base.groovy:9)
         at Base.callWithClosure(Base.groovy:13)
         at Base$callWithClosure.callCurrent(Unknown Source)
         at Base$callWithClosure.callCurrent(Unknown Source)
         at Base.theMethod(Base.groovy:8)
         at Base$theMethod.call(Unknown Source)
         at ConsoleScript1.run(ConsoleScript1:1)
      

        Activity

        Hide
        blackdrag blackdrag added a comment -

        I cannot confirm that there is any Groovy version in which this would have been working. The 1.7.5 for example fails. It seems your simplified version is too simplified to reproduce the issue

        Show
        blackdrag blackdrag added a comment - I cannot confirm that there is any Groovy version in which this would have been working. The 1.7.5 for example fails. It seems your simplified version is too simplified to reproduce the issue
        Hide
        Aled Sage added a comment -

        +1.

        I came across this in Groovy 1.8. It means I can't make my fields private, which is very disconcerting for someone who has spent a long time using Java and who likes encapsulation

        Whether or not it's a regression, it would be great if private fields could always be accessed from methods within that same class.

        Show
        Aled Sage added a comment - +1. I came across this in Groovy 1.8. It means I can't make my fields private, which is very disconcerting for someone who has spent a long time using Java and who likes encapsulation Whether or not it's a regression, it would be great if private fields could always be accessed from methods within that same class.
        Hide
        Andrew Smith added a comment -

        +1

        Ran into this same issue today with 1.7.10 (and also verified with 1.8.6).

        It's as simple as:

        class A
        {
            private Integer count = 5
        
            void call()
            {
                3.times { println("Count is $count") }
            }
        }
        
        class B extends A {}
        
        new B().call()
        

        It gives the same:

        Caught: groovy.lang.MissingPropertyException: No such property: count for class: B
        groovy.lang.MissingPropertyException: No such property: count for class: B
        	at A$_call_closure1.doCall(Scoping.groovy:15)
        	at A.call(Scoping.groovy:15)
        	at A$call.call(Unknown Source)
        	at Scoping.run(Scoping.groovy:23)
        
        Show
        Andrew Smith added a comment - +1 Ran into this same issue today with 1.7.10 (and also verified with 1.8.6). It's as simple as: class A { private Integer count = 5 void call() { 3.times { println("Count is $count") } } } class B extends A {} new B().call() It gives the same: Caught: groovy.lang.MissingPropertyException: No such property: count for class: B groovy.lang.MissingPropertyException: No such property: count for class: B at A$_call_closure1.doCall(Scoping.groovy:15) at A.call(Scoping.groovy:15) at A$call.call(Unknown Source) at Scoping.run(Scoping.groovy:23)
        blackdrag blackdrag made changes -
        Field Original Value New Value
        Component/s groovy-runtime [ 16250 ]
        Pascal Schumacher made changes -
        Affects Version/s 2.4.0-beta-3 [ 20544 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Burt Beckwith
          • Votes:
            3 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: