mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-21 05:51:25 +07:00
enum attribute values must be a constant (JLS 9.7.1 IDEA-132747)
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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() {}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user