Example usage for org.eclipse.jdt.core.dom IfStatement setElseStatement

List of usage examples for org.eclipse.jdt.core.dom IfStatement setElseStatement

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom IfStatement setElseStatement.

Prototype

public void setElseStatement(Statement statement) 

Source Link

Document

Sets or clears the "else" part of this if statement.

Usage

From source file:com.motorola.studio.android.generatecode.AbstractCodeGenerator.java

License:Apache License

/**
 * Creates a chain og else if and else statement for the given if statement
 * @param ifSt/*from   w  ww . j  a  v a  2 s  . c  o  m*/
 * @param invocation
 * @param guiQN
 */
protected void createElseIfAndElseStatements(IfStatement ifSt, MethodInvocation invocation,
        QualifiedName guiQN) {
    InfixExpression infixExp = typeDeclaration.getAST().newInfixExpression();
    infixExp.setOperator(InfixExpression.Operator.EQUALS);

    infixExp.setLeftOperand(invocation);
    infixExp.setRightOperand(guiQN);

    //first verifies if the expression of the if statement is missing, it means we created it, just need to add the expression.
    //Otherwise, the "else if" chain must be verified before add the new if statement
    if (ifSt.getExpression().toString().equals(JavaViewBasedOnLayoutModifierConstants.EXPRESSION_MISSING)) {
        ifSt.setExpression(infixExp);
    } else {
        boolean expressionAlreadyExists = false;
        //verifies if the first if's expression already verifies the current menu item or radio button
        if (ifChainContainsExpression(ifSt, infixExp)) {
            expressionAlreadyExists = true;
        }

        if (!expressionAlreadyExists) {
            IfStatement lastIfStatement = getLastIfStatementInChain(ifSt);
            if (lastIfStatement != null) {
                IfStatement elseSt = typeDeclaration.getAST().newIfStatement();
                elseSt.setExpression(infixExp);
                if (lastIfStatement.getElseStatement() != null) {
                    Statement oldElseStatement = lastIfStatement.getElseStatement();
                    elseSt.setElseStatement((Statement) ASTNode.copySubtree(elseSt.getAST(), oldElseStatement));
                    lastIfStatement.setElseStatement(elseSt);
                } else {
                    lastIfStatement.setElseStatement(elseSt);
                }
            }
        }
    }
}

From source file:com.motorola.studio.android.generatemenucode.model.codegenerators.MenuHandlerCodeGenerator.java

License:Apache License

/**
 * @param ifSt/* w ww .ja  va  2 s.co  m*/
 * @param methodDeclaration
 */
private void addingElseExpression(IfStatement ifSt, MethodDeclaration methodDeclaration) {
    Block block = typeDeclaration.getAST().newBlock();
    ReturnStatement returnStatement = typeDeclaration.getAST().newReturnStatement();
    List<String> arguments = new ArrayList<String>();
    arguments.add(CodeGeneratorBasedOnMenuConstants.ITEM); //$NON-NLS-1$
    SuperMethodInvocation superMethodInvocation = createSuperMethodInvocation(
            CodeGeneratorBasedOnMenuConstants.ON_OPTIONS_ITEM_SELECTED, arguments); //$NON-NLS-1$
    returnStatement.setExpression(superMethodInvocation);
    addStatementIfNotFound(block, returnStatement, false);
    ifSt.setElseStatement(block);
}

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.IfStatement node) {
    IfStatement element = (IfStatement) this.binding.get(node);
    this.initializeNode(element, node);

    if (this.binding.get(node.getExpression()) != null)
        element.setExpression((Expression) this.binding.get(node.getExpression()));
    if (this.binding.get(node.getThenStatement()) != null)
        element.setThenStatement((Statement) this.binding.get(node.getThenStatement()));
    if (this.binding.get(node.getElseStatement()) != null)
        element.setElseStatement((Statement) this.binding.get(node.getElseStatement()));
}

