Search in sources :

Example 26 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GrHighlightUtil method getClassHeaderTextRange.

public static TextRange getClassHeaderTextRange(GrTypeDefinition clazz) {
    final GrModifierList modifierList = clazz.getModifierList();
    final int startOffset = modifierList != null ? modifierList.getTextOffset() : clazz.getTextOffset();
    final GrImplementsClause implementsClause = clazz.getImplementsClause();
    final int endOffset;
    if (implementsClause != null) {
        endOffset = implementsClause.getTextRange().getEndOffset();
    } else {
        final GrExtendsClause extendsClause = clazz.getExtendsClause();
        if (extendsClause != null) {
            endOffset = extendsClause.getTextRange().getEndOffset();
        } else {
            endOffset = clazz.getNameIdentifierGroovy().getTextRange().getEndOffset();
        }
    }
    return new TextRange(startOffset, endOffset);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrExtendsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrExtendsClause) GrImplementsClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplementsClause) TextRange(com.intellij.openapi.util.TextRange)

Example 27 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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 28 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GroovyAnnotator method checkGetterOfImmutable.

private static void checkGetterOfImmutable(AnnotationHolder holder, GrMethod method) {
    if (!GroovyPropertyUtils.isSimplePropertyGetter(method))
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    PsiModifierList aClassModifierList = aClass.getModifierList();
    if (aClassModifierList == null)
        return;
    if (!PsiImplUtil.hasImmutableAnnotation(aClassModifierList))
        return;
    PsiField field = GroovyPropertyUtils.findFieldForAccessor(method, false);
    if (field == null || !(field instanceof GrField))
        return;
    GrModifierList fieldModifierList = ((GrField) field).getModifierList();
    if (fieldModifierList == null)
        return;
    if (fieldModifierList.hasExplicitVisibilityModifiers())
        return;
    holder.createErrorAnnotation(method.getNameIdentifierGroovy(), GroovyBundle.message("repetitive.method.name.0", method.getName()));
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)

Example 29 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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)

Example 30 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GroovyAnnotator method visitForInClause.

@Override
public void visitForInClause(@NotNull GrForInClause forInClause) {
    final GrVariable var = forInClause.getDeclaredVariable();
    if (var == null)
        return;
    final GrModifierList modifierList = var.getModifierList();
    if (modifierList == null)
        return;
    final PsiElement[] modifiers = modifierList.getModifiers();
    for (PsiElement modifier : modifiers) {
        if (modifier instanceof PsiAnnotation)
            continue;
        final String modifierText = modifier.getText();
        if (PsiModifier.FINAL.equals(modifierText))
            continue;
        if (GrModifier.DEF.equals(modifierText))
            continue;
        myHolder.createErrorAnnotation(modifier, GroovyBundle.message("not.allowed.modifier.in.forin", modifierText));
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)

Aggregations

GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)39 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)9 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)5 Annotation (com.intellij.lang.annotation.Annotation)4 PsiElement (com.intellij.psi.PsiElement)4 TextRange (com.intellij.openapi.util.TextRange)3 NotNull (org.jetbrains.annotations.NotNull)3 GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 Matcher (java.util.regex.Matcher)2 Nullable (org.jetbrains.annotations.Nullable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)2 LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)1