Skip to content

Field is ambiguous in annotation but fine in class or script body #259

@AntonPanikov

Description

@AntonPanikov

If you have two (or more) on demand static imports with the same name to import, the first one must be imported, but the second and others must be ignored as in JavaSpecs: http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.5.4

It is working perfectly fine for non static on demand imports and for static on demand imports if you use imported names in code. But as soon as you try to use name in annotation, groovy compiler complains about ambiguous field.

I am using groovy eclipse of version: Groovy-Eclipse Feature 2.9.2.xx-201701250356-e46

Here is a sample example:

package test

interface TestStrings {
    public static final String ONE = 'one'
    public static final String TWO = 'two'
    public static final String THREE = 'three'
}

interface TestStrings2 {
    public static final String ONE = 'one from two'
    public static final String TWO = 'two from two'
    public static final String THREE = 'three from two'
}
package test

@interface TestAnnotation {
    String[] value()
}
package test

import static test.TestStrings.*
import static test.TestStrings2.*

class Test {

    @TestAnnotation(value = [ONE, TWO, THREE])
    static main(args) {
        println "$ONE"
    }

}

Groovy eclipse will complain about ONE, TWO, THREE constants in annotation, but not in println. Code is running fine and printing 'one' as expected and if you change order of static imports:

import static test.TestStrings.*
import static test.TestStrings2.*

then println will print 'one from two' as expected. So there is no ambiguous field, because the same field is used and worked fine in code and groovy eclipse is not complaining about it, while in annotations it is failing.

At the same time non static import on demand is working fine for both: code and annotation:

package test

import test.*
import test.sub.*

class Test {

    @TestAnnotation(value = [TestStrings.ONE, TestStrings.TWO, TestStrings.THREE])
    static main(args) {
        println "$TestStrings.ONE"
    }

}

If you have the same TestString interface defined in package test.sub, then no errors for this case will be raised.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions