Search in sources :

Example 26 with ForStatement

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

the class ConvertForLoopOperation method convert.

@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, TextEditGroup group, LinkedProposalModel positionGroups) throws CoreException {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ImportRewrite importRewrite = cuRewrite.getImportRewrite();
    ForStatement forStatement = getForStatement();
    IJavaProject javaProject = ((CompilationUnit) forStatement.getRoot()).getJavaElement().getJavaProject();
    String[] proposals = getVariableNameProposals(fArrayAccess.resolveTypeBinding(), javaProject);
    String parameterName;
    if (fElementDeclaration != null) {
        parameterName = fElementDeclaration.getName().getIdentifier();
    } else {
        parameterName = proposals[0];
    }
    LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(parameterName, true);
    if (fElementDeclaration != null)
        pg.addProposal(parameterName, null, 10);
    for (int i = 0; i < proposals.length; i++) {
        pg.addProposal(proposals[i], null, 10);
    }
    AST ast = forStatement.getAST();
    EnhancedForStatement result = ast.newEnhancedForStatement();
    SingleVariableDeclaration parameterDeclaration = createParameterDeclaration(parameterName, fElementDeclaration, fArrayAccess, forStatement, importRewrite, rewrite, group, pg, fMakeFinal);
    result.setParameter(parameterDeclaration);
    result.setExpression((Expression) rewrite.createCopyTarget(fArrayAccess));
    convertBody(forStatement.getBody(), fIndexBinding, fArrayBinding, parameterName, rewrite, group, pg);
    result.setBody(getBody(cuRewrite, group, positionGroups));
    positionGroups.setEndPosition(rewrite.track(result));
    return result;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 27 with ForStatement

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

the class SourceProvider method isSingleControlStatementWithoutBlock.

private boolean isSingleControlStatementWithoutBlock() {
    List<Statement> statements = fDeclaration.getBody().statements();
    int size = statements.size();
    if (size != 1)
        return false;
    Statement statement = statements.get(size - 1);
    int nodeType = statement.getNodeType();
    if (nodeType == ASTNode.IF_STATEMENT) {
        IfStatement ifStatement = (IfStatement) statement;
        return !(ifStatement.getThenStatement() instanceof Block) && !(ifStatement.getElseStatement() instanceof Block);
    } else if (nodeType == ASTNode.FOR_STATEMENT) {
        return !(((ForStatement) statement).getBody() instanceof Block);
    } else if (nodeType == ASTNode.WHILE_STATEMENT) {
        return !(((WhileStatement) statement).getBody() instanceof Block);
    }
    return false;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) 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) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 28 with ForStatement

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

the class AllInOneMethodRatherThanLoopRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final ForLoopContent loopContent = iterateOverContainer(node);
    final List<Statement> stmts = asList(node.getBody());
    if (loopContent != null && loopContent.getLoopVariable() != null && stmts.size() == 1) {
        final SimpleName loopVariable = (SimpleName) loopContent.getLoopVariable();
        final IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
        // As we replace only one, there should be no more than one occurrence
        if (getVariableUseCount(loopVariableName, node.getBody()) == 1) {
            final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
            switch(loopContent.getContainerType()) {
                case COLLECTION:
                    if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
                        return maybeReplaceWithCollectionMethod(node, loopContent, "addAll", mi);
                    } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                        return maybeReplaceWithCollectionMethod(node, loopContent, "removeAll", mi);
                    }
                    break;
                case ARRAY:
                    if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), loopContent.getContainerVariable())) {
                        final Expression addArg0 = arg0(mi);
                        final ArrayAccess aa = as(addArg0, ArrayAccess.class);
                        if (isSameVariable(loopContent, aa)) {
                            replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
                            return DO_NOT_VISIT_SUBTREE;
                        }
                    }
                    break;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ForLoopContent(org.autorefactor.refactoring.ForLoopHelper.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

ForStatement (org.eclipse.jdt.core.dom.ForStatement)28 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)24 Statement (org.eclipse.jdt.core.dom.Statement)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)15 Block (org.eclipse.jdt.core.dom.Block)14 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)14 DoStatement (org.eclipse.jdt.core.dom.DoStatement)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)11 Expression (org.eclipse.jdt.core.dom.Expression)10 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)7 AST (org.eclipse.jdt.core.dom.AST)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 Assignment (org.eclipse.jdt.core.dom.Assignment)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)5