[ https://issues.apache.org/jira/browse/GROOVY-9043?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16827223#comment-16827223 ]
Eric Milles edited comment on GROOVY-9043 at 5/2/19 4:00 PM: ------------------------------------------------------------- I think the same package check needs a fix. Currently it checks for same package of receiver (object expression) and _enclosing_ class. But I think it should be _declaring_ class of the field and enclosing class of the field access. That should mean no need to pass {{samePackages(...)}} result from every call site of {{makeGetField}} through to {{isDirectAccessAllowed}}. And {{isDirectAccessAllowed}} would replace "|| isSamePackage" with "|| samePackages(receiver.getPackageName(), declaringClass.getPackageName())". Test case: {code:groovy} package p class One { @groovy.transform.PackageScope static final String VALUE = 'value' } package q @groovy.transform.CompileStatic class Two extends p.One { static void test() { VALUE // should be "Access to VALUE is forbidden ..." but samePackage is true for implicit-this q.Two and enclosing class q.Two } } {code} as well as {code:groovy} package p class One { @groovy.transform.PackageScope static final String VALUE = 'value' } package q class Two extends p.One {} package p @groovy.transform.CompileStatic class Three { static void test() { q.Two.VALUE // should be okay; Three is in same package as the declaration so it gets privileged access } } {code} The parameter names of {{isDirectAccessAllowed}}. Field is named "a" instead of "field" -and enclosing type is named "receiver" instead of "currentClass" or "enclosingClass"-. was (Author: emilles): I think the same package check needs a fix. Currently it checks for same package of receiver (object expression) and _enclosing_ class. But I think it should be _declaring_ class of the field and enclosing class of the field access. That should mean no need to pass {{samePackages(...)}} result from every call site of {{makeGetField}} through to {{isDirectAccessAllowed}}. And {{isDirectAccessAllowed}} would replace "|| isSamePackage" with "|| samePackages(receiver.getPackageName(), declaringClass.getPackageName())". Test case: {code:groovy} package p class One { @groovy.transform.PackageScope static final String VALUE = 'value' } package q @groovy.transform.CompileStatic class Two extends p.One { static void test() { VALUE // should be "Access to VALUE is forbidden ..." but samePackage is true for implicit-this q.Two and enclosing class q.Two } } {code} as well as {code:groovy} package p class One { @groovy.transform.PackageScope static final String VALUE = 'value' } package q class Two extends p.One {} package p @groovy.transform.CompileStatic class Three { static void test() { q.Two.VALUE // should be okay; Three is in same package as the declaration so it gets privileged access } } {code} The parameter names of {{isDirectAccessAllowed}}. Field is named "a" instead of "field"- and enclosing type is named "receiver" instead of "currentClass" or "enclosingClass"-. > Access to package-scoped static field is forbidden for inner class in static > compilation mode > --------------------------------------------------------------------------------------------- > > Key: GROOVY-9043 > URL: https://issues.apache.org/jira/browse/GROOVY-9043 > Project: Groovy > Issue Type: Bug > Affects Versions: 3.0.0-alpha-4, 2.5.6 > Reporter: Basil Peace > Assignee: Paul King > Priority: Minor > Fix For: 3.0.0-beta-1, 2.5.7 > > > Given: > > {code:java} > import groovy.transform.CompileStatic > import groovy.transform.PackageScope > @CompileStatic > class Test { > @PackageScope > static final String S = 'Some constant' > static class Inner { > String method() { > S > } > } > } > {code} > there is a compilation error: > > > {noformat} > Access to Test#S is forbidden @ line -1, column -1.{noformat} > No error without @CompileStatic. > > Analogous code works in Java -- This message was sent by Atlassian JIRA (v7.6.3#76005)