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

Eric Milles commented on GROOVY-9472:
-------------------------------------

It is this block in {{ResolveVisitor}} that adds type resolution for static 
imports:
{code:java}
    // GROOVY-7812(#1): Static inner classes cannot be accessed from other 
files when running by 'groovy' command
    // if the type to resolve is an inner class and it is in an outer class 
which is not resolved,
    // we set the resolved type to a placeholder class node, i.e. a 
ConstructedOuterNestedClass instance
    // when resolving the outer class later, we set the resolved type of 
ConstructedOuterNestedClass instance to the actual inner class node(SEE 
GROOVY-7812(#2))
    private boolean resolveToOuterNested(final ClassNode type) {
        CompileUnit compileUnit = currentClass.getCompileUnit();
        if (compileUnit == null) return false;
        String typeName = type.getName();

        BiConsumer<ConstructedOuterNestedClassNode, ClassNode> 
setRedirectListener = (s, c) -> type.setRedirect(s);

        ModuleNode module = currentClass.getModule();
        for (ImportNode importNode : module.getStaticImports().values()) {
            String importFieldName = importNode.getFieldName();
            String importAlias = importNode.getAlias();

            if (!typeName.equals(importAlias)) continue;

            ConstructedOuterNestedClassNode constructedOuterNestedClassNode = 
tryToConstructOuterNestedClassNodeViaStaticImport(compileUnit, importNode, 
importFieldName, setRedirectListener);
            if (null != constructedOuterNestedClassNode) {
                
compileUnit.addClassNodeToResolve(constructedOuterNestedClassNode);
                return true;
            }
        }
{code}

> Static import causes unresolved reference to become resolved
> ------------------------------------------------------------
>
>                 Key: GROOVY-9472
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9472
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler, Static Type Checker
>    Affects Versions: 2.5.10
>            Reporter: Daniil Ovchinnikov
>            Priority: Major
>
> {code:groovy|title=com/foo/Person.groovy}
> package com.foo
> @groovy.transform.builder.Builder
> class Person {
>     String name
> }
> {code}
> 1.
> {code:groovy|title=Main.groovy}
> import com.foo.Person
> class Main {
>     static void main(String[] args) {
>         Person.PersonBuilder pb = Person.builder() 
>         println(pb.build())
>     }
> }
> {code}
> Trying to use it without a static import yields {{unable to resolve class 
> Person.PersonBuilder}}, which is another issue.
> 2. Let's add a static import
> {code:groovy|title=Main.groovy}
> import com.foo.Person
> import static com.foo.Person.PersonBuilder
> class Main {
>     static void main(String[] args) {
>         PersonBuilder pb = Person.builder()
>         println(pb.build())
>     }
> }
> {code}
> The code compiles, but fails with {{java.lang.NoClassDefFoundError: 
> PersonBuilder}} when run.
> 3. Let's add {{@CompileStatic}}
> {code:groovy|title=Main.groovy}
> import com.foo.Person
> import static com.foo.Person.PersonBuilder
> import groovy.transform.CompileStatic
> @CompileStatic
> class Main {
>     static void main(String[] args) {
>         PersonBuilder pb = Person.builder()
>         println(pb.build())
>     }
> }
> {code}
> Compilation fails with: 
>  {{Cannot assign value of type com.foo.Person$PersonBuilder to variable of 
> type PersonBuilder}} and {{Cannot find matching method PersonBuilder#build()}}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to