[ 
https://issues.apache.org/jira/browse/GROOVY-9043?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16821262#comment-16821262
 ] 

Eric Milles edited comment on GROOVY-9043 at 4/18/19 6:32 PM:
--------------------------------------------------------------

Based on how the inner class check is described, I would rewrite the method 
like this:
{code:java}
    private static boolean isDirectAccessAllowed(FieldNode field, ClassNode 
receiver, boolean isSamePackage) {
        ClassNode declaringClass = field.getDeclaringClass().redirect();
        ClassNode receiverType = receiver.redirect();

        // first, direct access from within the class or inner class nodes
        while (receiverType != null) {
            if (declaringClass.equals(receiverType)) {
                return true;
            }
            if (/*a.isPrivate()*/(a.getModifiers() & ACC_PRIVATE) != 0) {
                return false;
            }
            receiverType = receiverType.getOuterClass();
        }

        return a.isPublic() || isSamePackage;
    }
{code}

This allows inner classes and same-package classes to access an outer class 
field of non-private visibility (private fields handled elsewhere).  -And any 
class in the same package to access a field that is public, protected or 
package-private.  What seems to be missing is subclass access to protected 
fields.-


was (Author: emilles):
Based on how the inner class check is described, I would rewrite the method 
like this:
{code:java}
    private static boolean isDirectAccessAllowed(FieldNode field, ClassNode 
receiver, boolean isSamePackage) {
        ClassNode declaringClass = field.getDeclaringClass().redirect();
        ClassNode receiverType = receiver.redirect();

        // first, direct access from within the class or inner class nodes
        while (receiverType != null) {
            if (declaringClass.equals(receiverType)) {
                return true;
            }
            receiverType = receiverType.getOuterClass();
        }

        return field.isPublic() || (isSamePackage && 
/*!field.isPrivate()*/(field.getModifiers() & ACC_PRIVATE) == 0);
    }
{code}

This would allow inner classes to access an outer class field of any 
visibility.  And any class in the same package to access a field that is 
public, protected or package-private.  What seems to be missing is subclass 
access to protected fields.

> 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
>            Priority: Minor
>
> 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)

Reply via email to