Search in sources :

Example 26 with SwitchCase

use of org.eclipse.jdt.core.dom.SwitchCase in project flux by eclipse.

the class StatementAnalyzer method endVisit.

/* (non-Javadoc)
	 * Method declared in ASTVisitor
	 */
@Override
public void endVisit(SwitchStatement node) {
    ASTNode[] selectedNodes = getSelectedNodes();
    if (doAfterValidation(node, selectedNodes)) {
        List<SwitchCase> cases = getSwitchCases(node);
        for (int i = 0; i < selectedNodes.length; i++) {
            ASTNode topNode = selectedNodes[i];
            if (cases.contains(topNode)) {
                invalidSelection(RefactoringCoreMessages.StatementAnalyzer_switch_statement);
                break;
            }
        }
    }
    super.endVisit(node);
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 27 with SwitchCase

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

the class ObsoleteSwitchCleanUp method mergeCases.

private void mergeCases(final Merge merge, final SwitchCaseSection sectionToKeep, final SwitchCaseSection sectionToRemove) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSwitchCleanUp_description);
    Statement caseKept;
    if (merge == Merge.BEFORE_SWITCH_CASES) {
        caseKept = sectionToKeep.existingCases.get(0);
    } else {
        // move == Move.AFTER_SWITCH_CASES
        caseKept = sectionToKeep.statements.get(0);
    }
    for (SwitchCase caseToMove : sectionToRemove.existingCases) {
        rewrite.insertBefore(ASTNodes.createMoveTarget(rewrite, caseToMove), caseKept, group);
    }
    rewrite.remove(sectionToRemove.statements, group);
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) 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) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 28 with SwitchCase

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

the class ObsoleteSwitchCleanUp method getSwitchStructure.

private List<SwitchCaseSection> getSwitchStructure(final SwitchStatement node) {
    List<SwitchCaseSection> switchStructure = new ArrayList<>();
    SwitchCaseSection currentCase = new SwitchCaseSection();
    for (Statement statement : (List<Statement>) node.statements()) {
        if (statement instanceof SwitchCase) {
            if (!currentCase.statements.isEmpty()) {
                switchStructure.add(currentCase);
                currentCase = new SwitchCaseSection();
            }
            SwitchCase swithCase = (SwitchCase) statement;
            currentCase.existingCases.add(swithCase);
        } else {
            currentCase.statements.add(statement);
        }
    }
    if (!currentCase.existingCases.isEmpty()) {
        switchStructure.add(currentCase);
    }
    return switchStructure;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) 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) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with SwitchCase

use of org.eclipse.jdt.core.dom.SwitchCase 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)

Example 30 with SwitchCase

use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse-cs by checkstyle.

the class DefaultComesLastQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SwitchCase node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                if (node.isDefault() && !isLastSwitchCase(node)) {
                    SwitchStatement switchStatement = (SwitchStatement) node.getParent();
                    List<ASTNode> defaultCaseStatements = new ArrayList<>();
                    defaultCaseStatements.add(node);
                    // collect all statements belonging to the default case
                    int defaultStatementIndex = switchStatement.statements().indexOf(node);
                    for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
                        ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
                        if (!(tmpNode instanceof SwitchCase)) {
                            defaultCaseStatements.add(tmpNode);
                        } else {
                            break;
                        }
                    }
                    // move the statements to the end of the statement list
                    switchStatement.statements().removeAll(defaultCaseStatements);
                    switchStatement.statements().addAll(defaultCaseStatements);
                }
            }
            return true;
        }

        private boolean isLastSwitchCase(SwitchCase switchCase) {
            SwitchStatement switchStatement = (SwitchStatement) switchCase.getParent();
            // collect all statements belonging to the default case
            int defaultStatementIndex = switchStatement.statements().indexOf(switchCase);
            for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
                ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
                if (tmpNode instanceof SwitchCase) {
                    return false;
                }
            }
            return true;
        }
    };
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Aggregations

SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)34 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)19 Statement (org.eclipse.jdt.core.dom.Statement)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)15 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)15 ForStatement (org.eclipse.jdt.core.dom.ForStatement)14 IfStatement (org.eclipse.jdt.core.dom.IfStatement)14 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)14 Expression (org.eclipse.jdt.core.dom.Expression)11 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)11 TryStatement (org.eclipse.jdt.core.dom.TryStatement)10 ArrayList (java.util.ArrayList)9 EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)9 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)9 ThrowStatement (org.eclipse.jdt.core.dom.ThrowStatement)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)8 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)8 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)7 DoStatement (org.eclipse.jdt.core.dom.DoStatement)7