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

Compilation error when accessing a protected super class field from inside a closure

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.7, 2.5.8, 3.0.0-rc-1
    • Fix Version/s: 3.0.0-rc-2, 2.5.13
    • Component/s: Static compilation
    • Labels:
      None

      Description

      The following code works fine in 2.5.6 but breaks in 2.5.7 and 2.5.8:

      GroovyShell shell = new GroovyShell()
      shell.evaluate('''
          package a
          
          import groovy.transform.CompileStatic
          
          @CompileStatic
          abstract class Abstract_Class {
              protected String protectedField = 'field'
              
              abstract String doThing()
          }
          assert true''')
      
      shell.evaluate('''
          package b
          
          import a.Abstract_Class
          import groovy.transform.CompileStatic
          
          @CompileStatic
          class ConcreteClass extends Abstract_Class {
             
              @Override
              String doThing() {
                  'something'.with {
                      return protectedField
                  }
              }
          }
          assert true''')
      
      shell.evaluate("assert new b.ConcreteClass().doThing() == 'field'")
      
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Script2.groovy: 13: Access to a.Abstract_Class#protectedField is forbidden @ line 13, column 32.
                                 return protectedField
                                        ^
      

      If you change both classes to be in the same package, you get a runtime error like this:

      tried to access field a.Abstract_Class.protectedField from class a.ConcreteClass$_doThing_closure1
      java.lang.IllegalAccessError: tried to access field a.Abstract_Class.protectedField from class a.ConcreteClass$_doThing_closure1
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                emilles Eric Milles
                Reporter:
                jbellassai John Bellassai
              • Votes:
                5 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 3h 20m
                  3h 20m