Search in sources :

Example 21 with NullableNotNullManager

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

the class AnnotateOverriddenMethodsIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element) {
    final PsiAnnotation annotation = (PsiAnnotation) element;
    final String annotationName = annotation.getQualifiedName();
    if (annotationName == null) {
        return;
    }
    final Project project = element.getProject();
    final NullableNotNullManager notNullManager = NullableNotNullManager.getInstance(project);
    final List<String> notNulls = notNullManager.getNotNulls();
    final List<String> nullables = notNullManager.getNullables();
    final List<String> annotationsToRemove;
    if (notNulls.contains(annotationName)) {
        annotationsToRemove = nullables;
    } else if (nullables.contains(annotationName)) {
        annotationsToRemove = notNulls;
    } else {
        annotationsToRemove = Collections.emptyList();
    }
    final PsiElement parent = annotation.getParent();
    final PsiElement grandParent = parent.getParent();
    final PsiMethod method;
    final int parameterIndex;
    if (!(grandParent instanceof PsiMethod)) {
        if (!(grandParent instanceof PsiParameter)) {
            return;
        }
        final PsiParameter parameter = (PsiParameter) grandParent;
        final PsiElement greatGrandParent = grandParent.getParent();
        if (!(greatGrandParent instanceof PsiParameterList)) {
            return;
        }
        final PsiParameterList parameterList = (PsiParameterList) greatGrandParent;
        parameterIndex = parameterList.getParameterIndex(parameter);
        final PsiElement greatGreatGrandParent = greatGrandParent.getParent();
        if (!(greatGreatGrandParent instanceof PsiMethod)) {
            return;
        }
        method = (PsiMethod) greatGreatGrandParent;
    } else {
        parameterIndex = -1;
        method = (PsiMethod) grandParent;
    }
    final Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(method).findAll();
    final List<PsiMethod> prepare = new ArrayList<>();
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
    for (PsiMethod overridingMethod : overridingMethods) {
        if (annotationsManager.chooseAnnotationsPlace(overridingMethod) == ExternalAnnotationsManager.AnnotationPlace.IN_CODE) {
            prepare.add(overridingMethod);
        }
    }
    if (!FileModificationService.getInstance().preparePsiElementsForWrite(prepare)) {
        return;
    }
    final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    try {
        for (PsiMethod overridingMethod : overridingMethods) {
            if (parameterIndex == -1) {
                annotate(overridingMethod, annotationName, attributes, annotationsToRemove, annotationsManager);
            } else {
                final PsiParameterList parameterList = overridingMethod.getParameterList();
                final PsiParameter[] parameters = parameterList.getParameters();
                final PsiParameter parameter = parameters[parameterIndex];
                annotate(parameter, annotationName, attributes, annotationsToRemove, annotationsManager);
            }
        }
    } catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {
    //escape on configuring root cancel further annotations
    }
    if (!prepare.isEmpty()) {
        UndoUtil.markPsiFileForUndo(annotation.getContainingFile());
    }
}
Also used : NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 22 with NullableNotNullManager

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

the class ReturnNullInspectionBase method buildFix.

@Override
@Nullable
protected InspectionGadgetsFix buildFix(Object... infos) {
    final PsiElement elt = (PsiElement) infos[0];
    if (!AnnotationUtil.isAnnotatingApplicable(elt)) {
        return null;
    }
    final PsiElement element = PsiTreeUtil.getParentOfType(elt, PsiMethod.class, PsiLambdaExpression.class);
    if (element instanceof PsiLambdaExpression) {
        return null;
    }
    if (element instanceof PsiMethod) {
        final PsiMethod method = (PsiMethod) element;
        final PsiType type = method.getReturnType();
        if (TypeUtils.isOptional(type)) {
            // don't suggest to annotate Optional methods as Nullable
            return new ReplaceWithEmptyOptionalFix(((PsiClassType) type).rawType().getCanonicalText());
        }
    }
    final NullableNotNullManager manager = NullableNotNullManager.getInstance(elt.getProject());
    return new DelegatingFix(new AnnotateMethodFix(manager.getDefaultNullable(), ArrayUtil.toStringArray(manager.getNotNulls())) {

        @Override
        public int shouldAnnotateBaseMethod(PsiMethod method, PsiMethod superMethod, Project project) {
            return ReturnNullInspectionBase.this.shouldAnnotateBaseMethod(method, superMethod);
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) NullableNotNullManager(com.intellij.codeInsight.NullableNotNullManager) AnnotateMethodFix(com.intellij.codeInspection.AnnotateMethodFix) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)22 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)3 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)2 NonNls (org.jetbrains.annotations.NonNls)2 CodeInsightUtil (com.intellij.codeInsight.CodeInsightUtil)1 ExternalAnnotationsManager (com.intellij.codeInsight.ExternalAnnotationsManager)1 JavaChainLookupElement (com.intellij.codeInsight.completion.JavaChainLookupElement)1 JavaCompletionUtil (com.intellij.codeInsight.completion.JavaCompletionUtil)1 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 AddAnnotationPsiFix (com.intellij.codeInsight.intention.AddAnnotationPsiFix)1 AddNullableNotNullAnnotationFix (com.intellij.codeInsight.intention.impl.AddNullableNotNullAnnotationFix)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupManager (com.intellij.codeInsight.lookup.LookupManager)1 ScopeHighlighter (com.intellij.codeInsight.unwrap.ScopeHighlighter)1 AnnotateMethodFix (com.intellij.codeInspection.AnnotateMethodFix)1 ChainCompletionLookupElementUtil.createLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionLookupElementUtil.createLookupElement)1 ChainCompletionNewVariableLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionNewVariableLookupElement)1 WeightableChainLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement)1