Search in sources :

Example 1 with ExternalAnnotationsManager

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

the class EditContractIntention method updateContract.

private static void updateContract(PsiMethod method, String contract, boolean pure) {
    Project project = method.getProject();
    ExternalAnnotationsManager manager = ExternalAnnotationsManager.getInstance(project);
    manager.deannotate(method, ControlFlowAnalyzer.ORG_JETBRAINS_ANNOTATIONS_CONTRACT);
    PsiAnnotation mockAnno = InferredAnnotationsManagerImpl.createContractAnnotation(project, pure, contract);
    if (mockAnno != null) {
        try {
            manager.annotateExternally(method, ControlFlowAnalyzer.ORG_JETBRAINS_ANNOTATIONS_CONTRACT, method.getContainingFile(), mockAnno.getParameterList().getAttributes());
        } catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {
        }
    }
    DaemonCodeAnalyzer.getInstance(project).restart();
}
Also used : Project(com.intellij.openapi.project.Project) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 2 with ExternalAnnotationsManager

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

the class AddAnnotationPsiFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiModifierListOwner myModifierListOwner = (PsiModifierListOwner) startElement;
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
    final PsiModifierList modifierList = myModifierListOwner.getModifierList();
    LOG.assertTrue(modifierList != null, myModifierListOwner + " (" + myModifierListOwner.getClass() + ")");
    if (modifierList.findAnnotation(myAnnotation) != null)
        return;
    final ExternalAnnotationsManager.AnnotationPlace annotationAnnotationPlace = annotationsManager.chooseAnnotationsPlace(myModifierListOwner);
    if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.NOWHERE)
        return;
    if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.EXTERNAL) {
        for (String fqn : myAnnotationsToRemove) {
            annotationsManager.deannotate(myModifierListOwner, fqn);
        }
        try {
            annotationsManager.annotateExternally(myModifierListOwner, myAnnotation, file, myPairs);
        } catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {
        }
    } else {
        final PsiFile containingFile = myModifierListOwner.getContainingFile();
        WriteCommandAction.runWriteCommandAction(project, null, null, () -> {
            removePhysicalAnnotations(myModifierListOwner, myAnnotationsToRemove);
            PsiAnnotation inserted = addPhysicalAnnotation(myAnnotation, myPairs, modifierList);
            JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
        }, containingFile);
        if (containingFile != file) {
            UndoUtil.markPsiFileForUndo(file);
        }
    }
}
Also used : ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 3 with ExternalAnnotationsManager

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

the class DeannotateIntentionAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    myAnnotationName = null;
    PsiModifierListOwner listOwner = getContainer(editor, file);
    if (listOwner != null) {
        final ExternalAnnotationsManager externalAnnotationsManager = ExternalAnnotationsManager.getInstance(project);
        final PsiAnnotation[] annotations = externalAnnotationsManager.findExternalAnnotations(listOwner);
        if (annotations != null && annotations.length > 0) {
            if (annotations.length == 1) {
                myAnnotationName = annotations[0].getQualifiedName();
            }
            final List<PsiFile> files = externalAnnotationsManager.findExternalAnnotationsFiles(listOwner);
            if (files == null || files.isEmpty())
                return false;
            final VirtualFile virtualFile = files.get(0).getVirtualFile();
            return virtualFile != null && (virtualFile.isWritable() || virtualFile.isInLocalFileSystem());
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 4 with ExternalAnnotationsManager

use of com.intellij.codeInsight.ExternalAnnotationsManager in project android by JetBrains.

the class LintIdeJavaParser method getAnnotations.

@NonNull
private static Iterable<ResolvedAnnotation> getAnnotations(@Nullable PsiModifierListOwner owner) {
    if (owner != null) {
        PsiModifierList modifierList = owner.getModifierList();
        if (modifierList != null) {
            PsiAnnotation[] annotations = modifierList.getAnnotations();
            if (annotations.length > 0) {
                List<ResolvedAnnotation> result = Lists.newArrayListWithExpectedSize(annotations.length);
                for (PsiAnnotation method : annotations) {
                    result.add(new ResolvedPsiAnnotation(method));
                }
                return result;
            }
        }
        ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(owner.getProject());
        PsiAnnotation[] annotations = annotationsManager.findExternalAnnotations(owner);
        if (annotations != null) {
            List<ResolvedAnnotation> result = Lists.newArrayListWithExpectedSize(annotations.length);
            for (PsiAnnotation method : annotations) {
                result.add(new ResolvedPsiAnnotation(method));
            }
            return result;
        }
    }
    return Collections.emptyList();
}
Also used : ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager) NonNull(com.android.annotations.NonNull)

Example 5 with ExternalAnnotationsManager

use of com.intellij.codeInsight.ExternalAnnotationsManager in project android by JetBrains.

the class ExternalAnnotationsSupport method checkAnnotationsJarAttached.

// Based on similar code in MagicConstantInspection
@SuppressWarnings("ALL")
private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
    // Not yet used
    if (false) {
        final Project project = file.getProject();
        PsiClass actionBar = JavaPsiFacade.getInstance(project).findClass("android.app.ActionBar", GlobalSearchScope.allScope(project));
        if (actionBar == null) {
            // no sdk to attach
            return;
        }
        PsiMethod[] methods = actionBar.findMethodsByName("getNavigationMode", false);
        if (methods.length != 1) {
            // no sdk to attach
            return;
        }
        PsiMethod getModifiers = methods[0];
        ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
        PsiAnnotation annotation = annotationsManager.findExternalAnnotation(getModifiers, MagicConstant.class.getName());
        if (annotation != null) {
            return;
        }
        final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
        if (virtualFile == null) {
            // no sdk to attach
            return;
        }
        final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
        Sdk sdk = null;
        for (OrderEntry orderEntry : entries) {
            if (orderEntry instanceof JdkOrderEntry) {
                sdk = ((JdkOrderEntry) orderEntry).getJdk();
                if (sdk != null) {
                    break;
                }
            }
        }
        if (sdk == null) {
            // no sdk to attach
            return;
        }
        final Sdk finalSdk = sdk;
        String path = finalSdk.getHomePath();
        String text = "No IDEA annotations attached to the Android SDK " + finalSdk.getName() + (path == null ? "" : " (" + FileUtil.toSystemDependentName(path) + ")") + ", some issues will not be found";
        holder.registerProblem(file, text, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix() {

            @NotNull
            @Override
            public String getName() {
                return "Attach annotations";
            }

            @NotNull
            @Override
            public String getFamilyName() {
                return getName();
            }

            @Override
            public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                    @Override
                    public void run() {
                        SdkModificator modifier = finalSdk.getSdkModificator();
                        attachJdkAnnotations(modifier);
                        modifier.commitChanges();
                    }
                });
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MagicConstant(org.intellij.lang.annotations.MagicConstant) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Sdk(com.intellij.openapi.projectRoots.Sdk) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Aggregations

ExternalAnnotationsManager (com.intellij.codeInsight.ExternalAnnotationsManager)7 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 NonNull (com.android.annotations.NonNull)1 NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1 ArrayList (java.util.ArrayList)1 MagicConstant (org.intellij.lang.annotations.MagicConstant)1 NotNull (org.jetbrains.annotations.NotNull)1