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

SC: subclass access to package-private or private field is not indicated as an error

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.16, 3.0.0-alpha-4, 2.5.6
    • 4.0.0-alpha-2
    • None
    • None

    Description

      Consider the following:

      package p
      import groovy.transform.*
      @CompileStatic class Main {
        @PackageScope static final String CONST = 'value'
        static main(args) {
          new q.Sub().meth()
        }
      }
      
      package q
      import groovy.transform.*
      @CompileStatic class Sub extends p.Main {
        void meth() {
          print CONST
        }
      }
      

      This code compiles but throws an IllegalAccessError at run-time. Since a subclass should not have access to it's parent's private or package-private members (if located in a different package), there should be a compile-time error like "Access to Main#CONST is forbidden @ line -1, column -1."

      Replacing "@PackageScope" with "private" should get the same compile-time error but produces "MissingPropertyExceptionNoStack: No such property: CONST for class: q.Sub" at run-time.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              emilles Eric Milles
              Votes:
              0 Vote for this issue
              Watchers:
              1 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 - 0.5h
                  0.5h