Groovy
  1. Groovy
  2. GROOVY-4692

Regression calling private fields from a Closure when subclassed

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 1.7.8, 2.4.0-beta-3
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None

      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

        Burt Beckwith created issue -
        Hide
        Jochen Theodorou 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
        Jochen Theodorou 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)
        Jochen Theodorou 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 ]
        Hide
        Jochen Theodorou added a comment -

        closed as duplicate of GROOVY-5438

        Show
        Jochen Theodorou added a comment - closed as duplicate of GROOVY-5438
        Jochen Theodorou made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Duplicate [ 3 ]
        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 [ 12733623 ] Default workflow, editable Closed status [ 12745421 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12971142 ] Default workflow, editable Closed status [ 12978907 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        1494d 14h 58m 1 Jochen Theodorou 28/Mar/15 05:09
        Resolved Resolved Closed Closed
        3d 59m 1 Paul King 31/Mar/15 07:08

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development