Search in sources :

Example 1 with AnnotateMethodFix

use of com.intellij.codeInspection.AnnotateMethodFix 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)1 AnnotateMethodFix (com.intellij.codeInspection.AnnotateMethodFix)1 Project (com.intellij.openapi.project.Project)1 Nullable (org.jetbrains.annotations.Nullable)1