Search in sources :

Example 6 with InspectionGadgetsFix

use of com.siyeh.ig.InspectionGadgetsFix in project intellij-community by JetBrains.

the class TooBroadCatchInspection method buildFixes.

@NotNull
@Override
protected InspectionGadgetsFix[] buildFixes(Object... infos) {
    final PsiElement context = (PsiElement) infos[1];
    final SmartTypePointerManager pointerManager = SmartTypePointerManager.getInstance(context.getProject());
    final List<PsiType> maskedTypes = (List<PsiType>) infos[0];
    final List<InspectionGadgetsFix> fixes = new ArrayList<>();
    for (PsiType thrown : maskedTypes) {
        final String typeText = thrown.getCanonicalText();
        if (CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION.equals(typeText)) {
            fixes.add(new ReplaceWithRuntimeExceptionFix());
        } else {
            fixes.add(new AddCatchSectionFix(pointerManager.createSmartTypePointer(thrown), typeText));
        }
    }
    final InspectionGadgetsFix fix = SuppressForTestsScopeFix.build(this, context);
    if (fix != null) {
        fixes.add(fix);
    }
    return fixes.toArray(new InspectionGadgetsFix[fixes.size()]);
}
Also used : ArrayList(java.util.ArrayList) InspectionGadgetsFix(com.siyeh.ig.InspectionGadgetsFix) ArrayList(java.util.ArrayList) List(java.util.List) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with InspectionGadgetsFix

use of com.siyeh.ig.InspectionGadgetsFix in project intellij-community by JetBrains.

the class StringConcatenationInspectionBase method buildFixes.

@Override
@NotNull
protected InspectionGadgetsFix[] buildFixes(Object... infos) {
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) infos[0];
    final Collection<InspectionGadgetsFix> result = new ArrayList<>();
    final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(polyadicExpression);
    if (parent instanceof PsiVariable) {
        final PsiVariable variable = (PsiVariable) parent;
        final InspectionGadgetsFix fix = createAddAnnotationFix(variable);
        result.add(fix);
    } else if (parent instanceof PsiAssignmentExpression) {
        final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent;
        final PsiExpression lhs = assignmentExpression.getLExpression();
        if (lhs instanceof PsiReferenceExpression) {
            final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) lhs;
            final PsiElement target = referenceExpression.resolve();
            if (target instanceof PsiModifierListOwner) {
                final PsiModifierListOwner modifierListOwner = (PsiModifierListOwner) target;
                final InspectionGadgetsFix fix = createAddAnnotationFix(modifierListOwner);
                result.add(fix);
            }
        }
    } else if (parent instanceof PsiExpressionList) {
        final PsiElement grandParent = parent.getParent();
        if (grandParent instanceof PsiMethodCallExpression) {
            final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) grandParent;
            final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
            final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
            if (qualifierExpression instanceof PsiReferenceExpression) {
                final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) qualifierExpression;
                final PsiElement target = referenceExpression.resolve();
                if (target instanceof PsiModifierListOwner) {
                    final PsiModifierListOwner modifierListOwner = (PsiModifierListOwner) target;
                    result.add(createAddAnnotationFix(modifierListOwner));
                }
            }
        }
    }
    final PsiExpression[] operands = polyadicExpression.getOperands();
    for (PsiExpression operand : operands) {
        final PsiModifierListOwner element1 = getAnnotatableElement(operand);
        if (element1 != null) {
            final InspectionGadgetsFix fix = createAddAnnotationFix(element1);
            result.add(fix);
        }
    }
    final PsiElement expressionParent = PsiTreeUtil.getParentOfType(polyadicExpression, PsiReturnStatement.class, PsiExpressionList.class);
    if (!(expressionParent instanceof PsiExpressionList) && expressionParent != null) {
        final PsiMethod method = PsiTreeUtil.getParentOfType(expressionParent, PsiMethod.class);
        if (method != null) {
            final InspectionGadgetsFix fix = createAddAnnotationFix(method);
            result.add(fix);
        }
    }
    return result.toArray(new InspectionGadgetsFix[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) InspectionGadgetsFix(com.siyeh.ig.InspectionGadgetsFix) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with InspectionGadgetsFix

use of com.siyeh.ig.InspectionGadgetsFix in project intellij-community by JetBrains.

the class StringConcatenationInspection method buildFixes.

@NotNull
@Override
protected InspectionGadgetsFix[] buildFixes(Object... infos) {
    final InspectionGadgetsFix[] fixes = super.buildFixes(infos);
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) infos[0];
    final SuppressForTestsScopeFix suppressFix = SuppressForTestsScopeFix.build(this, polyadicExpression);
    if (suppressFix == null) {
        return fixes;
    }
    final InspectionGadgetsFix[] newFixes = Arrays.copyOf(fixes, fixes.length + 1);
    newFixes[fixes.length] = suppressFix;
    return newFixes;
}
Also used : PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) InspectionGadgetsFix(com.siyeh.ig.InspectionGadgetsFix) SuppressForTestsScopeFix(com.siyeh.ig.fixes.SuppressForTestsScopeFix) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with InspectionGadgetsFix

