Search in sources :

Example 91 with Statement

use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.

the class SwitchRefactoring method addCaseWithStmts.

private void addCaseWithStmts(final SwitchStatement switchStmt, final List<Expression> caseValues, final List<Statement> innerStmts) {
    final ASTBuilder b = ctx.getASTBuilder();
    final List<Statement> switchStmts = statements(switchStmt);
    // Add the case statement(s)
    if (caseValues != null) {
        for (final Expression caseValue : caseValues) {
            switchStmts.add(b.case0(b.move(caseValue)));
        }
    } else {
        switchStmts.add(b.default0());
    }
    // Add the statement(s) for this case(s)
    boolean isBreakNeeded = true;
    if (!innerStmts.isEmpty()) {
        for (final Statement stmt : innerStmts) {
            switchStmts.add(b.move(stmt));
        }
        isBreakNeeded = !isEndingWithJump(getLast(innerStmts));
    }
    // When required: end with a break;
    if (isBreakNeeded) {
        switchStmts.add(b.break0());
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 92 with Statement

use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.

the class CFGBuilder method buildCFG.

/**
 * Builds a CFG for the passed in statement.
 *
 * @param node the statement for which to build a CFG
 * @param state the liveness state before the current statement.
 *          It contains: the live edges before the current statement,
 *          the live basic block to which the current statement might be added.
 *          If null, then the new basic block must be created for the current statement.
 * @param throwers the thrower blocks information
 * @return the new live state after the current statement
 */
public LivenessState buildCFG(IfStatement node, LivenessState state, ThrowerBlocks throwers) {
    final CFGBasicBlock exprBlock = getCFGBasicBlock(node, state.nextStmtWillCreateNewBlock(), true);
    try {
        addVariableAccess(exprBlock, node.getExpression(), READ, throwers);
        final LivenessState result = new LivenessState();
        CFGEdgeBuilder thenEdge = new CFGEdgeBuilder(node.getExpression(), true, exprBlock);
        result.addAll(buildCFG(node.getThenStatement(), LivenessState.of(thenEdge), throwers));
        final Statement elseStmt = node.getElseStatement();
        CFGEdgeBuilder elseEdge = new CFGEdgeBuilder(node.getExpression(), false, exprBlock);
        if (elseStmt != null) {
            result.addAll(buildCFG(elseStmt, LivenessState.of(elseEdge), throwers));
        } else {
            result.add(elseEdge);
        }
        return result.nextStmtWillCreateNewBlock();
    } finally {
        moveAllEdgesToBuild(node, state);
    }
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Example 93 with Statement

use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.

the class CFGBuilder method buildCFG.

/**
 * Builds a CFG for the provided node.
 *
 * @param node the node for which to build a CFG.
 * @param state the blocks liveness state before current node
 * @param throwers the thrower blocks information
 * @return the blocks liveness state after current node
 */
public LivenessState buildCFG(ContinueStatement node, LivenessState state, ThrowerBlocks throwers) {
    final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
    final Statement targetStmt;
    if (node.getLabel() != null) {
        targetStmt = findLabeledParentStmt(node);
    } else {
        targetStmt = findContinuableParentStmt(node);
    }
    addEdgeToBuild(targetStmt, new CFGEdgeBuilder(basicBlock), false);
    return state.copyLiveBasicBlock();
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Example 94 with Statement

use of org.eclipse.jdt.core.dom.Statement in project AutoRefactor by JnRouvignac.

the class CFGBuilder method buildCFG.

/**
 * Builds a CFG for the provided node.
 *
 * @param node the node for which to build a CFG.
 * @param state the blocks liveness state before current node
 * @param throwers the thrower blocks information
 * @return the blocks liveness state after current node
 */
public LivenessState buildCFG(BreakStatement node, LivenessState state, ThrowerBlocks throwers) {
    final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
    final Statement targetStmt;
    if (node.getLabel() != null) {
        targetStmt = findLabeledParentStmt(node);
    } else {
        targetStmt = findBreakableParentStmt(node);
    }
    addEdgeToBuild(targetStmt, new CFGEdgeBuilder(basicBlock), true);
    return state.copyLiveBasicBlock();
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Example 95 with Statement

use of org.eclipse.jdt.core.dom.Statement in project bayou by capergroup.

the class DOMSwitchStatement method handle.

@Override
public DSubTree handle() {
    DSubTree Sexpr = new DOMExpression(statement.getExpression(), visitor).handle();
    boolean branch = Sexpr.isValid();
    for (Object o : statement.statements()) {
        int type = ((Statement) o).getNodeType();
        nodeType.add(type);
        DSubTree body = new DOMStatement((Statement) o, visitor).handle();
        bodies.add(body.getNodes());
        if (// excludes 'case' statement
        type != 49)
            branch |= body.isValid();
    }
    if (branch) {
        DSubTree switchNode = BuildTree(Sexpr, 1);
        tree.addNode(switchNode.getNodes().get(0));
    } else {
        // only one  will add nodes, the rest will add nothing
        tree.addNodes(Sexpr.getNodes());
        for (Iterator<Object> iter = statement.statements().iterator(); iter.hasNext(); ) {
            Object o = iter.next();
            DSubTree body = new DOMStatement((Statement) o, visitor).handle();
            tree.addNodes(body.getNodes());
        }
    }
    return tree;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)95 ForStatement (org.eclipse.jdt.core.dom.ForStatement)59 IfStatement (org.eclipse.jdt.core.dom.IfStatement)58 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)56 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)51 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)49 DoStatement (org.eclipse.jdt.core.dom.DoStatement)46 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)46 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)45 Block (org.eclipse.jdt.core.dom.Block)42 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)39 Expression (org.eclipse.jdt.core.dom.Expression)38 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)30 AST (org.eclipse.jdt.core.dom.AST)26 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)26 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)25 TryStatement (org.eclipse.jdt.core.dom.TryStatement)23 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)22