Search in sources :

Example 1 with WhileStatement

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

the class ExtractMethodRefactoring method replaceBranches.

private void replaceBranches(final CompilationUnitChange result) {
    ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
    for (int i = 0; i < selectedNodes.length; i++) {
        ASTNode astNode = selectedNodes[i];
        astNode.accept(new ASTVisitor() {

            private LinkedList<String> fOpenLoopLabels = new LinkedList<String>();

            private void registerLoopLabel(Statement node) {
                String identifier;
                if (node.getParent() instanceof LabeledStatement) {
                    LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
                    identifier = labeledStatement.getLabel().getIdentifier();
                } else {
                    identifier = null;
                }
                fOpenLoopLabels.add(identifier);
            }

            @Override
            public boolean visit(ForStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(ForStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(WhileStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(WhileStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(EnhancedForStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(EnhancedForStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(DoStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(DoStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public void endVisit(ContinueStatement node) {
                final SimpleName label = node.getLabel();
                if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
                    TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
                    result.addTextEditGroup(description);
                    ReturnStatement rs = fAST.newReturnStatement();
                    IVariableBinding returnValue = fAnalyzer.getReturnValue();
                    if (returnValue != null) {
                        rs.setExpression(fAST.newSimpleName(getName(returnValue)));
                    }
                    fRewriter.replace(node, rs, description);
                }
            }
        });
    }
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) 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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) LinkedList(java.util.LinkedList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TextEditGroup(org.eclipse.text.edits.TextEditGroup) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 2 with WhileStatement

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

the class SourceProvider method isDangligIf.

public boolean isDangligIf() {
    List<Statement> statements = fDeclaration.getBody().statements();
    if (statements.size() != 1)
        return false;
    ASTNode p = statements.get(0);
    while (true) {
        if (p instanceof IfStatement) {
            return ((IfStatement) p).getElseStatement() == null;
        } else {
            ChildPropertyDescriptor childD;
            if (p instanceof WhileStatement) {
                childD = WhileStatement.BODY_PROPERTY;
            } else if (p instanceof ForStatement) {
                childD = ForStatement.BODY_PROPERTY;
            } else if (p instanceof EnhancedForStatement) {
                childD = EnhancedForStatement.BODY_PROPERTY;
            } else if (p instanceof DoStatement) {
                childD = DoStatement.BODY_PROPERTY;
            } else if (p instanceof LabeledStatement) {
                childD = LabeledStatement.BODY_PROPERTY;
            } else {
                return false;
            }
            Statement body = (Statement) p.getStructuralProperty(childD);
            if (body instanceof Block) {
                return false;
            } else {
                p = body;
            }
        }
    }
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) ChildPropertyDescriptor(org.eclipse.jdt.core.dom.ChildPropertyDescriptor) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 3 with WhileStatement

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

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

the class QuickAssistProcessor method getAddBlockProposals.

private static boolean getAddBlockProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof Statement)) {
        return false;
    }
    /*
		 * only show the quick assist when the selection is of the control statement keywords (if, else, while,...)
		 * but not inside the statement or the if expression.
		 */
    if (!isControlStatementWithBlock(node) && isControlStatementWithBlock(node.getParent())) {
        int statementStart = node.getStartPosition();
        int statementEnd = statementStart + node.getLength();
        int offset = context.getSelectionOffset();
        int length = context.getSelectionLength();
        if (length == 0) {
            if (offset != statementEnd) {
                // cursor at end
                return false;
            }
        } else {
            if (offset > statementStart || offset + length < statementEnd) {
                // statement selected
                return false;
            }
        }
        node = node.getParent();
    }
    StructuralPropertyDescriptor childProperty = null;
    ASTNode child = null;
    switch(node.getNodeType()) {
        case ASTNode.IF_STATEMENT:
            ASTNode then = ((IfStatement) node).getThenStatement();
            ASTNode elseStatement = ((IfStatement) node).getElseStatement();
            if ((then instanceof Block) && (elseStatement instanceof Block || elseStatement == null)) {
                break;
            }
            int thenEnd = then.getStartPosition() + then.getLength();
            int selectionEnd = context.getSelectionOffset() + context.getSelectionLength();
            if (!(then instanceof Block)) {
                if (selectionEnd <= thenEnd) {
                    childProperty = IfStatement.THEN_STATEMENT_PROPERTY;
                    child = then;
                    break;
                } else if (elseStatement != null && selectionEnd < elseStatement.getStartPosition()) {
                    // find out if we are before or after the 'else' keyword
                    try {
                        TokenScanner scanner = new TokenScanner(context.getCompilationUnit());
                        int elseTokenStart = scanner.getNextStartOffset(thenEnd, true);
                        if (selectionEnd < elseTokenStart) {
                            childProperty = IfStatement.THEN_STATEMENT_PROPERTY;
                            child = then;
                            break;
                        }
                    } catch (CoreException e) {
                    // ignore
                    }
                }
            }
            if (elseStatement != null && !(elseStatement instanceof Block) && context.getSelectionOffset() >= thenEnd) {
                childProperty = IfStatement.ELSE_STATEMENT_PROPERTY;
                child = elseStatement;
            }
            break;
        case ASTNode.WHILE_STATEMENT:
            ASTNode whileBody = ((WhileStatement) node).getBody();
            if (!(whileBody instanceof Block)) {
                childProperty = WhileStatement.BODY_PROPERTY;
                child = whileBody;
            }
            break;
        case ASTNode.FOR_STATEMENT:
            ASTNode forBody = ((ForStatement) node).getBody();
            if (!(forBody instanceof Block)) {
                childProperty = ForStatement.BODY_PROPERTY;
                child = forBody;
            }
            break;
        case ASTNode.ENHANCED_FOR_STATEMENT:
            ASTNode enhancedForBody = ((EnhancedForStatement) node).getBody();
            if (!(enhancedForBody instanceof Block)) {
                childProperty = EnhancedForStatement.BODY_PROPERTY;
                child = enhancedForBody;
            }
            break;
        case ASTNode.DO_STATEMENT:
            ASTNode doBody = ((DoStatement) node).getBody();
            if (!(doBody instanceof Block)) {
                childProperty = DoStatement.BODY_PROPERTY;
                child = doBody;
            }
            break;
        default:
    }
    if (child == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    AST ast = node.getAST();
    {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ASTNode childPlaceholder = rewrite.createMoveTarget(child);
        Block replacingBody = ast.newBlock();
        replacingBody.statements().add(childPlaceholder);
        rewrite.set(node, childProperty, replacingBody, null);
        String label;
        if (childProperty == IfStatement.THEN_STATEMENT_PROPERTY) {
            label = CorrectionMessages.QuickAssistProcessor_replacethenwithblock_description;
        } else if (childProperty == IfStatement.ELSE_STATEMENT_PROPERTY) {
            label = CorrectionMessages.QuickAssistProcessor_replaceelsewithblock_description;
        } else {
            label = CorrectionMessages.QuickAssistProcessor_replacebodywithblock_description;
        }
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_BLOCK);
        proposal.setCommandId(ADD_BLOCK_ID);
        proposal.setEndPosition(rewrite.track(child));
        resultingCollections.add(proposal);
    }
    if (node.getNodeType() == ASTNode.IF_STATEMENT) {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        while (node.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
            node = node.getParent();
        }
        boolean missingBlockFound = false;
        boolean foundElse = false;
        IfStatement ifStatement;
        Statement thenStatment;
        Statement elseStatment;
        do {
            ifStatement = (IfStatement) node;
            thenStatment = ifStatement.getThenStatement();
            elseStatment = ifStatement.getElseStatement();
            if (!(thenStatment instanceof Block)) {
                ASTNode childPlaceholder1 = rewrite.createMoveTarget(thenStatment);
                Block replacingBody1 = ast.newBlock();
                replacingBody1.statements().add(childPlaceholder1);
                rewrite.set(ifStatement, IfStatement.THEN_STATEMENT_PROPERTY, replacingBody1, null);
                if (thenStatment != child) {
                    missingBlockFound = true;
                }
            }
            if (elseStatment != null) {
                foundElse = true;
            }
            node = elseStatment;
        } while (elseStatment instanceof IfStatement);
        if (elseStatment != null && !(elseStatment instanceof Block)) {
            ASTNode childPlaceholder2 = rewrite.createMoveTarget(elseStatment);
            Block replacingBody2 = ast.newBlock();
            replacingBody2.statements().add(childPlaceholder2);
            rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, replacingBody2, null);
            if (elseStatment != child) {
                missingBlockFound = true;
            }
        }
        if (missingBlockFound && foundElse) {
            String label = CorrectionMessages.QuickAssistProcessor_replacethenelsewithblock_description;
            //				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_IF_ELSE_TO_BLOCK);
            resultingCollections.add(proposal);
        }
    }
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) CoreException(org.eclipse.core.runtime.CoreException) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 5 with WhileStatement

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

the class AdvancedQuickAssistProcessor method getCastAndAssignIfStatementProposals.

private static boolean getCastAndAssignIfStatementProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (node instanceof IfStatement) {
        node = ((IfStatement) node).getExpression();
    } else if (node instanceof WhileStatement) {
        node = ((WhileStatement) node).getExpression();
    } else if (node instanceof Block) {
        List<Statement> statements = ((Block) node).statements();
        if (statements.size() > 0) {
            if (context.getSelectionOffset() > statements.get(0).getStartPosition()) {
                return false;
            }
        }
        ASTNode parent = node.getParent();
        Expression expression = null;
        if (parent instanceof IfStatement) {
            expression = ((IfStatement) parent).getExpression();
        } else if (parent instanceof WhileStatement) {
            expression = ((WhileStatement) parent).getExpression();
        } else {
            return false;
        }
        if (expression instanceof InstanceofExpression) {
            node = expression;
        } else {
            final ArrayList<InstanceofExpression> nodes = new ArrayList<InstanceofExpression>();
            expression.accept(new ASTVisitor() {

                @Override
                public boolean visit(InstanceofExpression instanceofExpression) {
                    nodes.add(instanceofExpression);
                    return false;
                }
            });
            if (nodes.size() != 1) {
                return false;
            }
            node = nodes.get(0);
        }
    } else {
        while (node != null && !(node instanceof InstanceofExpression) && !(node instanceof Statement)) {
            node = node.getParent();
        }
    }
    if (!(node instanceof InstanceofExpression)) {
        return false;
    }
    InstanceofExpression expression = (InstanceofExpression) node;
    // test that we are the expression of a 'while' or 'if'
    while (node.getParent() instanceof Expression) {
        node = node.getParent();
    }
    StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
    boolean negated = isNegated(expression);
    Statement body = null;
    ASTNode insertionPosition = null;
    if (negated) {
        insertionPosition = node.getParent();
        if (locationInParent == IfStatement.EXPRESSION_PROPERTY) {
            body = ((IfStatement) node.getParent()).getElseStatement();
            if (body != null) {
                negated = false;
            }
        }
        if (body == null && insertionPosition.getParent() instanceof Block) {
            body = (Statement) insertionPosition.getParent();
        }
    } else {
        if (locationInParent == IfStatement.EXPRESSION_PROPERTY) {
            body = ((IfStatement) node.getParent()).getThenStatement();
        } else if (locationInParent == WhileStatement.EXPRESSION_PROPERTY) {
            body = ((WhileStatement) node.getParent()).getBody();
        }
    }
    if (body == null) {
        return false;
    }
    Type originalType = expression.getRightOperand();
    if (originalType.resolveBinding() == null) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //$NON-NLS-1$
    final String KEY_NAME = "name";
    //$NON-NLS-1$
    final String KEY_TYPE = "type";
    //
    AST ast = expression.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ICompilationUnit cu = context.getCompilationUnit();
    // prepare correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_castAndAssign;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CAST_AND_ASSIGN);
    // prepare possible variable names
    List<String> excludedNames = Arrays.asList(ASTResolving.getUsedVariableNames(body));
    String[] varNames = suggestLocalVariableNames(cu, originalType.resolveBinding(), excludedNames);
    for (int i = 0; i < varNames.length; i++) {
        proposal.addLinkedPositionProposal(KEY_NAME, varNames[i], null);
    }
    CastExpression castExpression = ast.newCastExpression();
    castExpression.setExpression((Expression) rewrite.createCopyTarget(expression.getLeftOperand()));
    castExpression.setType((Type) ASTNode.copySubtree(ast, originalType));
    // prepare new variable declaration
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName(varNames[0]));
    vdf.setInitializer(castExpression);
    // prepare new variable declaration statement
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    vds.setType((Type) ASTNode.copySubtree(ast, originalType));
    // add new variable declaration statement
    if (negated) {
        ListRewrite listRewriter = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
        listRewriter.insertAfter(vds, insertionPosition, null);
    } else {
        if (body instanceof Block) {
            ListRewrite listRewriter = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
            listRewriter.insertAt(vds, 0, null);
        } else {
            Block newBlock = ast.newBlock();
            List<Statement> statements = newBlock.statements();
            statements.add(vds);
            statements.add((Statement) rewrite.createMoveTarget(body));
            rewrite.replace(body, newBlock, null);
        }
    }
    // setup linked positions
    proposal.addLinkedPosition(rewrite.track(vdf.getName()), true, KEY_NAME);
    proposal.addLinkedPosition(rewrite.track(vds.getType()), false, KEY_TYPE);
    proposal.addLinkedPosition(rewrite.track(castExpression.getType()), false, KEY_TYPE);
    // set cursor after expression statement
    proposal.setEndPosition(rewrite.track(vds));
    // add correction proposal
    resultingCollections.add(proposal);
    return true;
}
Also used : ArrayList(java.util.ArrayList) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) 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) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) Block(org.eclipse.jdt.core.dom.Block) CastExpression(org.eclipse.jdt.core.dom.CastExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Aggregations

EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 DoStatement (org.eclipse.jdt.core.dom.DoStatement)11 Statement (org.eclipse.jdt.core.dom.Statement)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Block (org.eclipse.jdt.core.dom.Block)10 IfStatement (org.eclipse.jdt.core.dom.IfStatement)9 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)5 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)5 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)5 AST (org.eclipse.jdt.core.dom.AST)4 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)4 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3