Search in sources :

Example 1 with GrEnumConstant

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.

the class MoveGroovyMemberHandler method doMove.

@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
    GroovyChangeContextUtil.encodeContextInfo(member);
    final PsiDocComment docComment;
    if (member instanceof PsiDocCommentOwner) {
        docComment = ((PsiDocCommentOwner) member).getDocComment();
    } else {
        docComment = null;
    }
    PsiMember moved;
    if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
        final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
        moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
        member.delete();
    } else if (member instanceof GrEnumConstant) {
        moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
    } else if (member instanceof GrField) {
        if (anchor != null)
            anchor = anchor.getParent();
        final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
        GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
        int number = ArrayUtil.find(parent.getMembers(), member);
        final GrMember[] members = movedDeclaration.getMembers();
        for (int i = 0; i < number; i++) {
            members[i].delete();
        }
        for (int i = number + 1; i < members.length; i++) {
            members[i].delete();
        }
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = movedDeclaration.getModifierList();
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
        }
        moved = movedDeclaration.getMembers()[0];
    } else if (member instanceof GrMethod) {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = moved.getModifierList();
            assert list != null;
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
        }
    } else {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
    }
    if (docComment != null) {
        PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
        PsiElement prevSibling = insertedDocComment.getPrevSibling();
        addLineFeedIfNeeded(prevSibling);
        docComment.delete();
    }
    member.delete();
    return moved;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrEnumConstant

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.

the class GrEnumConstantInfo method getElementToHighlight.

@NotNull
@Override
public PsiElement getElementToHighlight() {
    GrEnumConstant constant = getCall();
    GrArgumentList argList = constant.getArgumentList();
    if (argList != null)
        return argList;
    return constant.getNameIdentifierGroovy();
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrEnumConstant

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.

the class GroovyConstructorUsagesSearcher method processConstructorUsages.

static void processConstructorUsages(final PsiMethod constructor, final SearchScope searchScope, final Processor<PsiReference> consumer, final SearchRequestCollector collector, final boolean includeOverloads) {
    if (!constructor.isConstructor())
        return;
    final PsiClass clazz = constructor.getContainingClass();
    if (clazz == null)
        return;
    SearchScope onlyGroovy = GroovyScopeUtil.restrictScopeToGroovyFiles(searchScope, GroovyScopeUtil.getEffectiveScope(constructor));
    Set<PsiClass> processed = collector.getSearchSession().getUserData(LITERALLY_CONSTRUCTED_CLASSES);
    if (processed == null) {
        collector.getSearchSession().putUserData(LITERALLY_CONSTRUCTED_CLASSES, processed = ContainerUtil.newConcurrentSet());
    }
    if (!processed.add(clazz))
        return;
    if (clazz.isEnum() && clazz instanceof GroovyPsiElement) {
        for (PsiField field : clazz.getFields()) {
            if (field instanceof GrEnumConstant) {
                final PsiReference ref = field.getReference();
                if (ref != null && ref.isReferenceTo(constructor)) {
                    if (!consumer.process(ref))
                        return;
                }
            }
        }
    }
    final LiteralConstructorSearcher literalProcessor = new LiteralConstructorSearcher(constructor, consumer, includeOverloads);
    final Processor<GrNewExpression> newExpressionProcessor = grNewExpression -> {
        final PsiMethod resolvedConstructor = grNewExpression.resolveMethod();
        if (includeOverloads || constructor.getManager().areElementsEquivalent(resolvedConstructor, constructor)) {
            return consumer.process(grNewExpression.getReferenceElement());
        }
        return true;
    };
    processGroovyClassUsages(clazz, searchScope, collector, newExpressionProcessor, literalProcessor);
    //this()
    if (clazz instanceof GrTypeDefinition) {
        if (!processConstructors(constructor, consumer, clazz, true)) {
            return;
        }
    }
    //super()
    DirectClassInheritorsSearch.search(clazz, onlyGroovy).forEach(new ReadActionProcessor<PsiClass>() {

        @Override
        public boolean processInReadAction(PsiClass inheritor) {
            if (inheritor instanceof GrTypeDefinition) {
                if (!processConstructors(constructor, consumer, inheritor, false))
                    return false;
            }
            return true;
        }
    });
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) PairProcessor(com.intellij.util.PairProcessor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) SearchRequestCollector(com.intellij.psi.search.SearchRequestCollector) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Key(com.intellij.openapi.util.Key) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) Set(java.util.Set) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TextRange(com.intellij.openapi.util.TextRange) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) Nullable(org.jetbrains.annotations.Nullable) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) Processor(com.intellij.util.Processor) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) SearchScope(com.intellij.psi.search.SearchScope)

