Search in sources :

Example 6 with GrModifierFix

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

the class GroovyAnnotator method checkMethodDefinitionModifiers.

private static void checkMethodDefinitionModifiers(AnnotationHolder holder, GrMethod method) {
    final GrModifierList modifiersList = method.getModifierList();
    checkAccessModifiers(holder, modifiersList, method);
    checkDuplicateModifiers(holder, modifiersList, method);
    checkOverrideAnnotation(holder, modifiersList, method);
    checkModifierIsNotAllowed(modifiersList, PsiModifier.VOLATILE, GroovyBundle.message("method.has.incorrect.modifier.volatile"), holder);
    checkForAbstractAndFinalCombination(holder, method, modifiersList);
    //script methods
    boolean isMethodAbstract = modifiersList.hasExplicitModifier(PsiModifier.ABSTRACT);
    if (method.getParent() instanceof GroovyFileBase) {
        if (isMethodAbstract) {
            final Annotation annotation = holder.createErrorAnnotation(getModifierOrList(modifiersList, PsiModifier.ABSTRACT), GroovyBundle.message("script.method.cannot.have.modifier.abstract"));
            registerMakeAbstractMethodNotAbstractFix(annotation, method, false);
        }
        checkModifierIsNotAllowed(modifiersList, PsiModifier.NATIVE, GroovyBundle.message("script.cannot.have.modifier.native"), holder);
    } else //type definition methods
    if (method.getParent() != null && method.getParent().getParent() instanceof GrTypeDefinition) {
        GrTypeDefinition containingTypeDef = ((GrTypeDefinition) method.getParent().getParent());
        if (containingTypeDef.isTrait()) {
            checkModifierIsNotAllowed(modifiersList, PsiModifier.PROTECTED, GroovyBundle.message("trait.method.cannot.be.protected"), holder);
        } else //interface
        if (containingTypeDef.isInterface()) {
            checkModifierIsNotAllowed(modifiersList, PsiModifier.STATIC, GroovyBundle.message("interface.must.have.no.static.method"), holder);
            checkModifierIsNotAllowed(modifiersList, PsiModifier.PRIVATE, GroovyBundle.message("interface.members.are.not.allowed.to.be", PsiModifier.PRIVATE), holder);
            checkModifierIsNotAllowed(modifiersList, PsiModifier.PROTECTED, GroovyBundle.message("interface.members.are.not.allowed.to.be", PsiModifier.PROTECTED), holder);
        } else if (containingTypeDef.isAnonymous()) {
            if (isMethodAbstract) {
                final Annotation annotation = holder.createErrorAnnotation(getModifierOrList(modifiersList, PsiModifier.ABSTRACT), GroovyBundle.message("anonymous.class.cannot.have.abstract.method"));
                registerMakeAbstractMethodNotAbstractFix(annotation, method, false);
            }
        } else //class
        {
            PsiModifierList typeDefModifiersList = containingTypeDef.getModifierList();
            LOG.assertTrue(typeDefModifiersList != null, "modifiers list must be not null");
            if (!typeDefModifiersList.hasModifierProperty(PsiModifier.ABSTRACT) && isMethodAbstract) {
                final Annotation annotation = holder.createErrorAnnotation(modifiersList, GroovyBundle.message("only.abstract.class.can.have.abstract.method"));
                registerMakeAbstractMethodNotAbstractFix(annotation, method, true);
            }
        }
        if (method.isConstructor()) {
            checkModifierIsNotAllowed(modifiersList, PsiModifier.STATIC, GroovyBundle.message("constructor.cannot.have.static.modifier"), holder);
        }
    }
    if (method.hasModifierProperty(PsiModifier.NATIVE) && method.getBlock() != null) {
        final Annotation annotation = holder.createErrorAnnotation(getModifierOrList(modifiersList, PsiModifier.NATIVE), GroovyBundle.message("native.methods.cannot.have.body"));
        registerFix(annotation, new GrModifierFix((PsiMember) modifiersList.getParent(), PsiModifier.NATIVE, true, false, GrModifierFix.MODIFIER_LIST), modifiersList);
        annotation.registerFix(QuickFixFactory.getInstance().createDeleteMethodBodyFix(method));
    }
}
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 7 with GrModifierFix

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

the class GroovyAnnotator method checkAccessModifiers.