use of com.siyeh.ig.InspectionGadgetsFix in project intellij-community by JetBrains.

the class ParameterizedParametersStaticCollectionInspection method buildFix.

@Override
protected InspectionGadgetsFix buildFix(final Object... infos) {
    return new InspectionGadgetsFix() {

        @Override
        protected void doFix(final Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
            final PsiElement element = descriptor.getPsiElement();
            final PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
            if (method != null && infos[1] instanceof PsiType) {
                PsiType type = (PsiType) infos[1];
                final ChangeSignatureProcessor csp = new ChangeSignatureProcessor(project, method, false, PsiModifier.PUBLIC, method.getName(), type, new ParameterInfoImpl[0]);
                csp.run();
            } else {
                final PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
                if (psiClass != null) {
                    final CreateMethodQuickFix fix = CreateMethodQuickFix.createFix(psiClass, "@" + PARAMETERS_FQN + " public static java.util.Collection parameters()", "");
                    if (fix != null) {
                        fix.applyFix(project, descriptor);
                    }
                }
            }
        }

        @Override
        @NotNull
        public String getName() {
            return infos.length > 0 ? (String) infos[0] : "Create @Parameterized.Parameters data provider";
        }

        @NotNull
        @Override
        public String getFamilyName() {
            return "Fix data provider signature";
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) InspectionGadgetsFix(com.siyeh.ig.InspectionGadgetsFix) CreateMethodQuickFix(com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix) ChangeSignatureProcessor(com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)

Example 10 with InspectionGadgetsFix

use of com.siyeh.ig.InspectionGadgetsFix in project intellij-community by JetBrains.

the class StaticImportInspection method buildFixes.

@NotNull
@Override
protected InspectionGadgetsFix[] buildFixes(Object... infos) {
    final PsiElement context = (PsiElement) infos[0];
    final SuppressForTestsScopeFix fix = SuppressForTestsScopeFix.build(this, context);
    if (fix == null) {
        return new InspectionGadgetsFix[] { buildFix(infos) };
    }
    return new InspectionGadgetsFix[] { buildFix(infos), fix };
}
Also used : InspectionGadgetsFix(com.siyeh.ig.InspectionGadgetsFix) SuppressForTestsScopeFix(com.siyeh.ig.fixes.SuppressForTestsScopeFix) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

InspectionGadgetsFix (com.siyeh.ig.InspectionGadgetsFix)14 NotNull (org.jetbrains.annotations.NotNull)13 PsiElement (com.intellij.psi.PsiElement)6 ArrayList (java.util.ArrayList)5 SuppressForTestsScopeFix (com.siyeh.ig.fixes.SuppressForTestsScopeFix)4 DelegatingFix (com.siyeh.ig.DelegatingFix)2 RenameFix (com.siyeh.ig.fixes.RenameFix)2 AddTypeCastFix (com.intellij.codeInsight.daemon.impl.quickfix.AddTypeCastFix)1 CreateMethodQuickFix (com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix)1 AddAnnotationPsiFix (com.intellij.codeInsight.intention.AddAnnotationPsiFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Project (com.intellij.openapi.project.Project)1 PsiField (com.intellij.psi.PsiField)1 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)1 ChangeSignatureProcessor (com.intellij.refactoring.changeSignature.ChangeSignatureProcessor)1 EncapsulateVariableFix (com.siyeh.ig.fixes.EncapsulateVariableFix)1 IntroduceConstantFix (com.siyeh.ig.fixes.IntroduceConstantFix)1 RemoveModifierFix (com.siyeh.ig.fixes.RemoveModifierFix)1 List (java.util.List)1