Search in sources :

Example 6 with AddAnnotationFix

use of com.intellij.codeInsight.intention.AddAnnotationFix in project android by JetBrains.

the class AndroidLintAnimatorKeepInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    return new AndroidLintQuickFix[] { new AndroidLintQuickFix() {

        @Override
        public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
            if (!ObjectAnimatorDetector.isAddKeepErrorMessage(message, TextFormat.RAW)) {
                return;
            }
            PsiModifierListOwner container = PsiTreeUtil.getParentOfType(startElement, PsiModifierListOwner.class);
            if (container == null) {
                return;
            }
            if (!FileModificationService.getInstance().preparePsiElementForWrite(container)) {
                return;
            }
            final PsiModifierList modifierList = container.getModifierList();
            if (modifierList != null) {
                PsiAnnotation annotation = AnnotationUtil.findAnnotation(container, KEEP_ANNOTATION);
                if (annotation == null) {
                    Project project = startElement.getProject();
                    new AddAnnotationFix(KEEP_ANNOTATION, container).invoke(project, null, container.getContainingFile());
                }
            }
        }

        @Override
        public boolean isApplicable(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.ContextType contextType) {
            return true;
        }

        @NotNull
        @Override
        public String getName() {
            return "Annotate with @Keep";
        }
    } };
}
Also used : Project(com.intellij.openapi.project.Project) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix) PsiModifierList(com.intellij.psi.PsiModifierList) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with AddAnnotationFix

use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.

the class OverrideImplementUtil method annotate.

public static void annotate(@NotNull PsiMethod result, String fqn, String... annosToRemove) throws IncorrectOperationException {
    Project project = result.getProject();
    AddAnnotationFix fix = new AddAnnotationFix(fqn, result, annosToRemove);
    if (fix.isAvailable(project, null, result.getContainingFile())) {
        fix.invoke(project, null, result.getContainingFile());
    }
}
Also used : Project(com.intellij.openapi.project.Project) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix)

Example 8 with AddAnnotationFix

use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.

the class GrPullUpHelper method doMoveMethod.

