mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
[java-highlighting] Provide the fix for the "*annotation* not applicable to *target*" error (IDEA-216251)
GitOrigin-RevId: ed6eb867457c4b3413156366be89a076c639ccf3
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8c36219f10
commit
8bb8d7ca5d
@@ -545,4 +545,6 @@ public abstract class QuickFixFactory {
|
||||
|
||||
@Nullable
|
||||
public abstract IntentionAction createDeleteDefaultFix(@NotNull PsiFile file, @Nullable Object highlightInfo);
|
||||
|
||||
public abstract @NotNull IntentionAction createAddAnnotationTargetFix(@NotNull PsiAnnotation annotation, PsiAnnotation.TargetType target);
|
||||
}
|
||||
@@ -396,7 +396,13 @@ public final class AnnotationsHighlightUtil {
|
||||
if (applicable == null) {
|
||||
String target = JavaAnalysisBundle.message("annotation.target." + targets[0]);
|
||||
String message = JavaErrorBundle.message("annotation.not.applicable", nameRef.getText(), target);
|
||||
return annotationError(annotation, message);
|
||||
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;
|
||||
}
|
||||
|
||||
if (applicable == PsiAnnotation.TargetType.TYPE_USE) {
|
||||
|
||||
@@ -410,4 +410,7 @@ implement.or.extend.fix.extend.text=Extend ''{0}''
|
||||
seal.class.from.permits.list.fix=Seal inheritor
|
||||
|
||||
unwrap.array.initializer.fix=Replace array initializer with its element
|
||||
replace.with.type.pattern.fix=Replace with type pattern
|
||||
replace.with.type.pattern.fix=Replace with type pattern
|
||||
|
||||
add.annotation.target.fix=Add the ''{0}'' target
|
||||
add.annotation.target.family=Add annotation target
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AddAnnotationTargetFix implements IntentionAction {
|
||||
@NotNull private final PsiAnnotation myAnnotation;
|
||||
@NotNull private final PsiAnnotation.TargetType myTarget;
|
||||
|
||||
public AddAnnotationTargetFix(@NotNull PsiAnnotation annotation, @NotNull PsiAnnotation.TargetType target) {
|
||||
myAnnotation = annotation;
|
||||
myTarget = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getText() {
|
||||
return QuickFixBundle.message("add.annotation.target.fix", myTarget);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getFamilyName() {
|
||||
return QuickFixBundle.message("add.annotation.target.family");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
final PsiClass annotationType = myAnnotation.resolveAnnotationType();
|
||||
if (annotationType == null) return;
|
||||
final PsiModifierList modifierList = annotationType.getModifierList();
|
||||
if (modifierList == null) return;
|
||||
final PsiAnnotation annotation = modifierList.findAnnotation(CommonClassNames.JAVA_LANG_ANNOTATION_TARGET);
|
||||
if (annotation == null) return;
|
||||
final PsiNameValuePair attribute = AnnotationUtil.findDeclaredAttribute(annotation, null);
|
||||
if (attribute == null) return;
|
||||
PsiAnnotationMemberValue value = attribute.getValue();
|
||||
if (value == null) return;
|
||||
if (!(value instanceof PsiArrayInitializerMemberValue)) {
|
||||
PsiAnnotation dummyAnnotation =
|
||||
JavaPsiFacade.getElementFactory(project).createAnnotationFromText("@A({" + value.getText() + "})", null);
|
||||
final PsiAnnotationMemberValue wrappedValue = dummyAnnotation.getParameterList().getAttributes()[0].getValue();
|
||||
value = annotation.setDeclaredAttributeValue("value", wrappedValue);
|
||||
}
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
|
||||
final PsiExpression expression = factory.createExpressionFromText("java.lang.annotation.ElementType." + myTarget, value);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression);
|
||||
value.addAfter(expression, value.getFirstChild());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1105,4 +1105,11 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
if (descriptor == null) return null;
|
||||
return new LocalQuickFixAsIntentionAdapter(new UnnecessaryDefaultInspection.DeleteDefaultFix(), descriptor);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionAction createAddAnnotationTargetFix(@NotNull PsiAnnotation annotation,
|
||||
@NotNull PsiAnnotation.TargetType target) {
|
||||
return new AddAnnotationTargetFix(annotation, target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD,})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Add the 'FIELD' target" "false"
|
||||
class Main {
|
||||
@Override<caret> int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo<caret> int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({,})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo<caret> int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo<caret> int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Add the 'FIELD' target" "true"
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@interface Foo {}
|
||||
|
||||
class Main {
|
||||
@Foo<caret> int x = 42;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
|
||||
public class AddAnnotationTargetFixTest extends LightQuickFixParameterizedTestCase {
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/addAnnotationTarget";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user