Search in sources :

Example 1 with GrControlFlowOwner

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

the class GrHighlightExitPointHandler method computeUsages.

@Override
public void computeUsages(List<PsiElement> targets) {
    PsiElement parent = myTarget.getParent();
    if (!(parent instanceof GrReturnStatement) && !(parent instanceof GrThrowStatement))
        return;
    final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(parent);
    ControlFlowUtils.visitAllExitPoints(flowOwner, new ControlFlowUtils.ExitPointVisitor() {

        @Override
        public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
            final PsiElement returnElement = instruction.getElement();
            if (returnElement != null && isCorrectReturn(returnElement)) {
                final TextRange range = returnElement.getTextRange();
                myReadUsages.add(range);
            }
            return true;
        }
    });
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Example 2 with GrControlFlowOwner

use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner 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 3 with GrControlFlowOwner

use of org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner 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 4 with GrControlFlowOwner

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

the class GroovyInlineLocalHandler method createSettings.

/**
   * Returns Settings object for referenced definition in case of local variable
   */
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
    final String localName = variable.getName();
    final Project project = variable.getProject();
    GrExpression initializer = null;
    Instruction writeInstr = null;
    Instruction[] flow = null;
    //search for initializer to inline
    if (invokedOnReference) {
        LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
        final PsiReference ref = TargetElementUtil.findReference(editor);
        LOG.assertTrue(ref != null);
        PsiElement cur = ref.getElement();
        if (cur instanceof GrReferenceExpression) {
            GrControlFlowOwner controlFlowOwner;
            do {
                controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
                if (controlFlowOwner == null)
                    break;
                flow = controlFlowOwner.getControlFlow();
                final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
                final PsiElement finalCur = cur;
                Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
                LOG.assertTrue(instruction != null);
                final BitSet prev = writes.get(instruction.num());
                if (prev.cardinality() == 1) {
                    writeInstr = flow[prev.nextSetBit(0)];
                    final PsiElement element = writeInstr.getElement();
                    if (element instanceof GrVariable) {
                        initializer = ((GrVariable) element).getInitializerGroovy();
                    } else if (element instanceof GrReferenceExpression) {
                        initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
                    }
                }
                PsiElement old_cur = cur;
                if (controlFlowOwner instanceof GrClosableBlock) {
                    cur = controlFlowOwner;
                } else {
                    PsiElement parent = controlFlowOwner.getParent();
                    if (parent instanceof GrMember)
                        cur = ((GrMember) parent).getContainingClass();
                }
                if (cur == old_cur)
                    break;
            } while (initializer == null);
        }
    } else {
        flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
        initializer = variable.getInitializerGroovy();
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
    }
    if (initializer == null || writeInstr == null) {
        String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
        CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
        return null;
    }
    int writeInstructionNumber = writeInstr.num();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
    RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
    if (dialog.showAndGet()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Logger(com.intellij.openapi.diagnostic.Logger) HelpID(com.intellij.refactoring.HelpID) InlineActionHandler(com.intellij.lang.refactoring.InlineActionHandler) PsiReference(com.intellij.psi.PsiReference) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) BitSet(java.util.BitSet) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) BitSet(java.util.BitSet) PsiReference(com.intellij.psi.PsiReference) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GrControlFlowOwner

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

the class ExpressionGenerator method isImplicitlyCastedToArray.

private static boolean isImplicitlyCastedToArray(GrListOrMap list) {
    PsiElement parent = list.getParent();
    GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(list);
    if (!(owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod))
        return false;
    if (!(parent instanceof GrReturnStatement || ControlFlowUtils.isReturnValue(list, owner)))
        return false;
    PsiType type = ((GrMethod) owner.getParent()).getReturnType();
    return type instanceof PsiArrayType;
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)15 PsiElement (com.intellij.psi.PsiElement)7 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)7 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)3 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 ArrayList (java.util.ArrayList)2 ControlFlowUtils (org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2