Search in sources :

Example 1 with SwitchStatement

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

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

the class CallInliner method initializeInsertionPoint.

private void initializeInsertionPoint(int nos) {
    fInsertionIndex = -1;
    fNeedsStatement = false;
    // if we have a constructor invocation the invocation itself is already a statement
    ASTNode parentStatement = fInvocation instanceof Statement ? fInvocation : ASTNodes.getParent(fInvocation, Statement.class);
    if (parentStatement == null)
        return;
    ASTNode container = parentStatement.getParent();
    int type = container.getNodeType();
    if (type == ASTNode.BLOCK) {
        Block block = (Block) container;
        fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
        fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
    } else if (type == ASTNode.SWITCH_STATEMENT) {
        SwitchStatement switchStatement = (SwitchStatement) container;
        fListRewrite = fRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
        fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
    } else if (isControlStatement(container) || type == ASTNode.LABELED_STATEMENT) {
        fNeedsStatement = true;
        if (nos > 1 || needsBlockAroundDanglingIf()) {
            Block block = fInvocation.getAST().newBlock();
            fInsertionIndex = 0;
            Statement currentStatement = null;
            switch(type) {
                case ASTNode.LABELED_STATEMENT:
                    currentStatement = ((LabeledStatement) container).getBody();
                    break;
                case ASTNode.FOR_STATEMENT:
                    currentStatement = ((ForStatement) container).getBody();
                    break;
                case ASTNode.ENHANCED_FOR_STATEMENT:
                    currentStatement = ((EnhancedForStatement) container).getBody();
                    break;
                case ASTNode.WHILE_STATEMENT:
                    currentStatement = ((WhileStatement) container).getBody();
                    break;
                case ASTNode.DO_STATEMENT:
                    currentStatement = ((DoStatement) container).getBody();
                    break;
                case ASTNode.IF_STATEMENT:
                    IfStatement node = (IfStatement) container;
                    Statement thenPart = node.getThenStatement();
                    if (fTargetNode == thenPart || ASTNodes.isParent(fTargetNode, thenPart)) {
                        currentStatement = thenPart;
                    } else {
                        currentStatement = node.getElseStatement();
                    }
                    break;
            }
            Assert.isNotNull(currentStatement);
            fRewrite.replace(currentStatement, block, null);
            fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
            // The method to be inlined is not the body of the control statement.
            if (currentStatement != fTargetNode) {
                fListRewrite.insertLast(fRewrite.createCopyTarget(currentStatement), null);
            } else {
                // We can't replace a copy with something else. So we
                // have to insert all statements to be inlined.
                fTargetNode = null;
            }
        }
    }
// We only insert one new statement or we delete the existing call.
// So there is no need to have an insertion index.
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 3 with SwitchStatement

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

the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.

private void addLocalDeclarationSplit(ASTRewrite rewrite) {
    VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
    ASTNode parentStatement = tempDeclarationStatement.getParent();
    ListRewrite listRewrite;
    if (parentStatement instanceof SwitchStatement) {
        listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
    } else if (parentStatement instanceof Block) {
        listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
    } else {
        // should not happen. VariableDeclaration's can not be in a control statement body
        listRewrite = null;
        Assert.isTrue(false);
    }
    int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
    Assert.isTrue(statementIndex != -1);
    Statement newStatement = createNewAssignmentStatement(rewrite);
    List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
    int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
    Assert.isTrue(fragmentIndex != -1);
    if (fragments.size() == 1) {
        rewrite.replace(tempDeclarationStatement, newStatement, null);
        return;
    }
    for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
        VariableDeclarationFragment fragment = fragments.get(i1);
        rewrite.remove(fragment, null);
    }
    if (fragmentIndex == 0)
        rewrite.remove(tempDeclarationStatement, null);
    Assert.isTrue(tempHasInitializer());
    listRewrite.insertAt(newStatement, statementIndex + 1, null);
    if (fragmentIndex + 1 < fragments.size()) {
        VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
        VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
        VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
        Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
        statement.setType(type);
        List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
        if (modifiers.size() > 0) {
            ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
            ASTNode firstModifier = (ASTNode) modifiers.get(0);
            ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
            ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
            statement.modifiers().add(modifiersCopy);
        }
        for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
            VariableDeclarationFragment fragment = fragments.get(i);
            VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
            statement.fragments().add(fragmentCopy);
        }
        listRewrite.insertAt(statement, statementIndex + 2, null);
    }
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) Statement(org.eclipse.jdt.core.dom.Statement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 4 with SwitchStatement

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

the class SwitchRefactoring method replaceWithSwitchStmt.

private void replaceWithSwitchStmt(final IfStatement node, final Expression switchExpr, final List<SwitchCaseSection> cases, final Statement remainingStmt) {
    final ASTBuilder b = ctx.getASTBuilder();
    final SwitchStatement switchStmt = b.switch0(b.move(switchExpr));
    for (final SwitchCaseSection aCase : cases) {
        addCaseWithStmts(switchStmt, aCase.constantExprs, aCase.stmts);
    }
    if (remainingStmt != null) {
        addDefaultWithStmts(switchStmt, asList(remainingStmt));
    }
    ctx.getRefactorings().replace(node, switchStmt);
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 5 with SwitchStatement

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

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