From source file:net.sf.eclipsecs.ui.quickfixes.blocks.NeedBracesQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}//from w  w  w .jav  a 2 s .  co  m
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {

    return new ASTVisitor() {
        public boolean visit(IfStatement node) {

            int nodePos = node.getStartPosition();
            int nodeEnd = nodePos + node.getLength();
            if ((nodePos >= lineInfo.getOffset() && nodePos <= (lineInfo.getOffset() + lineInfo.getLength()))
                    || (nodePos <= lineInfo.getOffset()
                            && nodeEnd >= lineInfo.getOffset() + lineInfo.getLength())) {
                bracifyIfStatement(node);
            }

            return true;
        }

        /**
         * Helper method to recursivly bracify a if-statement.
         * 
         * @param ifStatement the if statement
         */
        private void bracifyIfStatement(IfStatement ifStatement) {

            // change the then statement to a block if necessary
            if (!(ifStatement.getThenStatement() instanceof Block)) {
                if (ifStatement.getThenStatement() instanceof IfStatement) {
                    bracifyIfStatement((IfStatement) ifStatement.getThenStatement());
                }
                Block block = createBracifiedCopy(ifStatement.getAST(), ifStatement.getThenStatement());
                ifStatement.setThenStatement(block);
            }

            // check the else statement if it is a block
            Statement elseStatement = ifStatement.getElseStatement();
            if (elseStatement != null && !(elseStatement instanceof Block)) {

                // in case the else statement is an further if statement
                // (else if)
                // do the recursion
                if (elseStatement instanceof IfStatement) {
                    bracifyIfStatement((IfStatement) elseStatement);
                } else {
                    // change the else statement to a block
                    // Block block = ifStatement.getAST().newBlock();
                    // block.statements().add(ASTNode.copySubtree(block.getAST(),
                    // elseStatement));
                    Block block = createBracifiedCopy(ifStatement.getAST(), ifStatement.getElseStatement());
                    ifStatement.setElseStatement(block);
                }
            }
        }

        public boolean visit(ForStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                Block block = createBracifiedCopy(node.getAST(), node.getBody());
                node.setBody(block);
            }

            return true;
        }

        public boolean visit(DoStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                Block block = createBracifiedCopy(node.getAST(), node.getBody());
                node.setBody(block);
            }

            return true;
        }

        public boolean visit(WhileStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                Block block = createBracifiedCopy(node.getAST(), node.getBody());
                node.setBody(block);
            }

            return true;
        }

        private Block createBracifiedCopy(AST ast, Statement body) {
            Block block = ast.newBlock();
            block.statements().add(ASTNode.copySubtree(block.getAST(), body));
            return block;
        }
    };
}

From source file:org.asup.dk.compiler.rpj.writer.JDTStatementWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//www  . j av a 2s .  co m
public boolean visit(QIf statement) {

    Block block = blocks.peek();

    IfStatement ifSt = ast.newIfStatement();
    QPredicateExpression condition = expressionParser.parsePredicate(statement.getCondition());

    Expression expression = null;

    if (CompilationContextHelper.isPrimitive(compilationUnit, condition))
        expression = buildExpression(ast, condition, Boolean.class);
    else {
        expression = buildExpression(ast, condition, Boolean.class);
    }
    // expression = buildExpression(ast, condition, null);

    ifSt.setExpression(expression);

    block.statements().add(ifSt);

    // then
    if (statement.getThen() != null) {
        Block thenBlock = null;
        if (ifSt.getThenStatement() instanceof Block)
            thenBlock = (Block) ifSt.getThenStatement();
        else {
            thenBlock = ast.newBlock();
            ifSt.setThenStatement(thenBlock);
        }

        blocks.push(thenBlock);
        statement.getThen().accept(this);
        blocks.pop();
    }

    // else
    if (statement.getElse() != null) {
        Block elseBlock = null;
        if (ifSt.getElseStatement() instanceof Block)
            elseBlock = (Block) ifSt.getElseStatement();
        else {
            elseBlock = ast.newBlock();
            ifSt.setElseStatement(elseBlock);
        }

        // walk else
        blocks.push(elseBlock);
        statement.getElse().accept(this);
        blocks.pop();
    }

    // interrupt navigation
    return false;
}

