Search in sources :

Example 11 with SwitchCase

use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse-pmd by acanda.

the class DefaultLabelNotLastInSwitchStmtQuickFix method apply.

/**
 * Moves the default case to the last position. The default case includes the default {@code SwitchCase} and all
 * following statements up to the next {@code SwitchCase}.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final SwitchStatement node) {
    final List<Statement> statements = node.statements();
    final List<Statement> defaultCaseStatements = new ArrayList<>(statements.size());
    boolean isDefaultCaseStatement = false;
    for (final Statement statement : statements) {
        if (statement instanceof SwitchCase) {
            if (((SwitchCase) statement).getExpression() == DEFAULT_LABEL) {
                isDefaultCaseStatement = true;
            } else {
                isDefaultCaseStatement = false;
            }
        }
        if (isDefaultCaseStatement) {
            defaultCaseStatements.add(statement);
        }
    }
    statements.removeAll(defaultCaseStatements);
    statements.addAll(defaultCaseStatements);
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) ArrayList(java.util.ArrayList)

Example 12 with SwitchCase

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

the class ExtractConstantRefactoring method canReplace.

// !! - like one in ExtractTempRefactoring
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 (parent instanceof ExpressionStatement) {
        return false;
    }
    if (parent instanceof SwitchCase) {
        if (node instanceof Name) {
            Name name = (Name) node;
            ITypeBinding typeBinding = name.resolveTypeBinding();
            if (typeBinding != null) {
                return !typeBinding.isEnum();
            }
        }
    }
    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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 13 with SwitchCase

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

the class ExtractFieldRefactoring 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 (parent instanceof LambdaExpression) {
        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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 14 with SwitchCase

use of org.eclipse.jdt.core.dom.SwitchCase in project eclipse.jdt.ls 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 (parent instanceof LambdaExpression) {
        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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 15 with SwitchCase

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

the class LocalCorrectionsSubProcessor method addMissingDefaultCaseProposal.

public static void addMissingDefaultCaseProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof Expression) {
        StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
        ASTNode parent = selectedNode.getParent();
        List<Statement> statements;
        if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
            statements = ((SwitchStatement) parent).statements();
        } else if (locationInParent == SwitchExpression.EXPRESSION_PROPERTY) {
            statements = ((SwitchExpression) parent).statements();
        } else {
            return;
        }
        for (Statement statement : statements) {
            if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
                return;
            }
        }
        createMissingDefaultProposal(context, parent, proposals);
    }
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) 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) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

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