private static void checkAccessModifiers(AnnotationHolder holder, @NotNull GrModifierList modifierList, PsiMember member) {
    boolean hasPrivate = modifierList.hasExplicitModifier(PsiModifier.PRIVATE);
    boolean hasPublic = modifierList.hasExplicitModifier(PsiModifier.PUBLIC);
    boolean hasProtected = modifierList.hasExplicitModifier(PsiModifier.PROTECTED);
    if (hasPrivate && hasPublic || hasPrivate && hasProtected || hasPublic && hasProtected) {
        final Annotation annotation = holder.createErrorAnnotation(modifierList, GroovyBundle.message("illegal.combination.of.modifiers"));
        if (hasPrivate) {
            registerFix(annotation, new GrModifierFix(member, PsiModifier.PRIVATE, false, false, GrModifierFix.MODIFIER_LIST), modifierList);
        }
        if (hasProtected) {
            registerFix(annotation, new GrModifierFix(member, PsiModifier.PROTECTED, false, false, GrModifierFix.MODIFIER_LIST), modifierList);
        }
        if (hasPublic) {
            registerFix(annotation, new GrModifierFix(member, PsiModifier.PUBLIC, false, false, GrModifierFix.MODIFIER_LIST), modifierList);
        }
    } else if (member instanceof PsiClass && member.getContainingClass() == null && GroovyConfigUtils.getInstance().isVersionAtLeast(member, GroovyConfigUtils.GROOVY2_0)) {
        checkModifierIsNotAllowed(modifierList, PsiModifier.PRIVATE, GroovyBundle.message("top.level.class.maynot.have.private.modifier"), holder);
        checkModifierIsNotAllowed(modifierList, PsiModifier.PROTECTED, GroovyBundle.message("top.level.class.maynot.have.protected.modifier"), holder);
    }
}
Also used : GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix) Annotation(com.intellij.lang.annotation.Annotation)

Example 8 with GrModifierFix

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

the class GroovyAnnotator method registerImplementsMethodsFix.

private static void registerImplementsMethodsFix(@NotNull GrTypeDefinition typeDefinition, @NotNull PsiMethod abstractMethod, @NotNull Annotation annotation) {
    if (!OverrideImplementExploreUtil.getMethodsToOverrideImplement(typeDefinition, true).isEmpty()) {
        annotation.registerFix(QuickFixFactory.getInstance().createImplementMethodsFix(typeDefinition));
    }
    if (!JavaPsiFacade.getInstance(typeDefinition.getProject()).getResolveHelper().isAccessible(abstractMethod, typeDefinition, null)) {
        registerFix(annotation, new GrModifierFix(abstractMethod, PsiModifier.PUBLIC, true, true, GrModifierFix.MODIFIER_LIST_OWNER), abstractMethod);
        registerFix(annotation, new GrModifierFix(abstractMethod, PsiModifier.PROTECTED, true, true, GrModifierFix.MODIFIER_LIST_OWNER), abstractMethod);
    }
    if (!(typeDefinition instanceof GrAnnotationTypeDefinition) && typeDefinition.getModifierList() != null) {
        registerFix(annotation, new GrModifierFix(typeDefinition, PsiModifier.ABSTRACT, false, true, GrModifierFix.MODIFIER_LIST_OWNER), typeDefinition);
    }
}
Also used : GrModifierFix(org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)

Example 9 with GrModifierFix

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

the class GroovyAnnotator method checkTypeDefinitionModifiers.

private static void checkTypeDefinitionModifiers(AnnotationHolder holder, GrTypeDefinition typeDefinition) {
    GrModifierList modifiersList = typeDefinition.getModifierList();
    if (modifiersList == null)
        return;
    /**** class ****/
    checkAccessModifiers(holder, modifiersList, typeDefinition);
    checkDuplicateModifiers(holder, modifiersList, typeDefinition);
    PsiClassType[] extendsListTypes = typeDefinition.getExtendsListTypes();
    for (PsiClassType classType : extendsListTypes) {
        PsiClass psiClass = classType.resolve();
        if (psiClass != null && psiClass.hasModifierProperty(PsiModifier.FINAL)) {
            final Annotation annotation = holder.createErrorAnnotation(typeDefinition.getNameIdentifierGroovy(), GroovyBundle.message("final.class.cannot.be.extended"));
            registerFix(annotation, new GrModifierFix(typeDefinition, PsiModifier.FINAL, false, false, GrModifierFix.MODIFIER_LIST_OWNER), typeDefinition);
        }
    }
    if (!typeDefinition.isEnum()) {
        checkForAbstractAndFinalCombination(holder, typeDefinition, modifiersList);
    }
    checkModifierIsNotAllowed(modifiersList, PsiModifier.TRANSIENT, GroovyBundle.message("modifier.transient.not.allowed.here"), holder);
    checkModifierIsNotAllowed(modifiersList, PsiModifier.VOLATILE, GroovyBundle.message("modifier.volatile.not.allowed.here"), holder);
    /**** interface ****/
    if (typeDefinition.isInterface()) {
        checkModifierIsNotAllowed(modifiersList, PsiModifier.FINAL, GroovyBundle.message("intarface.cannot.have.modifier.final"), holder);
    }
    if (GroovyConfigUtils.getInstance().isVersionAtLeast(typeDefinition, GroovyConfigUtils.GROOVY1_8)) {
        if (typeDefinition.getContainingClass() == null && !(typeDefinition instanceof GrTraitTypeDefinition)) {
            checkModifierIsNotAllowed(modifiersList, PsiModifier.STATIC, 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)

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