Example 4 with GrEnumConstant

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.

the class ClassGenerator method writeMembers.

public void writeMembers(StringBuilder text, PsiClass typeDefinition) {
    if (typeDefinition instanceof GrEnumTypeDefinition) {
        final GrEnumConstant[] enumConstants = ((GrEnumTypeDefinition) typeDefinition).getEnumConstants();
        for (GrEnumConstant constant : enumConstants) {
            classItemGenerator.writeEnumConstant(text, constant);
            text.append(',');
        }
        if (enumConstants.length > 0) {
            //text.removeFromTheEnd(1).append(";\n");
            text.delete(text.length() - 1, text.length());
        }
        text.append(";\n");
    }
    writeAllMethods(text, classItemGenerator.collectMethods(typeDefinition), typeDefinition);
    if (typeDefinition instanceof GrTypeDefinition) {
        for (GrMembersDeclaration declaration : ((GrTypeDefinition) typeDefinition).getMemberDeclarations()) {
            if (declaration instanceof GrVariableDeclaration) {
                classItemGenerator.writeVariableDeclarations(text, (GrVariableDeclaration) declaration);
            }
        }
        for (PsiClass inner : typeDefinition.getInnerClasses()) {
            writeTypeDefinition(text, inner, false, false);
            text.append('\n');
        }
    }
    classItemGenerator.writePostponed(text, typeDefinition);
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMembersDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) PsiClass(com.intellij.psi.PsiClass)

Example 5 with GrEnumConstant

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.

the class GrFinalVariableAccessInspection method isFieldInitialized.

private static boolean isFieldInitialized(@NotNull GrField field) {
    if (field instanceof GrEnumConstant)
        return true;
    if (field.getInitializerGroovy() != null)
        return true;
    if (isImmutableField(field))
        return true;
    final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    final GrTypeDefinition aClass = ((GrTypeDefinition) field.getContainingClass());
    if (aClass == null)
        return true;
    GrClassInitializer[] initializers = aClass.getInitializers();
    for (GrClassInitializer initializer : initializers) {
        if (initializer.isStatic() != isStatic)
            continue;
        final GrOpenBlock block = initializer.getBlock();
        final Instruction[] initializerFlow = buildFlowForField(block);
        if (VariableInitializationChecker.isVariableDefinitelyInitializedCached(field, block, initializerFlow)) {
            return true;
        }
    }
    if (isStatic)
        return false;
    final GrMethod[] constructors = aClass.getCodeConstructors();
    if (constructors.length == 0)
        return false;
    Set<GrMethod> initializedConstructors = ContainerUtil.newHashSet();
    Set<GrMethod> notInitializedConstructors = ContainerUtil.newHashSet();
    NEXT_CONSTR: for (GrMethod constructor : constructors) {
        if (constructor.getBlock() == null)
            return false;
        final List<GrMethod> chained = getChainedConstructors(constructor);
        NEXT_CHAINED: for (GrMethod method : chained) {
            if (initializedConstructors.contains(method)) {
                continue NEXT_CONSTR;
            } else if (notInitializedConstructors.contains(method)) {
                continue NEXT_CHAINED;
            }
            final GrOpenBlock block = method.getBlock();
            assert block != null;
            final boolean initialized = VariableInitializationChecker.isVariableDefinitelyInitializedCached(field, block, buildFlowForField(block));
            if (initialized) {
                initializedConstructors.add(method);
                continue NEXT_CONSTR;
            } else {
                notInitializedConstructors.add(method);
            }
        }
        return false;
    }
    return true;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)

Aggregations

GrEnumConstant (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant)8 NotNull (org.jetbrains.annotations.NotNull)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)3 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 PsiClass (com.intellij.psi.PsiClass)2 Nullable (org.jetbrains.annotations.Nullable)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)2 QueryExecutorBase (com.intellij.openapi.application.QueryExecutorBase)1 ReadActionProcessor (com.intellij.openapi.application.ReadActionProcessor)1 Key (com.intellij.openapi.util.Key)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1 PsiElement (com.intellij.psi.PsiElement)1