Annotation parameter must be constant

This commit is contained in:
Natalia Ukhorskaya
2013-12-12 19:20:03 +04:00
parent 7a9d822070
commit 31ad4f8451
8 changed files with 100 additions and 1 deletions
@@ -117,6 +117,9 @@ public interface Errors {
DiagnosticFactory0<JetTypeReference> NULLABLE_TYPE_OF_ANNOTATION_MEMBER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetModifierListOwner> ILLEGAL_ANNOTATION_KEYWORD = DiagnosticFactory0
.create(ERROR, modifierSetPosition(JetTokens.ANNOTATION_KEYWORD));
DiagnosticFactory0<JetExpression> ANNOTATION_PARAMETER_MUST_BE_CONST = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetExpression> ANNOTATION_PARAMETER_MUST_BE_CLASS_LITERAL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetExpression> ANNOTATION_PARAMETER_MUST_BE_ENUM_CONST = DiagnosticFactory0.create(ERROR);
// Classes and traits
@@ -456,6 +456,9 @@ public class DefaultErrorMessages {
MAP.put(INVALID_TYPE_OF_ANNOTATION_MEMBER, "Invalid type of annotation member");
MAP.put(NULLABLE_TYPE_OF_ANNOTATION_MEMBER, "An annotation parameter cannot be nullable");
MAP.put(ILLEGAL_ANNOTATION_KEYWORD, "''annotation'' keyword is only applicable for class");
MAP.put(ANNOTATION_PARAMETER_MUST_BE_CONST, "An annotation parameter must be a compile-time constant");
MAP.put(ANNOTATION_PARAMETER_MUST_BE_ENUM_CONST, "An enum annotation parameter must be a enum constant");
MAP.put(ANNOTATION_PARAMETER_MUST_BE_CLASS_LITERAL, "An annotation parameter must be a class literal");
MAP.put(DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE, "An overriding function is not allowed to specify default values for its parameters");
@@ -255,6 +255,21 @@ public class AnnotationResolver {
if (constant != null) {
constants.add(constant);
}
else {
JetType expressionType = trace.get(BindingContext.EXPRESSION_TYPE, argumentExpression);
if (expressionType != null && expressionType.equals(expectedType)) {
ClassifierDescriptor descriptor = expressionType.getConstructor().getDeclarationDescriptor();
if (descriptor != null && DescriptorUtils.isEnumClass(descriptor)) {
trace.report(Errors.ANNOTATION_PARAMETER_MUST_BE_ENUM_CONST.on(argumentExpression));
}
else if (descriptor instanceof ClassDescriptor && AnnotationUtils.isJavaLangClass((ClassDescriptor) descriptor)) {
trace.report(Errors.ANNOTATION_PARAMETER_MUST_BE_CLASS_LITERAL.on(argumentExpression));
}
else {
trace.report(Errors.ANNOTATION_PARAMETER_MUST_BE_CONST.on(argumentExpression));
}
}
}
}
}
return constants;
@@ -125,7 +125,7 @@ public class AnnotationUtils {
return false;
}
private static boolean isJavaLangClass(ClassDescriptor descriptor) {
public static boolean isJavaLangClass(ClassDescriptor descriptor) {
return "java.lang.Class".equals(DescriptorUtils.getFQName(descriptor).asString());
}