[java] better error message on @interface throws/extends list not allowed (IDEA-350501)

GitOrigin-RevId: 1f89c42ac5ffda1d026f6e40cb922b08a095d1dc
This commit is contained in:
Bas Leijdekkers
2024-04-03 17:50:34 +02:00
committed by intellij-monorepo-bot
parent 01711f3ea2
commit 89c8dc03ed
4 changed files with 19 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.AnnotationTargetUtil;
@@ -600,17 +600,18 @@ public final class AnnotationsHighlightUtil {
if (list == method.getThrowsList()) {
String description = JavaErrorBundle.message("annotation.members.may.not.have.throws.list");
HighlightInfo.Builder info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description);
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list.getFirstChild()).descriptionAndTooltip(description);
IntentionAction action = QuickFixFactory.getInstance().createDeleteFix(list);
info.registerFix(action, null, null, null, null);
return info;
}
}
else if (parent instanceof PsiClass && ((PsiClass)parent).isAnnotationType()) {
if (PsiKeyword.EXTENDS.equals(list.getFirstChild().getText())) {
else if (parent instanceof PsiClass aClass && aClass.isAnnotationType()) {
PsiElement child = list.getFirstChild();
if (PsiUtil.isJavaToken(child, JavaTokenType.EXTENDS_KEYWORD)) {
String description = JavaErrorBundle.message("annotation.may.not.have.extends.list");
HighlightInfo.Builder info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(list).descriptionAndTooltip(description);
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(child).descriptionAndTooltip(description);
IntentionAction action = QuickFixFactory.getInstance().createDeleteFix(list);
info.registerFix(action, null, null, null, null);
return info;

View File

@@ -13,8 +13,8 @@ annotation.non.enum.constant.attribute.value=Attribute value must be an enum con
annotation.invalid.annotation.member.type=Invalid type ''{0}'' for annotation member
annotation.cyclic.element.type=Cyclic annotation element type
annotation.annotation.type.expected=Annotation type expected
annotation.members.may.not.have.throws.list=@interface members may not have throws list
annotation.may.not.have.extends.list=@interface may not have extends list
annotation.members.may.not.have.throws.list='throws' not allowed on @interface method
annotation.may.not.have.extends.list='extends' not allowed on @interface
annotation.not.allowed.ref=Annotation not applicable to this kind of reference
annotation.not.allowed.static=Static member qualifying type may not be annotated
annotation.not.allowed.void='void' type may not be annotated

View File

@@ -0,0 +1,9 @@
@interface Declarations <error descr="'extends' not allowed on @interface">extends</error> java.io.Serializable {
int x() <error descr="'throws' not allowed on @interface method">throws</error> IllegalArgumentException;
}
@interface Seconds <error descr="'permits' not allowed on @interface">permits</error> Declarations {
<error descr="@interface members may not have type parameters"><T></error> int y();
}
@interface Generic<error descr="@interface may not have type parameters"><T></error> {}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInsight.daemon;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
@@ -37,6 +37,7 @@ public class AnnotationsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testEnumValues() { doTest(); }
public void testReceiverParameters() { doTest(); }
public void testAnnotationOverIncompleteCode() { doTest(); }
public void testDeclarations() { doTest(); }
private void doTest() { doTest(getTestName(true) + ".java"); }
private void doTest(String name) { doTest(BASE_PATH + "/" + name, false, false); }