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

Eric Milles commented on GROOVY-5961:
-------------------------------------

Two additional edits in {{VariableScopeVisitor}} to alter the search for names 
when anonymous inner classes are encountered:
{code:java}
    private Variable findClassMember(ClassNode cn, String name) {
        ...
        Variable ret = findClassMember(cn.getSuperClass(), name);
        if (ret != null) return ret;
        // GRECLIPSE add
        if (isAnonymous(cn)) return null;
        // GRECLIPSE end
        return findClassMember(cn.getOuterClass(), name);
    }
{code}

{code:java}
    private Variable checkVariableNameForDeclaration(String name, Expression 
expression) {
        ...
            ClassNode classScope = scope.getClassScope();
            if (classScope != null) {
                Variable member = findClassMember(classScope, var.getName());
                if (member != null) {
                    boolean staticScope = crossingStaticContext || 
isSpecialConstructorCall;
                    boolean staticMember = member.isInStaticContext();
                    // We don't allow a static context (e.g. a static method) 
to access
                    // a non-static variable (e.g. a non-static field).
                    if (!(staticScope && !staticMember))
                        var = member;
                }
                // GRECLIPSE add
                if (!isAnonymous(classScope))
                // GRECLIPSE end
                break;
            }
            scope = scope.getParent();
        }
{code}


{{isAnonymous}} was extracted from {{visitClass}}:
{code:java}
    private static boolean isAnonymous(ClassNode node) {
        return (!node.isEnum() && node instanceof InnerClassNode && 
((InnerClassNode) node).isAnonymous());
    }

    public void visitClass(ClassNode node) {
        // AIC are already done, doing them here again will lead to wrong scopes
        /* GRECLIPSE edit
        if (node instanceof InnerClassNode) {
            InnerClassNode in = (InnerClassNode) node;
            if (in.isAnonymous() && !in.isEnum()) return;
        }
        */
        if (isAnonymous(node)) return;
        // GRECLIPSE end
{code}

> Variable scope not checked properly for AIC in static method
> ------------------------------------------------------------
>
>                 Key: GROOVY-5961
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5961
>             Project: Groovy
>          Issue Type: Sub-task
>          Components: Compiler
>    Affects Versions: 2.1.0
>            Reporter: Cédric Champeau
>            Priority: Major
>
> The following code will fail:
> {code}
> static void foo() {
>     new LinkedList() {
>         int index
>         Object get(int i) { super.get(index++) }
>     }
> }
> {code}
> The error is:
> {noformat}
> Apparent variable 'index' was found in a static scope but doesn't refer to a 
> local variable, static field or class. Possible causes:
> You attempted to reference a variable in the binding or an instance variable 
> from a static context.
> You misspelled a classname or statically imported field. Please check the 
> spelling.
> You attempted to use a method 'index' but left out brackets in a place not 
> allowed by the grammar.
>  at line: 5, column: 39
> {noformat}
> But obviously, {{index}} is not accessed from a static context here.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to