From source file:org.autorefactor.refactoring.ASTBuilder.java

License:Open Source License

/**
 * Builds a new {@link IfStatement} instance.
 *
 * @param condition the if condition/*ww  w .  j a v  a  2 s  .  co  m*/
 * @param thenStatement the statement of the then clause
 * @param elseStatement the statement of the else clause
 * @return a new if statement
 */
public IfStatement if0(Expression condition, Statement thenStatement, Statement elseStatement) {
    final IfStatement is = ast.newIfStatement();
    is.setExpression(condition);
    is.setThenStatement(thenStatement);
    is.setElseStatement(elseStatement);
    return is;
}

From source file:org.autorefactor.refactoring.rules.AndroidViewHolderRefactoring.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    Block body = node.getBody();//  w  w w .  j  a va  2s  . c o  m
    if (body != null && isMethod(node, "android.widget.Adapter", "getView", "int", "android.view.View",
            "android.view.ViewGroup")) {
        final GetViewVariableVisitor visitor = new GetViewVariableVisitor();
        body.accept(visitor);
        if (visitor.canApplyRefactoring()) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            final Refactorings r = this.ctx.getRefactorings();
            final TypeNameDecider typeNameDecider = new TypeNameDecider(visitor.viewVariableName);

            // Transform tree

            // Create If statement
            final SingleVariableDeclaration viewArg = parameters(node).get(1);
            final Variable convertViewVar = new Variable(viewArg.getName().getIdentifier(), b);
            final InfixExpression condition = b.infixExpr(convertViewVar.varName(), EQUALS, b.null0());
            final Block thenBlock = b.block();
            final IfStatement ifStmt = b.if0(condition, thenBlock);
            r.insertBefore(ifStmt, visitor.viewAssignmentStmt);
            final List<Statement> thenStmts = statements(thenBlock);

            thenStmts.add(
                    b.toStmt(b.assign(convertViewVar.varName(), ASSIGN, b.copy(visitor.getInflateExpr()))));

            // assign to local view variable when necessary
            if (!"convertView".equals(visitor.viewVariableName.getIdentifier())) {
                Statement assignConvertViewToView = null;
                if (visitor.viewVariableDeclFragment != null) {
                    assignConvertViewToView = b.declareStmt(
                            b.copyType(visitor.viewVariableName, typeNameDecider),
                            b.copy(visitor.viewVariableName), convertViewVar.varName());
                } else if (visitor.viewVariableAssignment != null) {
                    assignConvertViewToView = b.toStmt(
                            b.assign(b.copy(visitor.viewVariableName), ASSIGN, convertViewVar.varName()));
                }
                if (assignConvertViewToView != null) {
                    r.insertBefore(assignConvertViewToView, visitor.viewAssignmentStmt);
                }
            }

            // make sure method returns the view to be reused
            if (visitor.returnStmt != null) {
                r.insertAfter(b.return0(b.copy(visitor.viewVariableName)), visitor.returnStmt);
                r.remove(visitor.returnStmt);
            }

            // Optimize findViewById calls
            final FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor(visitor.viewVariableName);
            body.accept(findViewByIdVisitor);
            if (!findViewByIdVisitor.items.isEmpty()) {
                // TODO JNR name conflict? Use VariableNameDecider
                Variable viewHolderItemVar = new Variable("ViewHolderItem", "viewHolderItem", b);

                // create ViewHolderItem class
                r.insertBefore(createViewHolderItemClass(findViewByIdVisitor, viewHolderItemVar.typeName(),
                        typeNameDecider), node);

                // declare viewhHolderItem object
                r.insertFirst(body, Block.STATEMENTS_PROPERTY, viewHolderItemVar.declareStmt());
                // initialize viewHolderItem
                thenStmts.add(b.toStmt(
                        b.assign(viewHolderItemVar.varName(), ASSIGN, b.new0(viewHolderItemVar.type()))));
                // Assign findViewById to ViewHolderItem
                for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
                    // ensure we are accessing convertView object
                    FieldAccess fieldAccess = b.fieldAccess(viewHolderItemVar.varName(),
                            b.simpleName(item.variable.getIdentifier()));
                    // FIXME This does not work: not sure why??
                    // r.set(item.findViewByIdInvocation,
                    // MethodInvocation.EXPRESSION_PROPERTY, convertViewVar.varName());
                    item.findViewByIdInvocation.setExpression(convertViewVar.varName());
                    // FIXME For some reason b.copy() does not do what we would like
                    thenStmts
                            .add(b.toStmt(b.assign(fieldAccess, ASSIGN, b.copySubtree(item.findViewByIdExpr))));

                    // replace previous findViewById with accesses to viewHolderItem
                    r.replace(item.findViewByIdExpr, b.copy(fieldAccess));
                }
                // store viewHolderItem in convertView
                thenStmts.add(b.toStmt(b.invoke("convertView", "setTag", viewHolderItemVar.varName())));

                // retrieve viewHolderItem from convertView
                ifStmt.setElseStatement(b.block(b.toStmt(b.assign(viewHolderItemVar.varName(), ASSIGN,
                        b.cast(viewHolderItemVar.type(), b.invoke("convertView", "getTag"))))));
            }
            r.remove(visitor.viewAssignmentStmt);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}

