Search in sources :

Example 1 with AddAnnotationPsiFix

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

the class AnnotateOverriddenMethodParameterFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    PsiParameter parameter = PsiTreeUtil.getParentOfType(psiElement, PsiParameter.class, false);
    if (parameter == null)
        return;
    PsiMethod method = PsiTreeUtil.getParentOfType(parameter, PsiMethod.class);
    if (method == null)
        return;
    PsiParameter[] parameters = method.getParameterList().getParameters();
    int index = ArrayUtilRt.find(parameters, parameter);
    List<PsiParameter> toAnnotate = new ArrayList<>();
    PsiMethod[] methods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod psiMethod : methods) {
        PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
        if (index >= psiParameters.length)
            continue;
        PsiParameter psiParameter = psiParameters[index];
        if (!AnnotationUtil.isAnnotated(psiParameter, myAnnotation, false, false) && psiMethod.getManager().isInProject(psiMethod)) {
            toAnnotate.add(psiParameter);
        }
    }
    FileModificationService.getInstance().preparePsiElementsForWrite(toAnnotate);
    for (PsiParameter psiParam : toAnnotate) {
        assert psiParam != null : toAnnotate;
        if (AnnotationUtil.isAnnotatingApplicable(psiParam, myAnnotation)) {
            AddAnnotationPsiFix fix = new AddAnnotationPsiFix(myAnnotation, psiParam, PsiNameValuePair.EMPTY_ARRAY, myAnnosToRemove);
            fix.invoke(project, psiParam.getContainingFile(), psiParam, psiParam);
        }
    }
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix) PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 2 with AddAnnotationPsiFix

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

the class NullableStuffInspectionBase method checkAccessors.

