Search in sources :

Example 1 with ForStatement

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

the class GenerateForLoopAssistProposal method generateForRewrite.

/**
	 * Helper to generate an index based <code>for</code> loop to iterate over an array.
	 * 
	 * @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
	 * @return an applicable {@link ASTRewrite} instance
	 */
private ASTRewrite generateForRewrite(AST ast) {
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ForStatement loopStatement = ast.newForStatement();
    //$NON-NLS-1$
    SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "int", null, true);
    loopStatement.initializers().add(getForInitializer(ast, loopVariableName));
    FieldAccess getArrayLengthExpression = ast.newFieldAccess();
    getArrayLengthExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
    //$NON-NLS-1$
    getArrayLengthExpression.setName(ast.newSimpleName("length"));
    loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), getArrayLengthExpression, InfixExpression.Operator.LESS));
    loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));
    Block forLoopBody = ast.newBlock();
    forLoopBody.statements().add(ast.newExpressionStatement(getForBodyAssignment(rewrite, loopVariableName)));
    forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
    loopStatement.setBody(forLoopBody);
    rewrite.replace(fCurrentNode, loopStatement, null);
    return rewrite;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 2 with ForStatement

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

the class GenerateForLoopAssistProposal method generateIteratorBasedForRewrite.

/**
	 * Helper to generate an iterator based <code>for</code> loop to iterate over an
	 * {@link Iterable}.
	 * 
	 * @param ast the {@link AST} instance to rewrite the loop to
	 * @return the complete {@link ASTRewrite} object
	 */
private ASTRewrite generateIteratorBasedForRewrite(AST ast) {
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ForStatement loopStatement = ast.newForStatement();
    ITypeBinding loopOverType = extractElementType(ast);
    //$NON-NLS-1$
    SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true);
    loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName));
    MethodInvocation loopExpression = ast.newMethodInvocation();
    //$NON-NLS-1$
    loopExpression.setName(ast.newSimpleName("hasNext"));
    SimpleName expressionName = ast.newSimpleName(loopVariableName.getIdentifier());
    addLinkedPosition(rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier());
    loopExpression.setExpression(expressionName);
    loopStatement.setExpression(loopExpression);
    Block forLoopBody = ast.newBlock();
    Assignment assignResolvedVariable = getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName);
    forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable));
    forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
    loopStatement.setBody(forLoopBody);
    rewrite.replace(fCurrentNode, loopStatement, null);
    return rewrite;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 3 with ForStatement

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

the class ConvertForLoopQuickFixTest method satisfiesPrecondition.

private boolean satisfiesPrecondition(ICompilationUnit cu) {
    ForStatement statement = getForStatement(cu);
    ConvertLoopOperation op = new ConvertForLoopOperation(statement);
    return op.satisfiesPreconditions().isOK();
}
Also used : ConvertForLoopOperation(org.eclipse.jdt.internal.corext.fix.ConvertForLoopOperation) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ConvertLoopOperation(org.eclipse.jdt.internal.corext.fix.ConvertLoopOperation)

Example 4 with ForStatement

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

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

the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.

private static boolean isReferringToLocalVariableFromFor(Expression expression) {
    ASTNode current = expression;
    ASTNode parent = current.getParent();
    while (parent != null && !(parent instanceof BodyDeclaration)) {
        if (parent instanceof ForStatement) {
            ForStatement forStmt = (ForStatement) parent;
            if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
                List<Expression> initializers = forStmt.initializers();
                if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
                    List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
                    ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
                    expression.accept(checker);
                    if (checker.isReferringToForVariable())
                        return true;
                }
            }
        }
        current = parent;
        parent = current.getParent();
    }
    return false;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

ForStatement (org.eclipse.jdt.core.dom.ForStatement)25 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 Statement (org.eclipse.jdt.core.dom.Statement)13 Block (org.eclipse.jdt.core.dom.Block)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 DoStatement (org.eclipse.jdt.core.dom.DoStatement)11 IfStatement (org.eclipse.jdt.core.dom.IfStatement)9 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9 Expression (org.eclipse.jdt.core.dom.Expression)8 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 AST (org.eclipse.jdt.core.dom.AST)5 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)5 Assignment (org.eclipse.jdt.core.dom.Assignment)4 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)4 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)4 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)4 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)4