From 4ae81fd62a0f26a1757f809ca8b9a12b8b21e7be Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 19 Oct 2020 13:38:37 +0700 Subject: [PATCH] [java-highlighting] IDEA-253230 Editor should produce a red code in records with @SafeVarargs in Java 16 GitOrigin-RevId: 5da7b9733e75140ad066b7e9b8af6adb95fea297 --- .../daemon/impl/analysis/AnnotationsHighlightUtil.java | 10 ++++++++++ .../daemon/impl/analysis/HighlightVisitorImpl.java | 1 + .../src/messages/JavaErrorBundle.properties | 3 ++- .../advHighlightingRecords/RecordBasics.java | 3 ++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java index 0320ba2ae2c4..da41e03530c2 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java @@ -566,6 +566,16 @@ public final class AnnotationsHighlightUtil { return null; } + static HighlightInfo checkInvalidAnnotationOnRecordComponent(@NotNull PsiAnnotation annotation) { + if (!Comparing.strEqual(annotation.getQualifiedName(), CommonClassNames.JAVA_LANG_SAFE_VARARGS)) return null; + PsiAnnotationOwner owner = annotation.getOwner(); + if (!(owner instanceof PsiModifierList)) return null; + PsiElement parent = ((PsiModifierList)owner).getParent(); + if (!(parent instanceof PsiRecordComponent)) return null; + return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(annotation) + .descriptionAndTooltip(JavaErrorBundle.message("safevararg.annotation.cannot.be.applied.for.record.component")).create(); + } + static HighlightInfo checkFunctionalInterface(@NotNull PsiAnnotation annotation, @NotNull LanguageLevel languageLevel) { if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8) && Comparing.strEqual(annotation.getQualifiedName(), CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE)) { final PsiAnnotationOwner owner = annotation.getOwner(); diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java index bc7a70e6c3a2..187cb176f22b 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java @@ -286,6 +286,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkTargetAnnotationDuplicates(annotation)); if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkDuplicateAnnotations(annotation, myLanguageLevel)); if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkFunctionalInterface(annotation, myLanguageLevel)); + if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkInvalidAnnotationOnRecordComponent(annotation)); if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkRepeatableAnnotation(annotation)); if (CommonClassNames.JAVA_LANG_OVERRIDE.equals(annotation.getQualifiedName())) { PsiAnnotationOwner owner = annotation.getOwner(); diff --git a/java/java-psi-impl/src/messages/JavaErrorBundle.properties b/java/java-psi-impl/src/messages/JavaErrorBundle.properties index 8ccdc4cacf3f..3610c10639ca 100644 --- a/java/java-psi-impl/src/messages/JavaErrorBundle.properties +++ b/java/java-psi-impl/src/messages/JavaErrorBundle.properties @@ -511,4 +511,5 @@ text.improper.formed.type=Improper formed type; some type parameters are missing text.class.is.not.accessible={0} is not accessible in current context text.class.cannot.access=Cannot access {0} auto.closeable.resource=auto-closeable resource -annotation.type.permits=No permits clause allowed for annotation type \ No newline at end of file +annotation.type.permits=No permits clause allowed for annotation type +safevararg.annotation.cannot.be.applied.for.record.component=@SafeVarargs annotation cannot be applied for a record component \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingRecords/RecordBasics.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingRecords/RecordBasics.java index 32c16e6fdbd1..201b494fe6bf 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingRecords/RecordBasics.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingRecords/RecordBasics.java @@ -71,4 +71,5 @@ record StaticFieldCollides(int i) { static int i; } record Incomplete(@int a) {} -record CStyle(int a[]) {} \ No newline at end of file +record CStyle(int a[]) {} +record SafeVarargComponent(@SafeVarargs int... component) {} \ No newline at end of file