private void checkAccessors(PsiField field, Annotated annotated, Project project, NullableNotNullManager manager, final String anno, final List<String> annoToRemove, @NotNull ProblemsHolder holder) {
    String propName = JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(field.getName(), VariableKind.FIELD);
    final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    final PsiMethod getter = PropertyUtil.findPropertyGetter(field.getContainingClass(), propName, isStatic, false);
    final PsiIdentifier nameIdentifier = getter == null ? null : getter.getNameIdentifier();
    if (nameIdentifier != null && nameIdentifier.isPhysical()) {
        if (PropertyUtil.isSimpleGetter(getter)) {
            AnnotateMethodFix getterAnnoFix = new AnnotateMethodFix(anno, ArrayUtil.toStringArray(annoToRemove)) {

                @Override
                public int shouldAnnotateBaseMethod(PsiMethod method, PsiMethod superMethod, Project project) {
                    return 1;
                }
            };
            if (REPORT_NOT_ANNOTATED_GETTER) {
                if (!manager.hasNullability(getter) && !TypeConversionUtil.isPrimitiveAndNotNull(getter.getReturnType())) {
                    holder.registerProblem(nameIdentifier, InspectionsBundle.message("inspection.nullable.problems.annotated.field.getter.not.annotated", getPresentableAnnoName(field)), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, getterAnnoFix);
                }
            }
            if (annotated.isDeclaredNotNull && isNullableNotInferred(getter, false) || annotated.isDeclaredNullable && isNotNullNotInferred(getter, false, false)) {
                holder.registerProblem(nameIdentifier, InspectionsBundle.message("inspection.nullable.problems.annotated.field.getter.conflict", getPresentableAnnoName(field), getPresentableAnnoName(getter)), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, getterAnnoFix);
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final PsiMethod setter = PropertyUtil.findPropertySetter(containingClass, propName, isStatic, false);
    if (setter != null && setter.isPhysical()) {
        final PsiParameter[] parameters = setter.getParameterList().getParameters();
        assert parameters.length == 1 : setter.getText();
        final PsiParameter parameter = parameters[0];
        LOG.assertTrue(parameter != null, setter.getText());
        AddAnnotationPsiFix addAnnoFix = createAddAnnotationFix(anno, annoToRemove, parameter);
        if (REPORT_NOT_ANNOTATED_GETTER && !manager.hasNullability(parameter) && !TypeConversionUtil.isPrimitiveAndNotNull(parameter.getType())) {
            final PsiIdentifier nameIdentifier1 = parameter.getNameIdentifier();
            assertValidElement(setter, parameter, nameIdentifier1);
            holder.registerProblem(nameIdentifier1, InspectionsBundle.message("inspection.nullable.problems.annotated.field.setter.parameter.not.annotated", getPresentableAnnoName(field)), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, addAnnoFix);
        }
        if (PropertyUtil.isSimpleSetter(setter)) {
            if (annotated.isDeclaredNotNull && isNullableNotInferred(parameter, false)) {
                final PsiIdentifier nameIdentifier1 = parameter.getNameIdentifier();
                assertValidElement(setter, parameter, nameIdentifier1);
                holder.registerProblem(nameIdentifier1, InspectionsBundle.message("inspection.nullable.problems.annotated.field.setter.parameter.conflict", getPresentableAnnoName(field), getPresentableAnnoName(parameter)), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, addAnnoFix);
            }
        }
    }
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix) Project(com.intellij.openapi.project.Project)

Example 3 with AddAnnotationPsiFix

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

the class JavaSuppressionUtil method addSuppressAnnotation.

public static void addSuppressAnnotation(@NotNull Project project, final PsiElement container, final PsiModifierListOwner modifierOwner, @NotNull String id) throws IncorrectOperationException {
    PsiAnnotation annotation = AnnotationUtil.findAnnotation(modifierOwner, SUPPRESS_INSPECTIONS_ANNOTATION_NAME);
    final PsiAnnotation newAnnotation = createNewAnnotation(project, container, annotation, id);
    if (newAnnotation != null) {
        if (annotation != null && annotation.isPhysical()) {
            WriteCommandAction.runWriteCommandAction(project, null, null, () -> annotation.replace(newAnnotation), annotation.getContainingFile());
        } else {
            final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();
            new AddAnnotationPsiFix(SUPPRESS_INSPECTIONS_ANNOTATION_NAME, modifierOwner, attributes).applyFix();
        }
    }
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix)

Example 4 with AddAnnotationPsiFix

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

the class CallToSuspiciousStringMethodInspection method buildFixes.

@Override
@NotNull
protected InspectionGadgetsFix[] buildFixes(Object... infos) {
    final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) infos[0];
    final List<InspectionGadgetsFix> result = new ArrayList<>();
    final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
    final PsiModifierListOwner annotatableQualifier = NonNlsUtils.getAnnotatableQualifier(methodExpression);
    if (annotatableQualifier != null) {
        final InspectionGadgetsFix fix = new DelegatingFix(new AddAnnotationPsiFix(AnnotationUtil.NON_NLS, annotatableQualifier, PsiNameValuePair.EMPTY_ARRAY));
        result.add(fix);
    }
    final PsiModifierListOwner annotatableArgument = NonNlsUtils.getAnnotatableArgument(methodCallExpression);
    if (annotatableArgument != null) {
        final InspectionGadgetsFix fix = new DelegatingFix(new AddAnnotationPsiFix(AnnotationUtil.NON_NLS, annotatableArgument, PsiNameValuePair.EMPTY_ARRAY));
        result.add(fix);
    }
    return result.toArray(new InspectionGadgetsFix[result.size()]);
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix) DelegatingFix(com.siyeh.ig.DelegatingFix) ArrayList(java.util.ArrayList) InspectionGadgetsFix(com.siyeh.ig.InspectionGadgetsFix) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with AddAnnotationPsiFix

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

the class StringToUpperWithoutLocaleInspection method buildFix.

@Override
@Nullable
protected InspectionGadgetsFix buildFix(Object... infos) {
    final PsiReferenceExpression methodExpression = (PsiReferenceExpression) infos[0];
    final PsiModifierListOwner annotatableQualifier = NonNlsUtils.getAnnotatableQualifier(methodExpression);
    if (annotatableQualifier == null) {
        return null;
    }
    return new DelegatingFix(new AddAnnotationPsiFix(AnnotationUtil.NON_NLS, annotatableQualifier, PsiNameValuePair.EMPTY_ARRAY));
}
Also used : AddAnnotationPsiFix(com.intellij.codeInsight.intention.AddAnnotationPsiFix) DelegatingFix(com.siyeh.ig.DelegatingFix) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AddAnnotationPsiFix (com.intellij.codeInsight.intention.AddAnnotationPsiFix)7 DelegatingFix (com.siyeh.ig.DelegatingFix)3 ArrayList (java.util.ArrayList)2 NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiMethod (com.intellij.psi.PsiMethod)1 PsiParameter (com.intellij.psi.PsiParameter)1 InspectionGadgetsFix (com.siyeh.ig.InspectionGadgetsFix)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1