Search in sources :

Example 31 with SwitchCase

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

the class MissingSwitchDefaultQuickfix method handleGetCorrectingASTVisitor.

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

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SwitchStatement node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                SwitchCase defNode = node.getAST().newSwitchCase();
                defNode.setExpression(null);
                node.statements().add(defNode);
                node.statements().add(node.getAST().newBreakStatement());
            }
            // also visit children
            return true;
        }
    };
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 32 with SwitchCase

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

the class LocalCorrectionsSubProcessor method createMissingDefaultProposal.

@SuppressWarnings("deprecation")
private static void createMissingDefaultProposal(IInvocationContext context, ASTNode parent, Collection<ChangeCorrectionProposal> proposals) {
    List<Statement> statements;
    Expression expression;
    if (parent instanceof SwitchStatement) {
        SwitchStatement switchStatement = (SwitchStatement) parent;
        statements = switchStatement.statements();
        expression = switchStatement.getExpression();
    } else if (parent instanceof SwitchExpression) {
        SwitchExpression switchExpression = (SwitchExpression) parent;
        statements = switchExpression.statements();
        expression = switchExpression.getExpression();
    } else {
        return;
    }
    AST ast = parent.getAST();
    ASTRewrite astRewrite = ASTRewrite.create(ast);
    ListRewrite listRewrite;
    if (parent instanceof SwitchStatement) {
        listRewrite = astRewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
    } else {
        listRewrite = astRewrite.getListRewrite(parent, SwitchExpression.STATEMENTS_PROPERTY);
    }
    String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description;
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE);
    SwitchCase newSwitchCase = ast.newSwitchCase();
    listRewrite.insertLast(newSwitchCase, null);
    if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(ast)) {
        if (statements.size() > 0) {
            Statement firstStatement = statements.get(0);
            SwitchCase switchCase = (SwitchCase) firstStatement;
            boolean isArrow = switchCase.isSwitchLabeledRule();
            newSwitchCase.setSwitchLabeledRule(isArrow);
            if (isArrow || parent instanceof SwitchExpression) {
                ThrowStatement newThrowStatement = getThrowForUnexpectedDefault(expression, ast, astRewrite);
                listRewrite.insertLast(newThrowStatement, null);
                proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, null);
            } else {
                listRewrite.insertLast(ast.newBreakStatement(), null);
            }
        } else {
            listRewrite.insertLast(ast.newBreakStatement(), null);
        }
    } else {
        newSwitchCase.setExpression(null);
        listRewrite.insertLast(ast.newBreakStatement(), null);
    }
    proposals.add(proposal);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) 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) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 33 with SwitchCase

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

the class LocalCorrectionsSubProcessor method evaluateMissingSwitchCases.

@SuppressWarnings("deprecation")
public static boolean evaluateMissingSwitchCases(ITypeBinding enumBindings, List<Statement> switchStatements, ArrayList<String> enumConstNames) {
    for (IVariableBinding field : enumBindings.getDeclaredFields()) {
        if (field.isEnumConstant()) {
            enumConstNames.add(field.getName());
        }
    }
    boolean hasDefault = false;
    List<Statement> statements = switchStatements;
    for (int i = 0; i < statements.size(); i++) {
        Statement curr = statements.get(i);
        if (curr instanceof SwitchCase) {
            SwitchCase switchCase = (SwitchCase) curr;
            if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(switchCase.getAST())) {
                List<Expression> expressions = switchCase.expressions();
                if (expressions.size() == 0) {
                    hasDefault = true;
                } else {
                    for (Expression expression : expressions) {
                        if (expression instanceof SimpleName) {
                            enumConstNames.remove(((SimpleName) expression).getFullyQualifiedName());
                        }
                    }
                }
            } else {
                Expression expression = ((SwitchCase) curr).getExpression();
                if (expression instanceof SimpleName) {
                    enumConstNames.remove(((SimpleName) expression).getFullyQualifiedName());
                } else if (expression == null) {
                    hasDefault = true;
                }
            }
        }
    }
    return hasDefault;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) 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) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 34 with SwitchCase

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

the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.

private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
    if (covering instanceof Block) {
        List<Statement> statements = ((Block) covering).statements();
        int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1 || startIndex >= statements.size()) {
            return false;
        }
        covering = statements.get(startIndex);
    } else {
        while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
            covering = covering.getParent();
        }
    }
    SwitchStatement switchStatement;
    if (covering instanceof SwitchStatement) {
        switchStatement = (SwitchStatement) covering;
    } else {
        return false;
    }
    SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
    if (fix == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    // add correction proposal
    try {
        CompilationUnitChange change = fix.createChange(null);
        ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
        resultingCollections.add(proposal);
    } catch (CoreException e) {
    // continue
    }
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) CoreException(org.eclipse.core.runtime.CoreException) SwitchExpressionsFixCore(org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

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