From source file:org.autorefactor.refactoring.rules.ReduceVariableScopeRefactoring.java

License:Open Source License

private void replace(VariableAccess varDecl, VariableAccess varAccess) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final AST ast = b.getAST();
    final ASTNode scope = varAccess.getScope();
    final Name varName = varAccess.getVariableName();
    final Type varType = getType(varDecl.getVariableName().getParent());
    if (scope instanceof Block) {
        final List<Statement> stmts = statements((Block) scope);
        for (int i = 0; i < stmts.size(); i++) {
            final Statement stmt = stmts.get(i);
            final Expression parentExpr = getAncestor(varName, Expression.class); // FIXME i=0
            final Statement parentStmt = getAncestor(parentExpr, Statement.class); // FIXME i=0
            if (stmt.equals(parentStmt)) {
                final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
                final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
                vds.setType(varType);/*from  w w  w  .  j  a  v a 2 s. c o  m*/
                this.ctx.getRefactorings().replace(stmt, vds);
                break;
            }
        }
    } else if (scope instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) scope;
        final EnhancedForStatement newEfs = b.copy(efs);
        newEfs.setParameter(b.copy(efs.getParameter()));
        newEfs.setExpression(b.copy(efs.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(efs.getBody(), parentStmt)) {
            newEfs.setBody(copy(efs.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(efs, newEfs);
    } else if (scope instanceof ForStatement) {
        final ForStatement fs = (ForStatement) scope;
        final ForStatement newFs = b.copy(fs);
        final List<Expression> initializers = initializers(newFs);
        if (initializers.size() == 1) {
            final Expression init = initializers.remove(0);
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
            final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
            vde.setType(varType);
            initializers.add(vde);
            this.ctx.getRefactorings().replace(fs, newFs);
            // TODO JNR
            // if (equalNotNull(fs.getBody(), parentStmt)) {
            // newFs.setBody(copy(fs.getBody()));
            // }
        } else {
            throw new NotImplementedException(scope, "for more than one initializer in for loop.");
        }
    } else if (scope instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) scope;
        final WhileStatement newWs = ast.newWhileStatement();
        newWs.setExpression(b.copy(ws.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(ws.getBody(), parentStmt)) {
            newWs.setBody(copy(ws.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(ws, newWs);
    } else if (scope instanceof IfStatement) {
        final IfStatement is = (IfStatement) scope;
        final IfStatement newIs = ast.newIfStatement();
        newIs.setExpression(b.copy(is.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(is.getThenStatement(), parentStmt)) {
            newIs.setThenStatement(copy(is.getThenStatement(), varName));
            if (is.getElseStatement() != null) {
                newIs.setElseStatement(b.copy(is.getElseStatement()));
            }
            this.ctx.getRefactorings().replace(is, newIs);
        } else if (equalNotNull(is.getElseStatement(), parentStmt)) {
            if (is.getThenStatement() != null) {
                newIs.setThenStatement(b.copy(is.getThenStatement()));
            }
            newIs.setElseStatement(copy(is.getElseStatement(), varName));
            this.ctx.getRefactorings().replace(is, newIs);
        } else {
            throw new IllegalStateException(is,
                    "Parent statement should be inside the then or else statement of this if statement: " + is);
        }
    } else {
        throw new NotImplementedException(scope);
    }
}

From source file:org.autorefactor.refactoring.rules.ViewHolderRefactoring.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    IMethodBinding methodBinding = node.resolveBinding();

    if (methodBinding != null && isMethod(methodBinding, "android.widget.Adapter", "getView", "int",
            "android.view.View", "android.view.ViewGroup")) {
        GetViewVisitor visitor = new GetViewVisitor();
        Block body = node.getBody();/*from   w w  w. j av  a 2s  .  com*/
        if (body != null) {
            body.accept(visitor);
            if (!visitor.usesConvertView && visitor.viewVariable != null && !visitor.isInflateInsideIf()) {
                // Transform tree

                //Create If statement
                IfStatement ifStatement = b.getAST().newIfStatement();
                // test-clause
                InfixExpression infixExpression = b.getAST().newInfixExpression();
                infixExpression.setOperator(InfixExpression.Operator.EQUALS);
                infixExpression.setLeftOperand(b.simpleName("convertView"));
                infixExpression.setRightOperand(b.getAST().newNullLiteral());
                ifStatement.setExpression(infixExpression);
                //then
                Assignment assignment = b.assign(b.simpleName("convertView"), Assignment.Operator.ASSIGN,
                        b.copy(visitor.getInflateExpression()));
                Block thenBlock = b.block(b.getAST().newExpressionStatement(assignment));
                ifStatement.setThenStatement(thenBlock);
                r.insertBefore(ifStatement, visitor.viewAssignmentStatement);

                // assign to local view variable when necessary
                if (!"convertView".equals(visitor.viewVariable.getIdentifier())) {
                    Statement assignConvertViewToView = null;
                    if (visitor.viewVariableDeclarationFragment != null) {
                        assignConvertViewToView = b.declare(visitor.viewVariable.resolveTypeBinding().getName(),
                                b.copy(visitor.viewVariable), b.simpleName("convertView"));
                    } else if (visitor.viewVariableAssignment != null) {
                        assignConvertViewToView = b.getAST()
                                .newExpressionStatement(b.assign(b.copy(visitor.viewVariable),
                                        Assignment.Operator.ASSIGN, b.simpleName("convertView")));
                    }
                    if (assignConvertViewToView != null) {
                        r.insertBefore(assignConvertViewToView, visitor.viewAssignmentStatement);
                    }
                }

                // make sure method returns the view to be reused DELETEME
                if (visitor.returnStatement != null) {
                    r.insertAfter(b.return0(b.copy(visitor.viewVariable)), visitor.returnStatement);
                    r.remove(visitor.returnStatement);
                }

                //Optimize findViewById calls
                FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor();
                body.accept(findViewByIdVisitor);
                if (findViewByIdVisitor.items.size() > 0) {
                    //create ViewHolderItem class
                    TypeDeclaration viewHolderItemDeclaration = b.getAST().newTypeDeclaration();
                    viewHolderItemDeclaration.setName(b.simpleName("ViewHolderItem"));
                    List<ASTNode> viewItemsDeclarations = viewHolderItemDeclaration.bodyDeclarations();
                    for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {

                        VariableDeclarationFragment declarationFragment = b.getAST()
                                .newVariableDeclarationFragment();
                        SimpleName simpleName = b.simpleName(item.variable.getIdentifier());
                        declarationFragment.setName(simpleName);
                        FieldDeclaration fieldDeclaration = b.getAST().newFieldDeclaration(declarationFragment);
                        fieldDeclaration.setType(b.getAST()
                                .newSimpleType(b.simpleName(item.variable.resolveTypeBinding().getName())));
                        viewItemsDeclarations.add(fieldDeclaration);
                    }
                    viewHolderItemDeclaration.modifiers()
                            .add(b.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD));
                    r.insertBefore(viewHolderItemDeclaration, node);
                    // create viewhHolderItem object
                    VariableDeclarationStatement viewHolderItemVariableDeclaration = b.declare("ViewHolderItem",
                            b.simpleName("viewHolderItem"), null);
                    r.insertAt(viewHolderItemVariableDeclaration, 0, Block.STATEMENTS_PROPERTY, body);
                    //initialize viewHolderItem
                    Assignment viewHolderItemInitialization = b.assign(b.simpleName("viewHolderItem"),
                            Assignment.Operator.ASSIGN, b.new0("ViewHolderItem"));
                    thenBlock.statements().add(b.getAST().newExpressionStatement(viewHolderItemInitialization));
                    //  Assign findViewById to ViewHolderItem
                    for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
                        //ensure we are accessing to convertView object
                        QualifiedName qualifiedName = b.getAST().newQualifiedName(b.name("viewHolderItem"),
                                b.simpleName(item.variable.getIdentifier()));
                        item.findViewByIdInvocation.setExpression(b.simpleName("convertView"));
                        Assignment itemAssignment = b.assign(qualifiedName, Assignment.Operator.ASSIGN,
                                (Expression) ASTNode.copySubtree(b.getAST(), item.findViewByIdExpression));

                        thenBlock.statements().add(b.getAST().newExpressionStatement(itemAssignment));

                        //replace previous fidnviewbyid with accesses to viewHolderItem
                        QualifiedName viewHolderItemFieldAccessQualifiedName = b.getAST().newQualifiedName(
                                b.name("viewHolderItem"), b.simpleName(item.variable.getIdentifier()));
                        r.replace(item.findViewByIdExpression, b.copy(qualifiedName));
                    }
                    //store viewHolderItem in convertView
                    MethodInvocation setTagInvocation = b.invoke("convertView", "setTag",
                            b.simpleName("viewHolderItem"));
                    thenBlock.statements().add(b.getAST().newExpressionStatement(setTagInvocation));

                    //retrieve viewHolderItem from convertView
                    ifStatement
                            .setElseStatement(b.block(b.getAST()
                                    .newExpressionStatement(b.assign(b.simpleName("viewHolderItem"),
                                            Assignment.Operator.ASSIGN,
                                            b.cast("ViewHolderItem", b.invoke("convertView", "getTag"))))));

                }
                r.remove(visitor.viewAssignmentStatement);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}

From source file:org.decojer.cavaj.transformers.TrControlFlowStmts.java

License:Open Source License

@Nullable
private Statement transformCond(@Nonnull final Cond cond) {
    final BB head = cond.getHead();

    final IfStatement ifStatement = (IfStatement) head.getFinalStmt();
    if (ifStatement == null) {
        assert false;
        return null;
    }/*from ww w. j  av  a2 s . c o  m*/
    final Expression condExpression = ifStatement.getExpression();
    if (condExpression == null) {
        assert false;
        return null;
    }
    final E falseOut = head.getFalseOut();
    assert falseOut != null;
    final E trueOut = head.getTrueOut();
    assert trueOut != null;
    boolean negate = false;

    switch (cond.getKind()) {
    case IFNOT:
        ifStatement.setExpression(wrap(not(condExpression)));
        negate = true;
        // fall through
    case IF: {
        final AssertStatement assertStmt = transformAssert(negate ? falseOut : trueOut, condExpression);
        if (assertStmt != null) {
            return assertStmt;
        }
        ifStatement.setThenStatement(transformSequence(cond, negate ? falseOut : trueOut));
        return ifStatement;
    }
    case IFNOT_ELSE:
        ifStatement.setExpression(wrap(not(condExpression)));
        negate = true;
        // fall through
    case IF_ELSE:
        ifStatement.setThenStatement(transformSequence(cond, negate ? falseOut : trueOut));
        ifStatement.setElseStatement(transformSequence(cond, negate ? trueOut : falseOut));
        return ifStatement;
    default:
        log.warn(getM() + ": Unknown cond type '" + cond.getKind() + "'!");
        return null;
    }
}