private void doMoveMethod(PsiSubstitutor substitutor, MemberInfo info) {
    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
    GrMethod method = (GrMethod) info.getMember();
    PsiMethod sibling = method;
    PsiMethod anchor = null;
    while (sibling != null) {
        sibling = PsiTreeUtil.getNextSiblingOfType(sibling, PsiMethod.class);
        if (sibling != null) {
            anchor = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(method.getContainingClass(), myTargetSuperClass, sibling.getSignature(PsiSubstitutor.EMPTY), false);
            if (anchor != null) {
                break;
            }
        }
    }
    GrMethod methodCopy = (GrMethod) method.copy();
    if (method.findSuperMethods(myTargetSuperClass).length == 0) {
        deleteOverrideAnnotationIfFound(methodCopy);
    }
    final boolean isOriginalMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT) || method.hasModifierProperty(PsiModifier.DEFAULT);
    if (myTargetSuperClass.isInterface() || info.isToAbstract()) {
        GroovyChangeContextUtil.clearContextInfo(method);
        RefactoringUtil.makeMethodAbstract(myTargetSuperClass, methodCopy);
        if (myTargetSuperClass.isInterface()) {
            PsiUtil.setModifierProperty(methodCopy, PsiModifier.ABSTRACT, false);
        }
        replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
        final GrMethod movedElement = anchor != null ? (GrMethod) myTargetSuperClass.addBefore(methodCopy, anchor) : (GrMethod) myTargetSuperClass.add(methodCopy);
        CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(method.getProject());
        if (styleSettings.INSERT_OVERRIDE_ANNOTATION) {
            if (PsiUtil.isLanguageLevel5OrHigher(mySourceClass) && !myTargetSuperClass.isInterface() || PsiUtil.isLanguageLevel6OrHigher(mySourceClass)) {
                new AddAnnotationFix(CommonClassNames.JAVA_LANG_OVERRIDE, method).invoke(method.getProject(), null, mySourceClass.getContainingFile());
            }
        }
        GrDocComment oldDoc = method.getDocComment();
        if (oldDoc != null) {
            GrDocCommentUtil.setDocComment(movedElement, oldDoc);
        }
        myDocCommentPolicy.processCopiedJavaDoc(methodCopy.getDocComment(), oldDoc, isOriginalMethodAbstract);
        myMembersAfterMove.add(movedElement);
        if (isOriginalMethodAbstract) {
            deleteMemberWithDocComment(method);
        }
    } else {
        if (isOriginalMethodAbstract) {
            PsiUtil.setModifierProperty(myTargetSuperClass, PsiModifier.ABSTRACT, true);
        }
        fixReferencesToStatic(methodCopy);
        replaceMovedMemberTypeParameters(methodCopy, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
        final PsiMethod superClassMethod = myTargetSuperClass.findMethodBySignature(methodCopy, false);
        Language language = myTargetSuperClass.getLanguage();
        final PsiMethod movedElement;
        if (superClassMethod != null && superClassMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
            movedElement = (PsiMethod) superClassMethod.replace(convertMethodToLanguage(methodCopy, language));
        } else {
            movedElement = anchor != null ? (PsiMethod) myTargetSuperClass.addBefore(convertMethodToLanguage(methodCopy, language), anchor) : (PsiMethod) myTargetSuperClass.add(convertMethodToLanguage(methodCopy, language));
            myMembersAfterMove.add(movedElement);
        }
        if (movedElement instanceof GrMethod) {
            GrDocCommentUtil.setDocComment((GrDocCommentOwner) movedElement, method.getDocComment());
        }
        deleteMemberWithDocComment(method);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) Language(com.intellij.lang.Language) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 9 with AddAnnotationFix

use of com.intellij.codeInsight.intention.AddAnnotationFix in project intellij-community by JetBrains.

the class JUnit4Framework method findOrCreateSetUpMethod.

private PsiMethod findOrCreateSetUpMethod(PsiClass clazz, String beforeClassAnnotationName, String beforeAnnotationName) {
    PsiMethod method = findSetUpMethod(clazz);
    if (method != null)
        return method;
    PsiManager manager = clazz.getManager();
    PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
    method = createSetUpPatternMethod(factory);
    PsiMethod existingMethod = clazz.findMethodBySignature(method, false);
    if (existingMethod != null) {
        if (AnnotationUtil.isAnnotated(existingMethod, beforeClassAnnotationName, false))
            return existingMethod;
        int exit = ApplicationManager.getApplication().isUnitTestMode() ? Messages.OK : Messages.showOkCancelDialog("Method setUp already exist but is not annotated as @Before. Annotate?", CommonBundle.getWarningTitle(), Messages.getWarningIcon());
        if (exit == Messages.OK) {
            new AddAnnotationFix(beforeAnnotationName, existingMethod).invoke(existingMethod.getProject(), null, existingMethod.getContainingFile());
            return existingMethod;
        }
    }
    final PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz);
    if (testMethod != null) {
        method = (PsiMethod) clazz.addBefore(method, testMethod);
    } else {
        method = (PsiMethod) clazz.add(method);
    }
    JavaCodeStyleManager.getInstance(manager.getProject()).shortenClassReferences(method);
    return method;
}
Also used : AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix)

Example 10 with AddAnnotationFix

use of com.intellij.codeInsight.intention.AddAnnotationFix in project android by JetBrains.

the class SuppressLintIntentionAction method addSuppressAnnotation.

// Based on the equivalent code in com.intellij.codeInsight.daemon.impl.actions.SuppressFix
// to add @SuppressWarnings annotations
private static void addSuppressAnnotation(final Project project, final PsiElement container, final PsiModifierListOwner modifierOwner, final String id) throws IncorrectOperationException {
    PsiAnnotation annotation = AnnotationUtil.findAnnotation(modifierOwner, FQCN_SUPPRESS_LINT);
    final PsiAnnotation newAnnotation = createNewAnnotation(project, container, annotation, id);
    if (newAnnotation != null) {
        if (annotation != null && annotation.isPhysical()) {
            annotation.replace(newAnnotation);
        } else {
            final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();
            //noinspection ConstantConditions
            new AddAnnotationFix(FQCN_SUPPRESS_LINT, modifierOwner, attributes).invoke(project, null, /*editor*/
            container.getContainingFile());
        }
    }
}
Also used : AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix)

Aggregations

AddAnnotationFix (com.intellij.codeInsight.intention.AddAnnotationFix)11 Project (com.intellij.openapi.project.Project)3 PsiModifierListOwner (com.intellij.psi.PsiModifierListOwner)2 ASTNode (com.intellij.lang.ASTNode)1 Language (com.intellij.lang.Language)1 Annotation (com.intellij.lang.annotation.Annotation)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiElement (com.intellij.psi.PsiElement)1 PsiModifierList (com.intellij.psi.PsiModifierList)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 AndroidLintQuickFix (org.jetbrains.android.inspections.lint.AndroidLintQuickFix)1 Nls (org.jetbrains.annotations.Nls)1 NonNls (org.jetbrains.annotations.NonNls)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1