Search in sources :

Example 6 with SwitchExpression

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

the class LocalCorrectionsSubProcessor method getMissingEnumConstantCaseProposals.

public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    for (ChangeCorrectionProposal proposal : proposals) {
        if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(proposal.getName())) {
            return;
        }
    }
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof Expression) {
        StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
        ASTNode parent = selectedNode.getParent();
        ITypeBinding binding;
        List<Statement> statements;
        if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
            SwitchStatement statement = (SwitchStatement) parent;
            binding = statement.getExpression().resolveTypeBinding();
            statements = statement.statements();
        } else if (locationInParent == SwitchExpression.EXPRESSION_PROPERTY) {
            SwitchExpression switchExpression = (SwitchExpression) parent;
            binding = switchExpression.getExpression().resolveTypeBinding();
            statements = switchExpression.statements();
        } else {
            return;
        }
        if (binding == null || !binding.isEnum()) {
            return;
        }
        ArrayList<String> missingEnumCases = new ArrayList<>();
        boolean hasDefault = evaluateMissingSwitchCases(binding, statements, missingEnumCases);
        if (missingEnumCases.size() == 0 && hasDefault) {
            return;
        }
        createMissingCaseProposals(context, parent, missingEnumCases, proposals);
    }
}
Also used : 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) ArrayList(java.util.ArrayList) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 7 with SwitchExpression

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

SwitchExpression (org.eclipse.jdt.core.dom.SwitchExpression)7 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)7 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 Statement (org.eclipse.jdt.core.dom.Statement)5 ThrowStatement (org.eclipse.jdt.core.dom.ThrowStatement)5 TryStatement (org.eclipse.jdt.core.dom.TryStatement)5 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)4 EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)4 IfStatement (org.eclipse.jdt.core.dom.IfStatement)4 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)4 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)4 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)4 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3