Search in sources :

Example 1 with IfEndInstruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction in project intellij-community by JetBrains.

the class GrRedundantElseIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (!(element.getNode().getElementType() == GroovyTokenTypes.kELSE))
                return false;
            final PsiElement parent = element.getParent();
            if (!(parent instanceof GrIfStatement))
                return false;
            final GrIfStatement ifStatement = (GrIfStatement) parent;
            final GrStatement branch = ifStatement.getThenBranch();
            final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
            if (flowOwner == null)
                return false;
            final Instruction[] flow = flowOwner.getControlFlow();
            for (Instruction instruction : flow) {
                if (instruction instanceof IfEndInstruction && instruction.getElement() == ifStatement) {
                    for (Instruction pred : instruction.allPredecessors()) {
                        final PsiElement predElement = pred.getElement();
                        if (predElement != null && PsiTreeUtil.isAncestor(branch, predElement, false)) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    };
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IfEndInstruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction in project intellij-community by JetBrains.

the class InvertIfIntention method isTailAfterIf.

private static boolean isTailAfterIf(@NotNull GrIfStatement ifStatement, @NotNull GrStatementOwner owner) {
    final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
    if (flowOwner == null)
        return false;
    final Instruction[] flow = flowOwner.getControlFlow();
    final GrStatement[] statements = owner.getStatements();
    final int index = ArrayUtilRt.find(statements, ifStatement);
    if (index == statements.length - 1)
        return false;
    final GrStatement then = ifStatement.getThenBranch();
    for (Instruction i : flow) {
        final PsiElement element = i.getElement();
        if (element == null || !PsiTreeUtil.isAncestor(then, element, true))
            continue;
        for (Instruction succ : i.allSuccessors()) {
            if (succ instanceof IfEndInstruction) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)

Example 3 with IfEndInstruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction in project intellij-community by JetBrains.

the class ControlFlowUtils method visitAllExitPointsInner.

private static boolean visitAllExitPointsInner(Instruction last, Instruction first, boolean[] visited, ExitPointVisitor visitor) {
    if (first == last)
        return true;
    if (last instanceof AfterCallInstruction) {
        visited[last.num()] = true;
        return visitAllExitPointsInner(((AfterCallInstruction) last).myCall, first, visited, visitor);
    }
    if (last instanceof MaybeReturnInstruction) {
        return visitor.visitExitPoint(last, (GrExpression) last.getElement());
    } else if (last instanceof IfEndInstruction) {
        visited[last.num()] = true;
        for (Instruction instruction : last.allPredecessors()) {
            if (!visitAllExitPointsInner(instruction, first, visited, visitor))
                return false;
        }
        return true;
    } else if (last instanceof ThrowingInstruction) {
        PsiElement element = last.getElement();
        if (!(element instanceof GrThrowStatement || element instanceof GrAssertStatement))
            return true;
    }
    PsiElement element = last.getElement();
    if (element != null) {
        final GrExpression returnValue;
        if (element instanceof GrReturnStatement) {
            returnValue = ((GrReturnStatement) element).getReturnValue();
        } else if (element instanceof GrExpression && PsiUtil.isExpressionStatement(element)) {
            returnValue = (GrExpression) element;
        } else {
            returnValue = null;
        }
        return visitor.visitExitPoint(last, returnValue);
    }
    visited[last.num()] = true;
    for (Instruction pred : last.allPredecessors()) {
        if (!visited[pred.num()]) {
            if (!visitAllExitPointsInner(pred, first, visited, visitor))
                return false;
        }
    }
    return true;
}
Also used : MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)3 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)3 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 NotNull (org.jetbrains.annotations.NotNull)1 PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 AfterCallInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction)1 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)1 MaybeReturnInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction)1 ThrowingInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction)1