Search in sources :

Example 16 with SwitchCase

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

the class LocalCorrectionsSubProcessor method createMissingCaseProposals.

@SuppressWarnings("deprecation")
public static void createMissingCaseProposals(IInvocationContext context, ASTNode parent, ArrayList<String> enumConstNames, 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;
    }
    int defaultIndex = statements.size();
    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())) {
                if (switchCase.expressions().size() == 0) {
                    defaultIndex = i;
                    break;
                }
            } else if (switchCase.getExpression() == null) {
                defaultIndex = i;
                break;
            }
        }
    }
    boolean hasDefault = defaultIndex < statements.size();
    AST ast = parent.getAST();
    if (enumConstNames.size() > 0) {
        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_missing_cases_description;
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_CASE_STATEMENTS);
        for (int i = 0; i < enumConstNames.size(); i++) {
            SwitchCase newSwitchCase = ast.newSwitchCase();
            String enumConstName = enumConstNames.get(i);
            Name newName = ast.newName(enumConstName);
            if (ASTHelper.isSwitchCaseExpressionsSupportedInAST(ast)) {
                newSwitchCase.expressions().add(newName);
            } else {
                newSwitchCase.setExpression(newName);
            }
            listRewrite.insertAt(newSwitchCase, defaultIndex, null);
            defaultIndex++;
            if (!hasDefault) {
                if (ASTHelper.isSwitchExpressionNodeSupportedInAST(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 = getThrowForUnsupportedCase(expression, ast, astRewrite);
                            listRewrite.insertLast(newThrowStatement, null);
                            proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, enumConstName);
                        } else {
                            listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
                        }
                    } else {
                        listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
                    }
                } else {
                    listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
                }
                defaultIndex++;
            }
        }
        if (!hasDefault) {
            SwitchCase newSwitchCase = ast.newSwitchCase();
            listRewrite.insertAt(newSwitchCase, defaultIndex, null);
            defaultIndex++;
            if (ASTHelper.isSwitchExpressionNodeSupportedInAST(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);
                        // $NON-NLS-1$
                        proposal.addLinkedPosition(astRewrite.track(newThrowStatement), true, "defaultCase");
                    } else {
                        listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
                    }
                } else {
                    listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
                }
            } else {
                newSwitchCase.setExpression(null);
                listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
            }
        }
        proposals.add(proposal);
    }
    if (!hasDefault) {
        createMissingDefaultProposal(context, parent, proposals);
    }
}
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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) 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 17 with SwitchCase

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

the class LocalCorrectionsSubProcessor method addCasesOmittedProposals.

public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
        AST ast = selectedNode.getAST();
        SwitchStatement parent = (SwitchStatement) selectedNode.getParent();
        for (Statement statement : (List<Statement>) parent.statements()) {
            if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
                // insert //$CASES-OMITTED$:
                ASTRewrite rewrite = ASTRewrite.create(ast);
                rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
                ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
                // $NON-NLS-1$
                ASTNode casesOmittedComment = rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT);
                listRewrite.insertBefore(casesOmittedComment, statement, null);
                String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED);
                proposals.add(proposal);
                break;
            }
        }
    }
}
Also used : NoCommentSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer) AST(org.eclipse.jdt.core.dom.AST) 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) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 18 with SwitchCase

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

the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.

public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    ASTNode parent = selectedNode.getParent();
    while (parent instanceof ExpressionStatement) {
        selectedNode = parent;
        parent = selectedNode.getParent();
    }
    if (parent instanceof WhileStatement) {
        addRemoveIncludingConditionProposal(context, parent, null, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
        Statement elseStatement = ((IfStatement) parent).getElseStatement();
        addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
    } else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
        Statement thenStatement = ((IfStatement) parent).getThenStatement();
        addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
    } else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
        Statement body = ((ForStatement) parent).getBody();
        addRemoveIncludingConditionProposal(context, parent, body, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
        Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
        addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
    } else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
        Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
        addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
    } else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
        // also offer split && / || condition proposals:
        InfixExpression infixExpression = (InfixExpression) parent;
        Expression leftOperand = infixExpression.getLeftOperand();
        ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
        Expression replacement = leftOperand;
        while (replacement instanceof ParenthesizedExpression) {
            replacement = ((ParenthesizedExpression) replacement).getExpression();
        }
        Expression toReplace = infixExpression;
        while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
            toReplace = (Expression) toReplace.getParent();
        }
        if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
            if (leftOperand instanceof ParenthesizedExpression) {
                replacement = (Expression) replacement.getParent();
            } else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
                toReplace = ((ParenthesizedExpression) toReplace).getExpression();
            }
        }
        rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        addRemoveProposal(context, rewrite, label, proposals);
        InnovationContext assistContext = new InnovationContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
        assistContext.setASTRoot(root);
        InvertBooleanUtility.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
        InvertBooleanUtility.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
    } else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
        // remove all statements following the unreachable:
        List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
        int idx = statements.indexOf(selectedNode);
        ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
        if (idx > 0) {
            Object prevStatement = statements.get(idx - 1);
            if (prevStatement instanceof IfStatement) {
                IfStatement ifStatement = (IfStatement) prevStatement;
                if (ifStatement.getElseStatement() == null) {
                    // remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
                    rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
                    label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
                }
            }
        }
        for (int i = idx; i < statements.size(); i++) {
            ASTNode statement = statements.get(i);
            if (statement instanceof SwitchCase) {
                // stop at case *: and default:
                break;
            }
            rewrite.remove(statement, null);
        }
        addRemoveProposal(context, rewrite, label, proposals);
    } else {
        // no special case, just remove the node:
        addRemoveProposal(context, selectedNode, proposals);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor) IfStatement(org.eclipse.jdt.core.dom.IfStatement) 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) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 19 with SwitchCase

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

the class AdvancedQuickAssistProcessor method createSwitchCaseStatements.

private static SwitchCase[] createSwitchCaseStatements(AST ast, ASTRewrite rewrite, List<Expression> caseExpressions) {
    int len = (caseExpressions.size() == 0) ? 1 : caseExpressions.size();
    SwitchCase[] switchCaseStatements = new SwitchCase[len];
    if (caseExpressions.size() == 0) {
        switchCaseStatements[0] = ast.newSwitchCase();
        switchCaseStatements[0].setExpression(null);
    } else {
        for (int i = 0; i < caseExpressions.size(); i++) {
            ASTNode astNode = caseExpressions.get(i);
            switchCaseStatements[i] = ast.newSwitchCase();
            switchCaseStatements[i].setExpression((Expression) rewrite.createCopyTarget(astNode));
        }
    }
    return switchCaseStatements;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 20 with SwitchCase

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

the class ExtractTempRefactoring method canReplace.

private static boolean canReplace(IASTFragment fragment) {
    ASTNode node = fragment.getAssociatedNode();
    ASTNode parent = node.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
        if (node.equals(vdf.getName()))
            return false;
    }
    if (isMethodParameter(node))
        return false;
    if (isThrowableInCatchBlock(node))
        return false;
    if (parent instanceof ExpressionStatement)
        return false;
    if (isLeftValue(node))
        return false;
    if (isReferringToLocalVariableFromFor((Expression) node))
        return false;
    if (isUsedInForInitializerOrUpdater((Expression) node))
        return false;
    if (parent instanceof SwitchCase)
        return false;
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

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