Search in sources :

Example 11 with AddAnnotationFix

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

the class AddTargetApiQuickFix method apply.

@SuppressWarnings("unchecked")
@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
    // Find nearest method or class; can't add @TargetApi on modifier list owners like variable declarations
    @SuppressWarnings("unchecked") PsiModifierListOwner container = PsiTreeUtil.getParentOfType(startElement, PsiMethod.class, PsiClass.class);
    if (container == null) {
        // XML file? Set attribute
        XmlTag element = PsiTreeUtil.getParentOfType(startElement, XmlTag.class, false);
        if (element != null) {
            XmlFile file = PsiTreeUtil.getParentOfType(element, XmlFile.class, false);
            if (file != null) {
                AndroidResourceUtil.ensureNamespaceImported(file, TOOLS_URI, null);
                String codeName = SdkVersionInfo.getBuildCode(myApi);
                if (codeName == null) {
                    codeName = Integer.toString(myApi);
                } else {
                    codeName = codeName.toLowerCase(Locale.US);
                }
                element.setAttribute(ATTR_TARGET_API, TOOLS_URI, codeName);
            }
        }
        return;
    }
    while (container != null && container instanceof PsiAnonymousClass) {
        container = PsiTreeUtil.getParentOfType(container, PsiMethod.class, true, PsiClass.class);
    }
    if (container == null) {
        return;
    }
    if (!FileModificationService.getInstance().preparePsiElementForWrite(container)) {
        return;
    }
    final PsiModifierList modifierList = container.getModifierList();
    if (modifierList != null) {
        Project project = startElement.getProject();
        PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
        String fqcn = myRequiresApi ? REQUIRES_API_ANNOTATION : FQCN_TARGET_API;
        String annotationText;
        if (myRequiresApi) {
            annotationText = "@" + fqcn + "(api=" + getAnnotationValue(true) + ")";
        } else {
            annotationText = "@" + fqcn + "(" + getAnnotationValue(true) + ")";
        }
        PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText(annotationText, container);
        PsiAnnotation annotation = AnnotationUtil.findAnnotation(container, FQCN_TARGET_API);
        if (annotation != null && annotation.isPhysical()) {
            annotation.replace(newAnnotation);
        } else {
            PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();
            AddAnnotationFix fix = new AddAnnotationFix(fqcn, container, attributes);
            fix.invoke(project, null, /*editor*/
            container.getContainingFile());
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) Project(com.intellij.openapi.project.Project) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix) XmlTag(com.intellij.psi.xml.XmlTag)

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