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

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.7, 2.5.8, 3.0.0-rc-1
    • 3.0.0-rc-2, 2.5.13
    • Static compilation
    • 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

              emilles Eric Milles
              jbellassai John Bellassai
              Votes:
              5 Vote for this issue
              Watchers:
              4 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