Search in sources :

Example 11 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.

the class GenericBuilderAdapterBuilder method getMethodSwitch.

protected List getMethodSwitch(String type) {
    List cases = switchMap.get(type);
    if (cases == null) {
        MethodDeclaration method = newMethodDeclaration("void", type, newSingleVariableDeclaration(newParameterizedType(EntityDescriptor.class.getName(), ast.newWildcardType()), "entityDesc"));
        SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("entityDesc", "getOrdinal"));
        method.getBody().statements().add(switchStm);
        addBodyDeclaration(method);
        switchMap.put(type, cases = switchStm.statements());
    }
    return cases;
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) List(java.util.List)

Example 12 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.

the class GenericBuilderAdapterBuilder method getDataMethodSwitch.

protected List getDataMethodSwitch(String type, String valueType) {
    List cases = (List) dataSwitchMap.get(valueType);
    if (cases == null) {
        MethodDeclaration method = newMethodDeclaration("void", "wEntity", newSingleVariableDeclaration(newParameterizedType(EntityDescriptor.class.getName(), ast.newWildcardType()), "entityDesc"), newSingleVariableDeclaration(valueType, "value"));
        SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("entityDesc", "getOrdinal"));
        method.getBody().statements().add(switchStm);
        addBodyDeclaration(method);
        dataSwitchMap.put(valueType, cases = switchStm.statements());
    }
    return cases;
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) List(java.util.List)

Example 13 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.

the class CompilationUnitBuilder method newSwitchStatement.

public SwitchStatement newSwitchStatement(Expression exp) {
    SwitchStatement stm = ast.newSwitchStatement();
    stm.setExpression(exp);
    return stm;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement)

Example 14 with SwitchStatement

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

the class VariableDeclarationRewrite method rewriteModifiers.

public static void rewriteModifiers(final VariableDeclarationStatement declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, ASTRewrite rewrite, final TextEditGroup group) {
    final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
    AST ast = declarationNode.getAST();
    List<VariableDeclarationFragment> fragments = declarationNode.fragments();
    Iterator<VariableDeclarationFragment> iter = fragments.iterator();
    ListRewrite blockRewrite = null;
    ASTNode parentStatement = declarationNode.getParent();
    if (parentStatement instanceof SwitchStatement) {
        blockRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
    } else if (parentStatement instanceof Block) {
        blockRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
    } else {
        // should not happen. VariableDeclaration's can not be in a control statement body
        Assert.isTrue(false);
    }
    VariableDeclarationFragment lastFragment = iter.next();
    ASTNode lastStatement = declarationNode;
    boolean modifiersModified = false;
    if (fragmentsToChange.contains(lastFragment)) {
        ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
        modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
        modifiersModified = true;
    }
    ListRewrite fragmentsRewrite = null;
    while (iter.hasNext()) {
        VariableDeclarationFragment currentFragment = iter.next();
        if (fragmentsToChange.contains(lastFragment) != fragmentsToChange.contains(currentFragment)) {
            VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement((VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment));
            newStatement.setType((Type) rewrite.createCopyTarget(declarationNode.getType()));
            ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
            if (fragmentsToChange.contains(currentFragment)) {
                modifierRewrite.copyAllAnnotations(declarationNode, group);
                int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
                modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
            } else {
                modifierRewrite.copyAllModifiers(declarationNode, group, modifiersModified);
            }
            blockRewrite.insertAfter(newStatement, lastStatement, group);
            fragmentsRewrite = rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
            lastStatement = newStatement;
        } else if (fragmentsRewrite != null) {
            ASTNode fragment0 = rewrite.createMoveTarget(currentFragment);
            fragmentsRewrite.insertLast(fragment0, group);
        }
        lastFragment = currentFragment;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 15 with SwitchStatement

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

the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method visit.

@SuppressWarnings("deprecation")
@Override
public boolean visit(final SwitchStatement visited) {
    List<?> statements = visited.statements();
    if (statements.isEmpty()) {
        return true;
    }
    Set<SimpleName> previousVarIds = new HashSet<>();
    Set<SimpleName> caseVarIds = new HashSet<>();
    List<Pair<List<Expression>, List<Statement>>> switchStructure = new ArrayList<>();
    List<Expression> caseExprs = new ArrayList<>();
    List<Statement> caseStatements = new ArrayList<>();
    boolean isPreviousStmtACase = true;
    int caseIndexWithDefault = -1;
    for (Object object : statements) {
        Statement statement = (Statement) object;
        if (statement instanceof SwitchCase) {
            if (!isPreviousStmtACase) {
                if (switchStructure.size() > 2) {
                    return true;
                }
                previousVarIds.addAll(caseVarIds);
                caseVarIds.clear();
                switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
                caseExprs = new ArrayList<>();
                caseStatements = new ArrayList<>();
            }
            if (((SwitchCase) statement).isDefault()) {
                caseIndexWithDefault = switchStructure.size();
            } else {
                caseExprs.add(((SwitchCase) statement).getExpression());
            }
            isPreviousStmtACase = true;
        } else {
            VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(previousVarIds, false);
            varOccurrenceVisitor.traverseNodeInterruptibly(statement);
            if (varOccurrenceVisitor.isVarConflicting()) {
                return true;
            }
            caseVarIds.addAll(ASTNodes.getLocalVariableIdentifiers(statement, false));
            caseStatements.add(statement);
            isPreviousStmtACase = false;
        }
    }
    switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
    if (caseIndexWithDefault != -1) {
        Pair<List<Expression>, List<Statement>> caseWithDefault = switchStructure.remove(caseIndexWithDefault);
        switchStructure.add(caseWithDefault);
    }
    if (switchStructure.size() > 2 || !switchStructure.get(switchStructure.size() - 1).getFirst().isEmpty() && !ASTNodes.isPassive(visited.getExpression())) {
        return true;
    }
    List<BreakStatement> overBreaks = new ArrayList<>();
    for (int i = 0; i < switchStructure.size(); i++) {
        Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
        if (!caseStructure.getSecond().isEmpty()) {
            Statement lastStatement = caseStructure.getSecond().get(caseStructure.getSecond().size() - 1);
            if (i < switchStructure.size() - 1 && !ASTNodes.fallsThrough(lastStatement)) {
                return true;
            }
            BreakStatement breakStatement = ASTNodes.as(lastStatement, BreakStatement.class);
            if (breakStatement != null && breakStatement.getLabel() == null) {
                caseStructure.getSecond().remove(caseStructure.getSecond().size() - 1);
            }
        } else if (i < switchStructure.size() - 1) {
            return true;
        }
    }
    for (Pair<List<Expression>, List<Statement>> caseStructure : switchStructure) {
        for (Statement oneStatement : caseStructure.getSecond()) {
            BreakVisitor breakVisitor = new BreakVisitor();
            breakVisitor.traverseNodeInterruptibly(oneStatement);
            if (!breakVisitor.canBeRefactored()) {
                return true;
            }
            overBreaks.addAll(breakVisitor.getBreaks());
        }
    }
    replaceBySwitch(visited, switchStructure, caseIndexWithDefault, overBreaks);
    return false;
}
Also used : 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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) VarConflictVisitor(org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.autorefactor.util.Pair)

Aggregations

SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)35 Statement (org.eclipse.jdt.core.dom.Statement)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 DoStatement (org.eclipse.jdt.core.dom.DoStatement)12 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)11 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)10 Expression (org.eclipse.jdt.core.dom.Expression)9 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Block (org.eclipse.jdt.core.dom.Block)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)6 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 TryStatement (org.eclipse.jdt.core.dom.TryStatement)5