Search in sources :

Example 31 with SwitchStatement

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

the class ASTNodeFactory method newSwitchStatement.

/**
 * Builds a new {@link SwitchStatement} instance.
 *
 * @param expression the switch expression
 * @return a new switch statement
 */
public SwitchStatement newSwitchStatement(final Expression expression) {
    SwitchStatement ss = ast.newSwitchStatement();
    ss.setExpression(expression);
    return ss;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement)

Example 32 with SwitchStatement

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

the class ASTNodes method canHaveSiblings.

/**
 * Returns true if a sibling may exist.
 *
 * @param node the start node
 * @return true if a sibling may exist
 */
public static boolean canHaveSiblings(final Statement node) {
    ASTNode statementAtLevel = statementAtLevel(node);
    ASTNode parent = statementAtLevel.getParent();
    return parent instanceof Block || parent instanceof SwitchStatement && statementAtLevel.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 33 with SwitchStatement

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

the class ASTNodes method getSiblings.

private static List<Statement> getSiblings(final Statement startNode, final boolean isForward) {
    Statement statementAtLevel = statementAtLevel(startNode);
    if (canHaveSiblings(statementAtLevel)) {
        List<Statement> statements;
        if (statementAtLevel.getParent() instanceof SwitchStatement) {
            statements = ((SwitchStatement) statementAtLevel.getParent()).statements();
        } else {
            statements = asList((Statement) statementAtLevel.getParent());
        }
        int indexOfNode = statements.indexOf(statementAtLevel);
        int siblingIndex = indexOfNode + (isForward ? 1 : -1);
        if (0 <= siblingIndex && siblingIndex < statements.size()) {
            if (isForward) {
                return statements.subList(siblingIndex, statements.size());
            }
            return statements.subList(0, siblingIndex + 1);
        }
    }
    return Collections.emptyList();
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) 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)

Example 34 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final ReturnStatement node) {
    this.appendToBuffer("return");
    Expression _expression = node.getExpression();
    boolean _tripleNotEquals = (_expression != null);
    if (_tripleNotEquals) {
        this.appendSpaceToBuffer();
        node.getExpression().accept(this);
        this.appendSpaceToBuffer();
    } else {
        final ASTNode parent = node.getParent();
        final boolean isIfElse = ((parent instanceof IfStatement) && (((IfStatement) parent).getElseStatement() != null));
        if (((!isIfElse) && (!(parent instanceof SwitchStatement)))) {
            this.appendToBuffer(";");
        }
    }
    return false;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 35 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final SwitchStatement node) {
    this.appendLineWrapToBuffer();
    this.appendToBuffer("switch (");
    node.getExpression().accept(this);
    this.appendToBuffer(") ");
    this.appendToBuffer("{");
    this.increaseIndent();
    final Function2<Map<SwitchCase, ArrayList<Statement>>, Statement, Map<SwitchCase, ArrayList<Statement>>> _function = (Map<SwitchCase, ArrayList<Statement>> map, Statement currStatement) -> {
        if ((currStatement instanceof SwitchCase)) {
            map.put(((SwitchCase) currStatement), CollectionLiterals.<Statement>newArrayList());
        } else {
            map.get(IterableExtensions.<SwitchCase>last(map.keySet())).add(currStatement);
        }
        return map;
    };
    final Map<SwitchCase, ArrayList<Statement>> foldedCases = IterableExtensions.<Statement, Map<SwitchCase, ArrayList<Statement>>>fold(node.statements(), CollectionLiterals.<SwitchCase, ArrayList<Statement>>newLinkedHashMap(), _function);
    final BiConsumer<SwitchCase, ArrayList<Statement>> _function_1 = (SwitchCase switchCase, ArrayList<Statement> statements) -> {
        switchCase.accept(this);
        final boolean isLastCase = switchCase.equals(IterableExtensions.<SwitchCase>last(foldedCases.keySet()));
        if ((statements.isEmpty() && (!isLastCase))) {
            this.appendToBuffer(",");
        } else {
            this.appendToBuffer(":");
            final boolean probablyReturns = ((IterableExtensions.<Statement>last(statements) instanceof ReturnStatement) || ((IterableExtensions.<Statement>last(statements) instanceof Block) && (IterableExtensions.<Object>last(((Block) IterableExtensions.<Statement>last(statements)).statements()) instanceof ReturnStatement)));
            if (((!isLastCase) && (!probablyReturns))) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("/* FIXME unsupported fall-through */");
                this.appendToBuffer(_builder.toString());
                this.addProblem(node, "Unsupported fall-through case in switch expression");
            }
        }
        final boolean surround = ((isLastCase && statements.isEmpty()) || ((!statements.isEmpty()) && (!(statements.get(0) instanceof Block))));
        if (surround) {
            this.appendToBuffer("{");
            this.increaseIndent();
            this.appendLineWrapToBuffer();
        }
        this.visitAll(statements);
        if (surround) {
            this.decreaseIndent();
            this.appendLineWrapToBuffer();
            this.appendToBuffer("}");
        }
    };
    foldedCases.forEach(_function_1);
    this.decreaseIndent();
    this.appendLineWrapToBuffer();
    this.appendToBuffer("}");
    return false;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) 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) ArrayList(java.util.ArrayList) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Block(org.eclipse.jdt.core.dom.Block) Map(java.util.Map)

Aggregations

SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)35 Statement (org.eclipse.jdt.core.dom.Statement)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 DoStatement (org.eclipse.jdt.core.dom.DoStatement)12 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)11 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)10 Expression (org.eclipse.jdt.core.dom.Expression)9 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Block (org.eclipse.jdt.core.dom.Block)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)6 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 TryStatement (org.eclipse.jdt.core.dom.TryStatement)5