Java: improve error highlighting for unknown annotation method

GitOrigin-RevId: 6fdb6e2f397280cf4795cbd09ddcc1461a5897ff
This commit is contained in:
Bas Leijdekkers
2024-07-30 16:42:57 +02:00
committed by intellij-monorepo-bot
parent 1bdb332af2
commit a35a9f91ba
6 changed files with 17 additions and 28 deletions

View File

@@ -71,25 +71,18 @@ public final class AnnotationsHighlightUtil {
if (ref == null) return null;
PsiMethod method = (PsiMethod)ref.resolve();
if (method == null) {
if (pair.getName() != null) {
String description = JavaErrorBundle.message("annotation.unknown.method", ref.getCanonicalText());
HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF)
.range(ref.getElement(), ref.getRangeInElement())
.descriptionAndTooltip(description);
IntentionAction action = QuickFixFactory.getInstance().createCreateAnnotationMethodFromUsageFix(pair);
builder.registerFix(action, null, null, null, null);
return builder;
}
else {
String description = JavaErrorBundle.message("annotation.missing.method", ref.getCanonicalText());
PsiElement element = ref.getElement();
HighlightInfo.Builder highlightInfo =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description);
boolean noName = pair.getName() == null;
String description = JavaErrorBundle.message("annotation.unknown.method", ref.getCanonicalText());
HighlightInfo.Builder highlightInfo = HighlightInfo.newHighlightInfo(noName ? HighlightInfoType.ERROR : HighlightInfoType.WRONG_REF)
.range(ref.getElement())
.descriptionAndTooltip(description);
if (noName) {
for (IntentionAction action : QuickFixFactory.getInstance().createAddAnnotationAttributeNameFixes(pair)) {
highlightInfo.registerFix(action, null, null, null, null);
}
return highlightInfo;
}
highlightInfo.registerFix(QuickFixFactory.getInstance().createCreateAnnotationMethodFromUsageFix(pair), null, null, null, null);
return highlightInfo;
}
else {
PsiType returnType = method.getReturnType();

View File

@@ -1038,14 +1038,11 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitNameValuePair(@NotNull PsiNameValuePair pair) {
add(AnnotationsHighlightUtil.checkNameValuePair(pair));
if (!hasErrorResults()) {
PsiIdentifier nameId = pair.getNameIdentifier();
if (nameId != null) {
HighlightInfo.Builder result = HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.ANNOTATION_ATTRIBUTE_NAME).range(nameId);
add(result);
}
PsiIdentifier nameId = pair.getNameIdentifier();
if (nameId != null) {
add(HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.ANNOTATION_ATTRIBUTE_NAME).range(nameId));
}
add(AnnotationsHighlightUtil.checkNameValuePair(pair));
}
@Override

View File

@@ -1,7 +1,6 @@
# @interface (annotation) related messages
annotation.not.allowed.here=Annotations are not allowed here
annotation.unknown.method=Cannot resolve method ''{0}''
annotation.missing.method=Cannot find method ''{0}''
annotation.unknown.method=Cannot find @interface method ''{0}()''
annotation.illegal.array.initializer=Illegal initializer for ''{0}''
annotation.duplicate.annotation=Duplicate annotation
annotation.duplicate.attribute=Duplicate attribute ''{0}''

View File

@@ -2,5 +2,5 @@
int u () default 0;
}
@Ann(<error descr="Cannot resolve method 'v'">v</error>=0) class D {
@Ann(<error descr="Cannot find @interface method 'v()'">v=0</error>) class D {
}

View File

@@ -2,7 +2,7 @@
int u () default 0;
}
@Ann(<error descr="Cannot find method 'value'">0</error>) class D {
@Ann(<error descr="Cannot find @interface method 'value()'">0</error>) class D {
}
@In(<error descr="Annotation attribute of the form 'name=value' expected">""</error>, create = "")

View File

@@ -20,8 +20,8 @@ class KotlinAnnotations {
public static void m4() {
}
@k.Anno1(<error descr="Cannot resolve method 'x'">x</error> = 1)
@k.Anno2(<error descr="Cannot resolve method 'x'">x</error> = 2)
@k.Anno1(<error descr="Cannot find @interface method 'x()'">x = 1</error>)
@k.Anno2(<error descr="Cannot find @interface method 'x()'">x = 2</error>)
public static void m5() {
}
}