Search in sources :

Example 16 with EnhancedForStatement

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

use of org.eclipse.jdt.core.dom.EnhancedForStatement in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final SingleVariableDeclaration it) {
    if ((((it.getParent() instanceof MethodDeclaration) || (it.getParent() instanceof CatchClause)) || (it.getParent() instanceof EnhancedForStatement))) {
        final Function1<IExtendedModifier, Boolean> _function = (IExtendedModifier it_1) -> {
            return Boolean.valueOf(it_1.isAnnotation());
        };
        this.appendModifiers(it, IterableExtensions.<IExtendedModifier>filter(Iterables.<IExtendedModifier>filter(it.modifiers(), IExtendedModifier.class), _function));
    } else {
        this.appendModifiers(it, it.modifiers());
    }
    it.getType().accept(this);
    this.appendExtraDimensions(it.getExtraDimensions());
    boolean _isVarargs = it.isVarargs();
    if (_isVarargs) {
        this.appendToBuffer("...");
    }
    this.appendSpaceToBuffer();
    it.getName().accept(this);
    Expression _initializer = it.getInitializer();
    boolean _tripleNotEquals = (_initializer != null);
    if (_tripleNotEquals) {
        this.appendToBuffer("=");
        it.getInitializer().accept(this);
    }
    return false;
}
Also used : InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) 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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) CatchClause(org.eclipse.jdt.core.dom.CatchClause) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 18 with EnhancedForStatement

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

the class NoLoopIterationRatherThanEmptyCheckCleanUp method getContainer.

private Expression getContainer(final List<Statement> statements) {
    ForStatement forStatement = ASTNodes.as(statements.get(0), ForStatement.class);
    EnhancedForStatement enhancedForStatement = ASTNodes.as(statements.get(0), EnhancedForStatement.class);
    if (forStatement != null) {
        ForLoopContent loopContent = ForLoops.iterateOverContainer(forStatement);
        if (loopContent != null) {
            return loopContent.getContainerVariable();
        }
    } else if (enhancedForStatement != null) {
        return enhancedForStatement.getExpression();
    }
    return null;
}
Also used : ForLoopContent(org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 19 with EnhancedForStatement

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

the class ExtractMethodAnalyzer method getParentLoopBody.

private Statement getParentLoopBody(ASTNode node) {
    Statement stmt = null;
    ASTNode start = node;
    while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
        start = start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt = ((ForStatement) start).getBody();
    } else if (start instanceof DoStatement) {
        stmt = ((DoStatement) start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt = ((WhileStatement) start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt = ((EnhancedForStatement) start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
        fEnclosingLoopLabel = labeledStatement.getLabel();
    }
    return stmt;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) 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) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) 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) 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 20 with EnhancedForStatement

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

the class ControlStatementsFix method createRemoveBlockFix.

public static ControlStatementsFix[] createRemoveBlockFix(CompilationUnit compilationUnit, ASTNode node) {
    if (!(node instanceof Statement)) {
        return null;
    }
    Statement statement = (Statement) node;
    if (statement instanceof Block) {
        Block block = (Block) statement;
        if (block.statements().size() != 1)
            return null;
        ASTNode parent = block.getParent();
        if (!(parent instanceof Statement))
            return null;
        statement = (Statement) parent;
    }
    if (statement instanceof IfStatement) {
        List<ControlStatementsFix> result = new ArrayList<ControlStatementsFix>();
        List<RemoveBlockOperation> removeAllList = new ArrayList<RemoveBlockOperation>();
        IfElseIterator iter = new IfElseIterator((IfStatement) statement);
        IfStatement item = null;
        while (iter.hasNext()) {
            item = iter.next();
            if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.THEN_STATEMENT_PROPERTY)) {
                RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.THEN_STATEMENT_PROPERTY);
                removeAllList.add(op);
                if (item == statement)
                    result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
            }
        }
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.ELSE_STATEMENT_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.ELSE_STATEMENT_PROPERTY);
            removeAllList.add(op);
            if (item == statement)
                result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeElseBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
        }
        if (removeAllList.size() > 1) {
            CompilationUnitRewriteOperation[] allConvert = removeAllList.toArray(new CompilationUnitRewriteOperation[removeAllList.size()]);
            result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfElseBlock_proposalDescription, compilationUnit, allConvert));
        }
        return result.toArray(new ControlStatementsFix[result.size()]);
    } else if (statement instanceof WhileStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, WhileStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, WhileStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof ForStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, ForStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, ForStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof EnhancedForStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, EnhancedForStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, EnhancedForStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof DoStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, DoStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, DoStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    }
    return null;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ArrayList(java.util.ArrayList) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Aggregations

EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)25 ForStatement (org.eclipse.jdt.core.dom.ForStatement)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 Statement (org.eclipse.jdt.core.dom.Statement)15 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)13 Block (org.eclipse.jdt.core.dom.Block)12 DoStatement (org.eclipse.jdt.core.dom.DoStatement)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)8 AST (org.eclipse.jdt.core.dom.AST)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)6 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)6 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)6 Expression (org.eclipse.jdt.core.dom.Expression)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 Type (org.eclipse.jdt.core.dom.Type)5 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)5