Search in sources :

Example 1 with Instruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction 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 Instruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction 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 Instruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction 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 Instruction

use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction 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 Instruction

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

the class GrIntroduceClosureParameterProcessor method findUsagesForLocal.

private static Collection<PsiReference> findUsagesForLocal(GrClosableBlock initializer, final GrVariable var) {
    final Instruction[] flow = ControlFlowUtils.findControlFlowOwner(initializer).getControlFlow();
    final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, var);
    Instruction writeInstr = null;
    final PsiElement parent = initializer.getParent();
    if (parent instanceof GrVariable) {
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == var);
    } else if (parent instanceof GrAssignmentExpression) {
        final GrReferenceExpression refExpr = (GrReferenceExpression) ((GrAssignmentExpression) parent).getLValue();
        final Instruction instruction = ContainerUtil.find(flow, instruction1 -> instruction1.getElement() == refExpr);
        LOG.assertTrue(instruction != null);
        final BitSet prev = writes.get(instruction.num());
        if (prev.cardinality() == 1) {
            writeInstr = flow[prev.nextSetBit(0)];
        }
    }
    LOG.assertTrue(writeInstr != null);
    Collection<PsiReference> result = new ArrayList<>();
    for (Instruction instruction : flow) {
        if (!(instruction instanceof ReadWriteVariableInstruction))
            continue;
        if (((ReadWriteVariableInstruction) instruction).isWrite())
            continue;
        final PsiElement element = instruction.getElement();
        if (element instanceof GrVariable && element != var)
            continue;
        if (!(element instanceof GrReferenceExpression))
            continue;
        final GrReferenceExpression ref = (GrReferenceExpression) element;
        if (ref.isQualified() || ref.resolve() != var)
            continue;
        final BitSet prev = writes.get(instruction.num());
        if (prev.cardinality() == 1 && prev.get(writeInstr.num())) {
            result.add(ref);
        }
    }
    return result;
}
Also used : IntroduceParameterRefactoring(com.intellij.refactoring.IntroduceParameterRefactoring) PsiImplUtil(org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) Nullable(org.jetbrains.annotations.Nullable) org.jetbrains.plugins.groovy.lang.psi.api.statements(org.jetbrains.plugins.groovy.lang.psi.api.statements) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) List(java.util.List) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) DescriptiveNameUtil(com.intellij.lang.findUsages.DescriptiveNameUtil) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ArrayUtil(com.intellij.util.ArrayUtil) PsiUtilBase(com.intellij.psi.util.PsiUtilBase) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) ChangeContextUtil(com.intellij.codeInsight.ChangeContextUtil) UsageViewDescriptorAdapter(com.intellij.refactoring.ui.UsageViewDescriptorAdapter) UsageInfo(com.intellij.usageView.UsageInfo) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) ArrayList(java.util.ArrayList) OldReferencesResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver) GroovyRefactoringUtil(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) Editor(com.intellij.openapi.editor.Editor) UsageViewUtil(com.intellij.usageView.UsageViewUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) UsageViewDescriptor(com.intellij.usageView.UsageViewDescriptor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) ExpressionConverter(com.intellij.psi.impl.ExpressionConverter) TIntProcedure(gnu.trove.TIntProcedure) org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions) BitSet(java.util.BitSet) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)

Aggregations

Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)27 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)18 PsiElement (com.intellij.psi.PsiElement)10 Nullable (org.jetbrains.annotations.Nullable)8 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)7 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)6 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 AfterCallInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction)5 MaybeReturnInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction)5 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 ThrowingInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction)4 BitSet (java.util.BitSet)3 ControlFlowUtils (org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3