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

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

Introduction

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

Prototype

public Statement getElseStatement() 

Source Link

Document

Returns the "else" part of this if statement, or null if this if statement has no "else" part.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(IfStatement node) {
    this.fBuffer.append("if (");//$NON-NLS-1$
    node.getExpression().accept(this);
    this.fBuffer.append(") ");//$NON-NLS-1$
    node.getThenStatement().accept(this);
    if (node.getElseStatement() != null) {
        this.fBuffer.append(" else ");//$NON-NLS-1$
        node.getElseStatement().accept(this);
    }/*from  ww  w  .  j  av  a2s .c o  m*/
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(IfStatement node) {
    boa.types.Ast.Statement.Builder b = boa.types.Ast.Statement.newBuilder();
    //      b.setPosition(pos.build());
    List<boa.types.Ast.Statement> list = statements.peek();
    b.setKind(boa.types.Ast.Statement.StatementKind.IF);
    node.getExpression().accept(this);
    b.setExpression(expressions.pop());/*from  ww w.j  av  a 2s. c  o m*/
    statements.push(new ArrayList<boa.types.Ast.Statement>());
    node.getThenStatement().accept(this);
    for (boa.types.Ast.Statement s : statements.pop())
        b.addStatements(s);
    // FIXME
    if (node.getElseStatement() != null) {
        statements.push(new ArrayList<boa.types.Ast.Statement>());
        node.getElseStatement().accept(this);
        for (boa.types.Ast.Statement s : statements.pop())
            b.addStatements(s);
    }
    list.add(b.build());
    return false;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

public IMessageGrouping[] calculateGroups(UMLSequenceViewer viewer, Object activationElement,
        Object[] children) {/*from ww w  .ja va 2 s.  co m*/
    HashMap<ASTNode, MappedMessageGrouping> groups = new HashMap<ASTNode, MappedMessageGrouping>();
    if (!(activationElement instanceof IAdaptable)) {
        return new IMessageGrouping[0];
    }
    ASTNode activationNode = (ASTNode) ((IAdaptable) activationElement).getAdapter(ASTNode.class);
    if (!(activationNode instanceof MethodDeclaration)) {
        return new IMessageGrouping[0];
    }
    for (int i = 0; i < children.length; i++) {
        if (children[i] instanceof IAdaptable) {
            ASTNode messageNode = (ASTNode) ((IAdaptable) children[i]).getAdapter(ASTNode.class);
            if (messageNode != null) {
                ASTNode blockParent = findBlockParent(messageNode);

                List<MappedMessageGrouping> blocks = new LinkedList<MappedMessageGrouping>();
                while (blockParent != null && blockParent.getNodeType() != ASTNode.METHOD_DECLARATION) {

                    if (blockParent != null && blockParent.getNodeType() == ASTNode.IF_STATEMENT) {
                        IfStatement ifStatement = (IfStatement) blockParent;
                        ASTNode block = checkIfSide(ifStatement, messageNode);
                        if (block != null && block.equals(ifStatement.getElseStatement())) {
                            //add a block for the else statement as well
                            MappedMessageGrouping blockNode = groups.get(block);
                            if (blockNode == null) {
                                blockNode = new MappedMessageGrouping(activationElement, i, 0, "", block);
                                groups.put(block, blockNode);
                            }
                            blocks.add(blockNode);
                        }
                    }
                    MappedMessageGrouping blockNode = groups.get(blockParent);
                    if (blockNode == null) {
                        blockNode = new MappedMessageGrouping(activationElement, i, 0, "", blockParent);
                        groups.put(blockParent, blockNode);
                    }
                    blocks.add(blockNode);
                    blockParent = findBlockParent(blockParent);
                }
                for (MappedMessageGrouping blockNode : blocks) {
                    blockNode.setLength(blockNode.getLength() + 1);
                }
            }
        }
    }
    ArrayList<MappedMessageGrouping> groupList = new ArrayList<MappedMessageGrouping>(groups.values());
    Collections.sort(groupList, new Comparator<MappedMessageGrouping>() {
        public int compare(MappedMessageGrouping o1, MappedMessageGrouping o2) {
            ASTNode n1 = (ASTNode) o1.getKey();
            ASTNode n2 = (ASTNode) o2.getKey();
            int diff = n1.getStartPosition() - n2.getStartPosition();
            if (diff == 0) {
                diff = (n1.getStartPosition() + n1.getLength()) - (n2.getStartPosition() + n2.getLength());
            }
            if (diff == 0) {
                IfStatement ifStatement = null;
                //make sure that else statements are contained in if statements
                if (n1 instanceof IfStatement) {
                    ifStatement = (IfStatement) n1;
                } else if (n2 instanceof IfStatement) {
                    ifStatement = (IfStatement) n2;
                }
                if (ifStatement != null) {
                    if (n2.equals(ifStatement.getElseStatement())) {
                        return -1;
                    } else if (n1.equals(ifStatement.getElseStatement())) {
                        return 1;
                    }
                }
            }
            return diff;
        }
    });
    for (MappedMessageGrouping blockNode : groupList) {
        updateGrouping(blockNode);
    }
    return groupList.toArray(new IMessageGrouping[groupList.size()]);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

/**
 * @param currentParent/*from   w w w.  j  a  va 2 s .  c  o m*/
 * @param messageNode
 * @return
 */
private ASTNode checkIfSide(IfStatement ifStatement, ASTNode messageNode) {
    ASTNode currentParent = messageNode;
    ASTNode thenStatement = ifStatement.getThenStatement();
    ASTNode elseStatement = ifStatement.getElseStatement();
    while (currentParent != null && currentParent != ifStatement) {
        if (currentParent == thenStatement) {
            return ifStatement;
        } else if (currentParent == elseStatement) {
            return elseStatement;
        }
        currentParent = currentParent.getParent();
    }
    return null;
}

From source file:cc.kave.eclipse.commons.analysis.transformer.BodyVisitor.java

License:Apache License

@Override
public boolean visit(IfStatement stmt) {
    if (isTargetMatch(stmt, CompletionCase.EmptyCompletionBefore)) {
        body.add(getEmptyCompletionExpression());
    }//www  .j  a v a  2s  .  com
    IfElseBlock ifElseBlock = new IfElseBlock();
    // TODO: Was bedeutet stmt.Condition?
    // Condition = _exprVisitor.ToSimpleExpression(stmt.Condition, body) ??
    // new UnknownExpression()
    ifElseBlock.setCondition(exprVisitor.toSimpleExpression(null, body));

    if (isTargetMatch(stmt, CompletionCase.InBody)) {
        ifElseBlock.getThen().add(getEmptyCompletionExpression());
    }
    if (isTargetMatch(stmt, CompletionCase.InElse)) {
        ifElseBlock.getElse().add(getEmptyCompletionExpression());
    }
    if (stmt.getThenStatement() != null) {
        stmt.getThenStatement().accept(new BodyVisitor(nameGen, marker, ifElseBlock.getThen()));
    }
    if (stmt.getElseStatement() != null) {
        stmt.getElseStatement().accept(new BodyVisitor(nameGen, marker, ifElseBlock.getElse()));
    }

    body.add(ifElseBlock);

    if (isTargetMatch(stmt, CompletionCase.EmptyCompletionAfter)) {
        body.add(getEmptyCompletionExpression());
    }
    return super.visit(stmt);
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(IfStatement node) {
    Statement thenStmt = node.getThenStatement();
    Statement elseStmt = node.getElseStatement();
    Expression condExpr = node.getExpression();

    String thenStr = thenStmt.toString().replace('\n', ' ');
    String elseStr = "";
    if (elseStmt != null) {
        elseStr = elseStmt.toString().replace('\n', ' ');
    }//from w  w w  .j av  a  2s .  c  o m
    if (this.mtbStack.isEmpty()) {
        return true;
    }
    IMethodBinding mtb = (IMethodBinding) this.mtbStack.peek();
    String methodStr = getQualifiedName(mtb);
    String condStr = condExpr.toString();

    condStr = edit_str(condStr);
    thenStr = edit_str(thenStr);
    elseStr = edit_str(elseStr);
    try {
        this.facts.add(Fact.makeConditionalFact(condStr, thenStr, elseStr, methodStr));
    } catch (Exception e) {
        System.err.println("Cannot resolve conditional \"" + condExpr.toString() + "\"");
        System.out.println("ifStmt: " + thenStr);
        System.out.println("elseStmt: " + elseStr);
        System.err.println(e.getMessage());
    }
    return true;
}

From source file:chibi.gumtreediff.gen.jdt.cd.CdJdtVisitor.java

License:Open Source License

@Override
public boolean visit(IfStatement node) {
    String expression = node.getExpression().toString();
    pushNode(node, expression/* , node.getStartPosition(), node.getLength() */);

    Statement stmt = node.getThenStatement();
    if (stmt != null) {
        pushNode(stmt, expression);/*from ww  w. j  a va2 s.  c  o  m*/
        stmt.accept(this);
        popNode();
    }

    stmt = node.getElseStatement();
    if (stmt != null) {
        pushNode(stmt, expression);
        node.getElseStatement().accept(this);
        popNode();
    }
    return false;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(IfStatement node) {
    printIndent();//  w w w . jav  a 2 s  .c om
    this.buffer.append("if (");//$NON-NLS-1$
    node.getExpression().accept(this);
    this.buffer.append(") ");//$NON-NLS-1$
    node.getThenStatement().accept(this);
    if (node.getElseStatement() != null) {
        this.buffer.append(" else ");//$NON-NLS-1$
        node.getElseStatement().accept(this);
    }
    return false;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.LineBreaksPreparator.java

License:Open Source License

@Override
public boolean visit(IfStatement node) {
    Statement elseNode = node.getElseStatement();
    Statement thenNode = node.getThenStatement();
    if (elseNode != null) {
        if (this.options.insert_new_line_before_else_in_if_statement || !(thenNode instanceof Block))
            this.tm.firstTokenBefore(elseNode, TokenNameelse).breakBefore();

        boolean keepElseOnSameLine = (elseNode instanceof Block)
                || (this.options.keep_else_statement_on_same_line)
                || (this.options.compact_else_if && (elseNode instanceof IfStatement));
        if (!keepElseOnSameLine) {
            breakLineBefore(elseNode);// w ww  .  j  a v a 2s  . c om
            indent(elseNode);
        }
    }

    boolean keepThenOnSameLine = this.options.keep_then_statement_on_same_line
            || (this.options.keep_simple_if_on_one_line && elseNode == null);
    if (!keepThenOnSameLine && !(thenNode instanceof Block)) {
        breakLineBefore(thenNode);
        indent(thenNode);
    }

    return true;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java

License:Open Source License

@Override
public boolean visit(IfStatement node) {
    if (!(node.getThenStatement() instanceof Block)) {
        int thenIndex = this.tm.firstIndexIn(node.getThenStatement(), -1);
        if (this.tm.get(thenIndex).getLineBreaksBefore() == 0)
            this.wrapIndexes.add(thenIndex);
    }/*  www  . j  a  v a2  s.  c  o m*/
    Statement elseStatement = node.getElseStatement();
    if (elseStatement != null && !(elseStatement instanceof Block) && !(elseStatement instanceof IfStatement)) {
        int elseIndex = this.tm.firstIndexIn(elseStatement, -1);
        if (this.tm.get(elseIndex).getLineBreaksBefore() == 0)
            this.wrapIndexes.add(elseIndex);
    }
    if (!this.wrapIndexes.isEmpty()) {
        this.wrapParentIndex = this.tm.firstIndexAfter(node.getExpression(), TokenNameRPAREN);
        this.wrapGroupEnd = this.tm.lastIndexIn(node, -1);
        handleWrap(this.options.alignment_for_compact_if, node);
    }
    return true;
}