You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2019/04/18 15:45:00 UTC

[jira] [Commented] (GROOVY-9043) Access to package-scoped static field is forbidden for inner class in static compilation mode

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

Eric Milles commented on GROOVY-9043:
-------------------------------------

Looks like that decision is made on the last line of this method in {{StaticTypesCallSiteWriter}}:
{code:java}
    private static boolean isDirectAccessAllowed(FieldNode a, ClassNode receiver, boolean isSamePackage) {
        ClassNode declaringClass = a.getDeclaringClass().redirect();
        ClassNode receiverType = receiver.redirect();

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

        // no getter
        return a.isPublic() || (a.isProtected() && isSamePackage);
    }
{code}

> 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)