Search in sources :

Example 1 with SwitchCase

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

the class Checks method isEnumCase.

public static boolean isEnumCase(ASTNode node) {
    if (node instanceof SwitchCase) {
        final SwitchCase caze = (SwitchCase) node;
        final Expression expression = caze.getExpression();
        if (expression instanceof Name) {
            final Name name = (Name) expression;
            final IBinding binding = name.resolveBinding();
            if (binding instanceof IVariableBinding) {
                IVariableBinding variableBinding = (IVariableBinding) binding;
                return variableBinding.isEnumConstant();
            }
        }
    }
    return false;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) Expression(org.eclipse.jdt.core.dom.Expression) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name)

Example 2 with SwitchCase

use of org.eclipse.jdt.core.dom.SwitchCase in project che 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 3 with SwitchCase

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

the class SwitchRefactoring method getSwitchStructure.

private List<SwitchCaseSection> getSwitchStructure(final SwitchStatement node) {
    final List<SwitchCaseSection> switchStructure = new ArrayList<SwitchCaseSection>();
    SwitchCaseSection currentCase = new SwitchCaseSection();
    for (final Statement stmt : statements(node)) {
        if (stmt instanceof SwitchCase) {
            final SwitchCase swithCase = (SwitchCase) stmt;
            if (!currentCase.stmts.isEmpty()) {
                switchStructure.add(currentCase);
                currentCase = new SwitchCaseSection();
            }
            currentCase.existingCases.add(swithCase);
        } else {
            currentCase.stmts.add(stmt);
        }
    }
    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)

Example 4 with SwitchCase

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

the class ASTBuilder method case0.

/**
 * Builds a new {@link SwitchCase} instance.
 *
 * @param expr
 *            the case expression
 * @return a new switch case statement
 */
public SwitchCase case0(Expression expr) {
    final SwitchCase sc = ast.newSwitchCase();
    sc.setExpression(expr);
    return sc;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase)

Example 5 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)

Aggregations

SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)9 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)7 Statement (org.eclipse.jdt.core.dom.Statement)6 ArrayList (java.util.ArrayList)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)5 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)5 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)4 DoStatement (org.eclipse.jdt.core.dom.DoStatement)4 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)4 Expression (org.eclipse.jdt.core.dom.Expression)4 Name (org.eclipse.jdt.core.dom.Name)4 EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)3 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)3 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)2 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)2