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());
}
@@ -0,0 +1,14 @@
annotation class AnnC(val c: Class<*>)
AnnC(<!ANNOTATION_PARAMETER_MUST_BE_CLASS_LITERAL!>c<!>)
class Test
AnnC(javaClass<A>())
class Test2
val c: Class<*> = javaClass<A>()
class A
// from stdlib
fun <T> javaClass() : Class<T> = null <!CAST_NEVER_SUCCEEDS!>as<!> Class<T>
@@ -0,0 +1,36 @@
annotation class Ann(val i: Int)
annotation class AnnIA(val ia: IntArray)
annotation class AnnSA(val sa: Array<String>)
Ann(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>MyClass().i<!>)
Ann(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>O.i<!>)
Ann(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>i<!>)
Ann(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>i2<!>)
AnnIA(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>ia<!>)
AnnSA(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>sa<!>)
class Test {
val i = 1
Ann(<!ANNOTATION_PARAMETER_MUST_BE_CONST!>i<!>) val i2 = 1
}
var i = 1
val i2 = foo()
fun foo(): Int = 1
class MyClass {
val i = 1
}
object O {
val i = 1
}
val ia: IntArray = intArray(1, 2)
val sa: Array<String> = array("a", "b")
annotation class Ann2
// from stdlib
fun <T> array(vararg t : T) : Array<T> = t
fun intArray(vararg content : Int) : IntArray = content
@@ -0,0 +1,13 @@
annotation class AnnE(val i: MyEnum)
AnnE(<!ANNOTATION_PARAMETER_MUST_BE_ENUM_CONST!>e<!>)
class Test
val e: MyEnum = MyEnum.A
enum class MyEnum {
A
}
AnnE(<!TYPE_MISMATCH!>Test()<!>)
class Test2
@@ -540,6 +540,21 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/annotations/annotationModifier.kt");
}
@TestMetadata("annotationParameterMustBeClassLiteral.kt")
public void testAnnotationParameterMustBeClassLiteral() throws Exception {
doTest("compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeClassLiteral.kt");
}
@TestMetadata("annotationParameterMustBeConstant.kt")
public void testAnnotationParameterMustBeConstant() throws Exception {
doTest("compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeConstant.kt");
}
@TestMetadata("annotationParameterMustBeEnumConst.kt")
public void testAnnotationParameterMustBeEnumConst() throws Exception {
doTest("compiler/testData/diagnostics/tests/annotations/annotationParameterMustBeEnumConst.kt");
}
@TestMetadata("AnnotationsForClasses.kt")
public void testAnnotationsForClasses() throws Exception {
doTest("compiler/testData/diagnostics/tests/annotations/AnnotationsForClasses.kt");