-
Notifications
You must be signed in to change notification settings - Fork 194
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
Compilation error on annotation parameters #233
Comments
In your case, is FOO and BAR really of mixed case like Foo and Bar? I had to rely on convention for class literal vs constant expression. At the moment, it looks like this: } else if (expr instanceof PropertyExpression) {
PropertyExpression prop = (PropertyExpression) expr;
assert prop.getProperty() instanceof ConstantExpression;
String name = prop.getPropertyAsString();
if (name.equals("class") || !NON_TYPE_NAME.matcher(name).matches()) {
return new ClassLiteralAccess(expr.getEnd(), createTypeReferenceForClassLiteral(prop));
}
char[][] tokens = CharOperation.splitOn('.', prop.getText().toCharArray());
// guess the intermediate positions based on start offset and token lengths
int n = tokens.length, s = prop.getObjectExpression().getStart();
long[] positions = new long[n];
for (int i = 0; i < n; i += 1) {
positions[i] = toPos(s, s + tokens[i].length - 1);
s += tokens[i].length;
}
assert s <= expr.getEnd();
return new QualifiedNameReference(tokens, positions, expr.getStart(), expr.getEnd());
// TODO: Look up name to determine if it is a class or a constant
private static final Pattern NON_TYPE_NAME = Pattern.compile("[a-z_]\\w*|[A-Z][A-Z_0-9]*"); I am hoping to find a better solution for this but there was no immediate solution available. This happens during parsing and so there is very little type information available. Java had the ".class" or not that made things easy. Groovy's optional ".class" for Class literals makes things a bit more difficult. |
There is a way around the compile error if you want to try out the other changes. You can name your enum constants using standard static final notation, that is THIS_IS_A_CONSTANT and not ThisIsAType. Or you could assign the enum values to local static constants and use those in the annotation, like this: @Options([FOO, BAR])
class SomeSpec extends BaseSpec {
private static final Options FOO = Options.Foo, BAR = Options.Bar;
} And you could probably use static imports as well, like this: import static wherever.Options.Foo as FOO
import static wherever.Options.Bar as BAR
@Options([FOO, BAR])
class SomeSpec extends BaseSpec {
} I understand these are temporary measures. Thanks for reporting the issue and bearing with me. |
Yes, the enum constants are things like DoNotClearDatabase. I will give the static import a whirl, thanks. Do you not have enough type information to deduce that SpecOption is an enum? I'm guessing that Groovy doesn't have an equivalent of the Java AST given the flexibility of types. |
During parsing and conversion, type resolution has not run, so it is really AST information only. The structure of the Groovy AST within annotations leaves ambiguity with respect to the attribute values. All this gets sorted out in Groovy land. It is the parallel Java model that gets created where the error comes from. In earlier versions of the plug-in, only a few annotations got processed into the Java model, namely @test and @RunWith, so that JUnit functions would work. I replaced the special-case handling with general handling of annotation values to help with syntax coloring and code hover/select. Unfortunately, class literals are still not fully resolved. I'm thinking of another way to choose between class literal and constant value. |
Neither of the static import or the local constant worked, unfortunately. Only renaming the enum constant to use constant naming style worked. Unfortunately, the enum that I am using is from another project that I don't have control over, so I can't change the names of the constants. The class would appear in the import list, so if you find a value of the form |
Can you retest with the latest snapshot? |
I just upgraded to 2.9.2-xx-201611222343-e46 to see if there were bug fixes for some of the groovy editor issues I've been experiencing. Unfortunately, this build has completely broken my projects, so I am going to have to back out to the earlier groovy-eclipse version.
I have several groovy unit tests that had previously been working fine. The tests all extend a common base class, so I want to pass in some options to configure individual tests. These options are specified with an annotation called @options, and I have an enum called SpecOption to list the various options. The @options annotation has a single attribute which is an array of SpecOptions.
So for example, I might have a class annotated:
Now I get a compile error saying:
Type mismatch: cannot convert from Class<SpecOption> to SpecOption
and there doesn't seem to be any way to get rid of it. This is a complete blocker for me.
The text was updated successfully, but these errors were encountered: