[java] fix parsed TYPE_USE annotation applicability (IDEA-291306)

GitOrigin-RevId: cb735529eae2adcfd9414e4921e9569a75018772
This commit is contained in:
Anna Kozlova
2022-04-07 16:32:23 +02:00
committed by intellij-monorepo-bot
parent b5cafe9cdc
commit 15a9ef4789
3 changed files with 38 additions and 22 deletions

View File

@@ -398,17 +398,23 @@ public final class AnnotationsHighlightUtil {
if (applicable == PsiAnnotation.TargetType.UNKNOWN) return null;
if (applicable == null) {
String target = JavaAnalysisBundle.message("annotation.target." + targets[0]);
String message = JavaErrorBundle.message("annotation.not.applicable", nameRef.getText(), target);
HighlightInfo info = annotationError(annotation, message);
if (Objects.requireNonNull(annotation.resolveAnnotationType()).isWritable()) {
for (PsiAnnotation.TargetType targetType : targets) {
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddAnnotationTargetFix(annotation, targetType));
if (targets.length == 1 && targets[0] == PsiAnnotation.TargetType.TYPE_USE) {
PsiElement parent = annotation.getParent();
if (parent instanceof PsiTypeElement) {
PsiElement modifierList = PsiTreeUtil.skipSiblingsBackward(parent, PsiWhiteSpace.class, PsiComment.class, PsiTypeParameterList.class);
if (modifierList instanceof PsiModifierList) {
targets = AnnotationTargetUtil.getTargetsForLocation((PsiModifierList)modifierList);
if (AnnotationTargetUtil.findAnnotationTarget(annotation, targets) == null) {
return notApplicableTargetInfo(annotation, nameRef, targets);
}
return null;
}
}
}
return info;
return notApplicableTargetInfo(annotation, nameRef, targets);
}
if (applicable == PsiAnnotation.TargetType.TYPE_USE) {
if (owner instanceof PsiClassReferenceType) {
PsiJavaCodeReferenceElement ref = ((PsiClassReferenceType)owner).getReference();
@@ -452,6 +458,21 @@ public final class AnnotationsHighlightUtil {
return null;
}
@Nullable
private static HighlightInfo notApplicableTargetInfo(@NotNull PsiAnnotation annotation,
PsiJavaCodeReferenceElement nameRef,
PsiAnnotation.TargetType[] targets) {
String target = JavaAnalysisBundle.message("annotation.target." + targets[0]);
String message = JavaErrorBundle.message("annotation.not.applicable", nameRef.getText(), target);
HighlightInfo info = annotationError(annotation, message);
if (Objects.requireNonNull(annotation.resolveAnnotationType()).isWritable()) {
for (PsiAnnotation.TargetType targetType : targets) {
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddAnnotationTargetFix(annotation, targetType));
}
}
return info;
}
@Nullable
private static HighlightInfo annotationError(@NotNull PsiAnnotation annotation, @NotNull @NlsContexts.DetailedDescription String message) {
LocalQuickFixAndIntentionActionOnPsiElement fix = QUICK_FIX_FACTORY.createDeleteFix(annotation, JavaAnalysisBundle.message("intention.text.remove.annotation"));

View File

@@ -0,0 +1,8 @@
@interface Ann { }
class Foo {
<K> @Ann String getFoo() {
return null;
}
}

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2022 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;
@@ -38,6 +24,7 @@ public class AnnotationsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testNonConstantInitializer() { doTest(); }
public void testInvalidType() { doTest(); }
public void testInapplicable() { doTest(); }
public void testFalseTypeUse() { doTest(); }
public void testDuplicateAttribute() { doTest(); }
public void testDuplicateTarget() { doTest(); }
public void testPingPongAnnotationTypesDependencies() { doTest(); }