Search in sources :

Example 1 with BranchingInstruction

use of com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction in project intellij-community by JetBrains.

the class ExtractMethodProcessor method isNullInferred.

private boolean isNullInferred(String exprText, boolean trueSet) {
    final PsiCodeBlock block = myElementFactory.createCodeBlockFromText("{}", myElements[0]);
    for (PsiElement element : myElements) {
        block.add(element);
    }
    final PsiIfStatement statementFromText = (PsiIfStatement) myElementFactory.createStatementFromText("if (" + exprText + " == null);", null);
    block.add(statementFromText);
    final StandardDataFlowRunner dfaRunner = new StandardDataFlowRunner();
    final StandardInstructionVisitor visitor = new StandardInstructionVisitor();
    final RunnerResult rc = dfaRunner.analyzeMethod(block, visitor);
    if (rc == RunnerResult.OK) {
        final Pair<Set<Instruction>, Set<Instruction>> expressions = dfaRunner.getConstConditionalExpressions();
        final Set<Instruction> set = trueSet ? expressions.getFirst() : expressions.getSecond();
        for (Instruction instruction : set) {
            if (instruction instanceof BranchingInstruction) {
                if (((BranchingInstruction) instruction).getPsiAnchor().getText().equals(statementFromText.getCondition().getText())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : BranchingInstruction(com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction) BranchingInstruction(com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction) Instruction(com.intellij.codeInspection.dataFlow.instructions.Instruction)

Aggregations

BranchingInstruction (com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction)1 Instruction (com.intellij.codeInspection.dataFlow.instructions.Instruction)1