Search in sources :

Example 31 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GrReassignedLocalVarsChecker method getUsedVarsInsideBlock.

@NotNull
private static Set<String> getUsedVarsInsideBlock(@NotNull final GrCodeBlock block) {
    return CachedValuesManager.getCachedValue(block, () -> {
        final Set<String> result = ContainerUtil.newHashSet();
        block.acceptChildren(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitOpenBlock(@NotNull GrOpenBlock openBlock) {
                result.addAll(getUsedVarsInsideBlock(openBlock));
            }

            @Override
            public void visitClosure(@NotNull GrClosableBlock closure) {
                result.addAll(getUsedVarsInsideBlock(closure));
            }

            @Override
            public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
                if (referenceExpression.getQualifier() == null && referenceExpression.getReferenceName() != null) {
                    result.add(referenceExpression.getReferenceName());
                }
            }
        });
        return CachedValueProvider.Result.create(result, block);
    });
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class PsiImplUtil method inferReturnType.

@Nullable
public static PsiType inferReturnType(PsiElement position) {
    final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(position);
    if (flowOwner == null)
        return null;
    final PsiElement parent = flowOwner.getContext();
    if (flowOwner instanceof GrOpenBlock && parent instanceof GrMethod) {
        final GrMethod method = (GrMethod) parent;
        if (method.isConstructor())
            return null;
        return method.getReturnType();
    }
    return null;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class ControlFlowBuilder method visitTryStatement.

@Override
public void visitTryStatement(@NotNull GrTryCatchStatement tryCatchStatement) {
    final GrOpenBlock tryBlock = tryCatchStatement.getTryBlock();
    final GrCatchClause[] catchClauses = tryCatchStatement.getCatchClauses();
    final GrFinallyClause finallyClause = tryCatchStatement.getFinallyClause();
    for (int i = catchClauses.length - 1; i >= 0; i--) {
        myCaughtExceptionInfos.push(new ExceptionInfo(catchClauses[i]));
    }
    if (finallyClause != null)
        myFinallyCount++;
    List<Pair<InstructionImpl, GroovyPsiElement>> oldPending = null;
    if (finallyClause != null) {
        oldPending = myPending;
        myPending = new ArrayList<>();
    }
    InstructionImpl tryBegin = startNode(tryBlock);
    tryBlock.accept(this);
    InstructionImpl tryEnd = myHead;
    finishNode(tryBegin);
    Set<Pair<InstructionImpl, GroovyPsiElement>> pendingAfterTry = new LinkedHashSet<>(myPending);
    @SuppressWarnings("unchecked") List<InstructionImpl>[] throwers = new List[catchClauses.length];
    for (int i = 0; i < catchClauses.length; i++) {
        throwers[i] = myCaughtExceptionInfos.pop().myThrowers;
    }
    InstructionImpl[] catches = new InstructionImpl[catchClauses.length];
    for (int i = 0; i < catchClauses.length; i++) {
        interruptFlow();
        final InstructionImpl catchBeg = startNode(catchClauses[i]);
        for (InstructionImpl thrower : throwers[i]) {
            addEdge(thrower, catchBeg);
        }
        final GrParameter parameter = catchClauses[i].getParameter();
        if (parameter != null && myPolicy.isVariableInitialized(parameter)) {
            addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
        }
        catchClauses[i].accept(this);
        catches[i] = myHead;
        finishNode(catchBeg);
    }
    pendingAfterTry.addAll(myPending);
    myPending = new ArrayList<>(pendingAfterTry);
    if (finallyClause != null) {
        myFinallyCount--;
        interruptFlow();
        final InstructionImpl finallyInstruction = startNode(finallyClause, false);
        Set<AfterCallInstruction> postCalls = new LinkedHashSet<>();
        final List<Pair<InstructionImpl, GroovyPsiElement>> copy = myPending;
        myPending = new ArrayList<>();
        for (Pair<InstructionImpl, GroovyPsiElement> pair : copy) {
            postCalls.add(addCallNode(finallyInstruction, pair.getSecond(), pair.getFirst()));
        }
        if (tryEnd != null) {
            postCalls.add(addCallNode(finallyInstruction, tryCatchStatement, tryEnd));
        }
        for (InstructionImpl catchEnd : catches) {
            if (catchEnd != null) {
                postCalls.add(addCallNode(finallyInstruction, tryCatchStatement, catchEnd));
            }
        }
        //save added postcalls into separate list because we don't want returnInstruction grabbed their pending edges
        List<Pair<InstructionImpl, GroovyPsiElement>> pendingPostCalls = myPending;
        myPending = new ArrayList<>();
        myHead = finallyInstruction;
        finallyClause.accept(this);
        final ReturnInstruction returnInstruction = new ReturnInstruction(finallyClause);
        for (AfterCallInstruction postCall : postCalls) {
            postCall.setReturnInstruction(returnInstruction);
            addEdge(returnInstruction, postCall);
        }
        addNodeAndCheckPending(returnInstruction);
        interruptFlow();
        finishNode(finallyInstruction);
        if (oldPending == null) {
            error();
        }
        oldPending.addAll(pendingPostCalls);
        myPending = oldPending;
    } else {
        if (tryEnd != null) {
            addPendingEdge(tryCatchStatement, tryEnd);
        }
        for (InstructionImpl catchEnd : catches) {
            addPendingEdge(tryBlock, catchEnd);
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) FList(com.intellij.util.containers.FList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Pair(com.intellij.openapi.util.Pair)

Example 34 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class ControlFlowBuilder method collectUsedVariableWithoutInitialization.

private static Set<ReadWriteVariableInstruction> collectUsedVariableWithoutInitialization(GrTypeDefinition typeDefinition) {
    final Set<ReadWriteVariableInstruction> vars = ContainerUtil.newLinkedHashSet();
    typeDefinition.acceptChildren(new GroovyRecursiveElementVisitor() {

        private void collectVars(Instruction[] flow) {
            ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, false);
            Collections.addAll(vars, reads);
        }

        @Override
        public void visitField(@NotNull GrField field) {
            GrExpression initializer = field.getInitializerGroovy();
            if (initializer != null) {
                Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
                collectVars(flow);
            }
        }

        @Override
        public void visitMethod(@NotNull GrMethod method) {
            GrOpenBlock block = method.getBlock();
            if (block != null) {
                collectVars(block.getControlFlow());
            }
        }

        @Override
        public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
            GrOpenBlock block = initializer.getBlock();
            collectVars(block.getControlFlow());
        }
    });
    return vars;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 35 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class PsiUtil method getConstructorInvocation.

@Nullable
public static GrConstructorInvocation getConstructorInvocation(@NotNull GrMethod constructor) {
    assert constructor.isConstructor();
    final GrOpenBlock body = constructor.getBlock();
    if (body == null)
        return null;
    final GrStatement[] statements = body.getStatements();
    if (statements.length > 0 && statements[0] instanceof GrConstructorInvocation) {
        return ((GrConstructorInvocation) statements[0]);
    } else {
        return null;
    }
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)68 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)24 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)21 PsiElement (com.intellij.psi.PsiElement)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)5 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)5 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)5 TextRange (com.intellij.openapi.util.TextRange)4 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 UsageInfo (com.intellij.usageView.UsageInfo)3