enum attribute values must be a constant (JLS 9.7.1 IDEA-132747)

This commit is contained in:
Anna Kozlova
2014-11-12 18:56:14 +01:00
parent 05ff6e8a83
commit d98a3da35a
4 changed files with 21 additions and 0 deletions

View File

@@ -145,6 +145,13 @@ public class AnnotationsHighlightUtil {
if (value instanceof PsiExpression) {
PsiExpression expr = (PsiExpression)value;
PsiType type = expr.getType();
final PsiClass psiClass = PsiUtil.resolveClassInType(type);
if (psiClass != null && psiClass.isEnum() && !(expr instanceof PsiReferenceExpression && ((PsiReferenceExpression)expr).resolve() instanceof PsiEnumConstant)) {
String description = JavaErrorMessages.message("annotation.non.enum.constant.attribute.value");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(value).descriptionAndTooltip(description).create();
}
if (type != null && TypeConversionUtil.areTypesAssignmentCompatible(expectedType, expr) ||
expectedType instanceof PsiArrayType &&
TypeConversionUtil.areTypesAssignmentCompatible(((PsiArrayType)expectedType).getComponentType(), expr)) {

View File

@@ -10,6 +10,7 @@ annotation.missing.attribute={0} missing though required
annotation.not.applicable=''@{0}'' not applicable to {1}
annotation.non.constant.attribute.value=Attribute value must be constant
annotation.non.class.literal.attribute.value=Attribute value must be a class literal
annotation.non.enum.constant.attribute.value=Attribute value must be an enum constant
annotation.invalid.annotation.member.type=Invalid type for annotation member
annotation.cyclic.element.type=Cyclic annotation element type
annotation.annotation.type.expected=Annotation type expected

View File

@@ -0,0 +1,12 @@
class EnumExample {
static @interface MyAnnotation {
MyEnum enumValue();
}
static enum MyEnum { E }
static final MyEnum E = MyEnum.E;
@MyAnnotation(enumValue = <error descr="Attribute value must be an enum constant">E</error>)
void method() {}
}

View File

@@ -49,6 +49,7 @@ public class AnnotationsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testTypeAnnotations() { doTest8(false); }
public void testRepeatable() { doTest8(false); }
public void testEnumValues() { doTest8(false); }
private void doTest(boolean checkWarnings) {
setLanguageLevel(LanguageLevel.JDK_1_7);