Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliases can not be used in annotaions #260

Closed
AntonPanikov opened this issue Jan 25, 2017 · 5 comments
Closed

Aliases can not be used in annotaions #260

AntonPanikov opened this issue Jan 25, 2017 · 5 comments

Comments

@AntonPanikov
Copy link

If your annotation is expecting class value and you aliased it, using alias will raise an error, while using original class name - not. It was working perfectly fine in the past.

I am using groovy eclipse: 2.9.2.xx-201701250356-e46

Here is an example:

package test

class TestClass {
}
package test

@interface TestAnnotation {
    Class<?>[] value()
}
package test

import test.TestClass
import test.TestClass as ALIAS

class Test {

    //@TestAnnotation(value = [TestClass]) // Works
    @TestAnnotation(value = [ALIAS]) // Does not
    static main(args) {
        println 'hello groovy'
    }

}

Groovy eclipse will complain about using alias, but using original class name will work fine. But in both cases code will run fine.

@eric-milles
Copy link
Member

eric-milles commented Jan 26, 2017

It is this bit from MemberValuePair

	private Expression repairClassLiteralReference(Expression exp, BlockScope scope, TypeBinding[] valueType) {
		TypeBinding vtb = null;
		if (exp instanceof SingleNameReference) {
			vtb = exp.resolveType(scope);
			SingleNameReference ref = (SingleNameReference) exp;
			if (vtb != null && Arrays.equals(ref.token, vtb.sourceName())) {
				return new ClassLiteralAccess(ref.sourceEnd, new SingleTypeReference(ref.token, ((long) ref.sourceStart) << 32 | ref.sourceEnd));
			}

vtb resolves to the class but the equals check fails and so it is not corrected to a class literal reference.

@AntonPanikov
Copy link
Author

Checked this one, it fixed all cases, but my test example is still failing:

package test

import test.TestClass  // comment this and everything will be fine
import test.TestClass as ALIAS

class Test {

    @TestAnnotation(value = [ALIAS])
    static main(args) {
        println 'hello groovy'
    }

}

If both imports are present, then ALIAS is still not a class. If only aliased import here - everything working fine. I do not know if it as a valid case, but looks like it is.

@eric-milles
Copy link
Member

The repeated use of TestClass in your example is a general issue with imports. If you import a type and alias it in a compilation unit, you will get the resolution errors if the 2nd is used in annotations, extends, implements, throws, generics, method params/return type and fields. That is, any place a type reference is copied into the Java model, this will occur.

@AntonPanikov
Copy link
Author

Thanks for the explanation. In this case, this can be considered as done.

@eric-milles
Copy link
Member

I am trying out a possible solution to the import issue. The commit will be linked here if I come up with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants