Search in sources :

Example 1 with GrModifierFix

use of org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix in project intellij-community by JetBrains.

the class GroovyAnnotator method checkForAbstractAndFinalCombination.

private static void checkForAbstractAndFinalCombination(AnnotationHolder holder, GrMember member, GrModifierList modifiersList) {
    if (member.hasModifierProperty(PsiModifier.FINAL) && member.hasModifierProperty(PsiModifier.ABSTRACT)) {
        final Annotation annotation = holder.createErrorAnnotation(modifiersList, GroovyBundle.message("illegal.combination.of.modifiers.abstract.and.final"));
        registerFix(annotation, new GrModifierFix(member, PsiModifier.FINAL, false, false, GrModifierFix.MODIFIER_LIST), modifiersList);
        registerFix(annotation, new GrModifierFix(member, PsiModifier.ABSTRACT, false, false, GrModifierFix.MODIFIER_LIST), modifiersList);
    }
}
Also used : GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) Annotation(com.intellij.lang.annotation.Annotation)

Example 2 with GrModifierFix

use of org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix in project intellij-community by JetBrains.

the class GroovyAnnotator method checkDuplicateModifiers.

private static void checkDuplicateModifiers(AnnotationHolder holder, @NotNull GrModifierList list, PsiMember member) {
    final PsiElement[] modifiers = list.getModifiers();
    Set<String> set = new THashSet<>(modifiers.length);
    for (PsiElement modifier : modifiers) {
        if (modifier instanceof GrAnnotation)
            continue;
        @GrModifier.GrModifierConstant String name = modifier.getText();
        if (set.contains(name)) {
            final Annotation annotation = holder.createErrorAnnotation(list, GroovyBundle.message("duplicate.modifier", name));
            registerFix(annotation, new GrModifierFix(member, name, false, false, GrModifierFix.MODIFIER_LIST), list);
        } else {
            set.add(name);
        }
    }
}
Also used : GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) THashSet(gnu.trove.THashSet) Annotation(com.intellij.lang.annotation.Annotation)

Example 3 with GrModifierFix

use of org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix in project intellij-community by JetBrains.

the class GroovyAnnotator method checkFieldModifiers.

private static void checkFieldModifiers(AnnotationHolder holder, GrVariableDeclaration fieldDeclaration) {
    GrVariable[] variables = fieldDeclaration.getVariables();
    if (variables.length == 0)
        return;
    GrVariable variable = variables[0];
    if (!(variable instanceof GrField))
        return;
    final GrField member = (GrField) variable;
    final GrModifierList modifierList = fieldDeclaration.getModifierList();
    checkAccessModifiers(holder, modifierList, member);
    checkDuplicateModifiers(holder, modifierList, member);
    if (modifierList.hasExplicitModifier(PsiModifier.VOLATILE) && modifierList.hasExplicitModifier(PsiModifier.FINAL)) {
        final Annotation annotation = holder.createErrorAnnotation(modifierList, GroovyBundle.message("illegal.combination.of.modifiers.volatile.and.final"));
        registerFix(annotation, new GrModifierFix(member, PsiModifier.VOLATILE, true, false, GrModifierFix.MODIFIER_LIST), modifierList);
        registerFix(annotation, new GrModifierFix(member, PsiModifier.FINAL, true, false, GrModifierFix.MODIFIER_LIST), modifierList);
    }
    if (member.getContainingClass() instanceof GrInterfaceDefinition) {
        checkModifierIsNotAllowed(modifierList, PsiModifier.PRIVATE, GroovyBundle.message("interface.members.are.not.allowed.to.be", PsiModifier.PRIVATE), holder);
        checkModifierIsNotAllowed(modifierList, PsiModifier.PROTECTED, GroovyBundle.message("interface.members.are.not.allowed.to.be", PsiModifier.PROTECTED), holder);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) Annotation(com.intellij.lang.annotation.Annotation)

Example 4 with GrModifierFix

use of org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix in project intellij-community by JetBrains.

the class GroovyVariableCanBeFinalInspection method process.

private static void process(@NotNull GrControlFlowOwner owner, @NotNull GrVariable variable, @NotNull ProblemsHolder problemsHolder) {
    if (variable.hasModifierProperty(PsiModifier.FINAL))
        return;
    if (!checkVariableDeclaredInsideScope(owner, variable))
        return;
    if (checkVariableAssignedInsideClosureOrAnonymous(owner, variable))
        return;
    final boolean isParameterTooltip = variable instanceof GrParameter && (((GrParameter) variable).getDeclarationScope() instanceof GrMethod || ((GrParameter) variable).getDeclarationScope() instanceof GrClosableBlock);
    final String tooltip = GroovyInspectionBundle.message(isParameterTooltip ? "parameter.can.be.final.tooltip" : "variable.can.be.final.tooltip", variable.getName());
    problemsHolder.registerProblem(variable.getNameIdentifierGroovy(), tooltip, new GrModifierFix(variable, PsiModifier.FINAL, true, ID_MODIFIER_LIST_PROVIDER));
}
Also used : GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 5 with GrModifierFix

use of org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix in project intellij-community by JetBrains.

the class GrMethodMayBeStaticInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitMethod(@NotNull GrMethod method) {
            if (checkMethod(method)) {
                final GrModifierFix modifierFix = new GrModifierFix(method, PsiModifier.STATIC, false, true, descriptor -> {
                    final PsiElement element = descriptor.getPsiElement();
                    final PsiElement parent = element.getParent();
                    assert parent instanceof GrMethod : "element: " + element + ", parent:" + parent;
                    return ((GrMethod) parent).getModifierList();
                });
                registerError(method.getNameIdentifierGroovy(), message("method.may.be.static"), new LocalQuickFix[] { modifierFix }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) NotNull(org.jetbrains.annotations.NotNull) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)9 Annotation (com.intellij.lang.annotation.Annotation)6 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 THashSet (gnu.trove.THashSet)1 NotNull (org.jetbrains.annotations.NotNull)1 BaseInspectionVisitor (org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1