Search in sources :

Example 11 with ForStatement

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

the class CleanCodeRatherThanSemicolonRefactoring method visit.

@Override
public boolean visit(EmptyStatement node) {
    ASTNode parent = node.getParent();
    if (parent instanceof Block) {
        this.ctx.getRefactorings().remove(node);
        return DO_NOT_VISIT_SUBTREE;
    }
    parent = getParentIgnoring(node, Block.class);
    if (parent instanceof IfStatement) {
        IfStatement is = (IfStatement) parent;
        List<Statement> thenStmts = asList(is.getThenStatement());
        List<Statement> elseStmts = asList(is.getElseStatement());
        boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
        boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
        if (thenIsEmptyStmt && elseIsEmptyStmt) {
            this.ctx.getRefactorings().remove(parent);
            return DO_NOT_VISIT_SUBTREE;
        } else if (thenIsEmptyStmt && is.getElseStatement() == null) {
            this.ctx.getRefactorings().remove(is);
            return DO_NOT_VISIT_SUBTREE;
        } else if (elseIsEmptyStmt) {
            this.ctx.getRefactorings().remove(is.getElseStatement());
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (parent instanceof TryStatement) {
        TryStatement ts = (TryStatement) parent;
        return maybeRemoveEmptyStmtBody(node, ts, ts.getBody());
    } else if (parent instanceof EnhancedForStatement) {
        EnhancedForStatement efs = (EnhancedForStatement) parent;
        return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
    } else if (parent instanceof ForStatement) {
        ForStatement fs = (ForStatement) parent;
        return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
    } else if (parent instanceof WhileStatement) {
        WhileStatement ws = (WhileStatement) parent;
        return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 12 with ForStatement

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

the class HotSpotIntrinsicedAPIsRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final SystemArrayCopyParams params = new SystemArrayCopyParams();
    collectUniqueIndex(node, params);
    final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
    final List<Statement> stmts = asList(node.getBody());
    if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
        collectLength(node.getExpression(), incrementedIdx, params);
        final Assignment as = asExpression(stmts.get(0), Assignment.class);
        if (hasOperator(as, ASSIGN)) {
            final Expression lhs = as.getLeftHandSide();
            final Expression rhs = as.getRightHandSide();
            if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
                final ArrayAccess aaLHS = (ArrayAccess) lhs;
                final ArrayAccess aaRHS = (ArrayAccess) rhs;
                params.destArrayExpr = aaLHS.getArray();
                params.srcArrayExpr = aaRHS.getArray();
                if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
                    params.destPos = calcIndex(aaLHS.getIndex(), params);
                    params.srcPos = calcIndex(aaRHS.getIndex(), params);
                    return replaceWithSystemArrayCopyCloneAll(node, params);
                }
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 13 with ForStatement

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

the class ReduceVariableScopeRefactoring method replace.

private void replace(VariableAccess varDecl, VariableAccess varAccess) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final AST ast = b.getAST();
    final ASTNode scope = varAccess.getScope();
    final Name varName = varAccess.getVariableName();
    final Type varType = getType(varDecl.getVariableName().getParent());
    if (scope instanceof Block) {
        final List<Statement> stmts = statements((Block) scope);
        for (int i = 0; i < stmts.size(); i++) {
            final Statement stmt = stmts.get(i);
            // FIXME i=0
            final Expression parentExpr = getAncestor(varName, Expression.class);
            // FIXME i=0
            final Statement parentStmt = getAncestor(parentExpr, Statement.class);
            if (stmt.equals(parentStmt)) {
                final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName);
                final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
                vds.setType(varType);
                this.ctx.getRefactorings().replace(stmt, vds);
                break;
            }
        }
    } else if (scope instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) scope;
        final EnhancedForStatement newEfs = b.copy(efs);
        newEfs.setParameter(b.copy(efs.getParameter()));
        newEfs.setExpression(b.copy(efs.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(efs.getBody(), parentStmt)) {
            newEfs.setBody(copy(efs.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(efs, newEfs);
    } else if (scope instanceof ForStatement) {
        final ForStatement fs = (ForStatement) scope;
        final ForStatement newFs = b.copy(fs);
        final List<Expression> initializers = initializers(newFs);
        if (initializers.size() == 1) {
            final Expression init = initializers.remove(0);
            final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName);
            final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf);
            vde.setType(varType);
            initializers.add(vde);
            this.ctx.getRefactorings().replace(fs, newFs);
        // TODO JNR
        // if (equalNotNull(fs.getBody(), parentStmt)) {
        // newFs.setBody(copy(fs.getBody()));
        // }
        } else {
            throw new NotImplementedException(scope, "for more than one initializer in for loop.");
        }
    } else if (scope instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) scope;
        final WhileStatement newWs = ast.newWhileStatement();
        newWs.setExpression(b.copy(ws.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(ws.getBody(), parentStmt)) {
            newWs.setBody(copy(ws.getBody(), varName));
        }
        this.ctx.getRefactorings().replace(ws, newWs);
    } else if (scope instanceof IfStatement) {
        final IfStatement is = (IfStatement) scope;
        final IfStatement newIs = ast.newIfStatement();
        newIs.setExpression(b.copy(is.getExpression()));
        final Statement parentStmt = getAncestor(varName, Statement.class);
        if (equalNotNull(is.getThenStatement(), parentStmt)) {
            newIs.setThenStatement(copy(is.getThenStatement(), varName));
            if (is.getElseStatement() != null) {
                newIs.setElseStatement(b.copy(is.getElseStatement()));
            }
            this.ctx.getRefactorings().replace(is, newIs);
        } else if (equalNotNull(is.getElseStatement(), parentStmt)) {
            if (is.getThenStatement() != null) {
                newIs.setThenStatement(b.copy(is.getThenStatement()));
            }
            newIs.setElseStatement(copy(is.getElseStatement(), varName));
            this.ctx.getRefactorings().replace(is, newIs);
        } else {
            throw new IllegalStateException(is, "Parent statement should be inside the then or else statement of this if statement: " + is);
        }
    } else {
        throw new NotImplementedException(scope);
    }
}
Also used : IllegalStateException(org.autorefactor.util.IllegalStateException) AST(org.eclipse.jdt.core.dom.AST) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) NotImplementedException(org.autorefactor.util.NotImplementedException) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Type(org.eclipse.jdt.core.dom.Type) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) 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) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 14 with ForStatement

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

the class RemoveEmptyStatementRefactoring method visit.

@Override
public boolean visit(EmptyStatement node) {
    ASTNode parent = node.getParent();
    if (parent instanceof Block) {
        this.ctx.getRefactorings().remove(node);
        return DO_NOT_VISIT_SUBTREE;
    }
    parent = getParentIgnoring(node, Block.class);
    if (parent instanceof IfStatement) {
        final IfStatement is = (IfStatement) parent;
        if (isPassive(is.getExpression())) {
            final List<Statement> thenStmts = asList(is.getThenStatement());
            final List<Statement> elseStmts = asList(is.getElseStatement());
            final boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
            final boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
            if (thenIsEmptyStmt && elseIsEmptyStmt) {
                this.ctx.getRefactorings().remove(parent);
                return DO_NOT_VISIT_SUBTREE;
            } else if (thenIsEmptyStmt && is.getElseStatement() == null) {
                this.ctx.getRefactorings().remove(is);
                return DO_NOT_VISIT_SUBTREE;
            } else if (elseIsEmptyStmt) {
                this.ctx.getRefactorings().remove(is.getElseStatement());
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    } else if (parent instanceof EnhancedForStatement) {
        final EnhancedForStatement efs = (EnhancedForStatement) parent;
        if (isPassive(efs.getExpression()) && efs.getExpression().resolveTypeBinding().isArray()) {
            return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
        }
    } else if (parent instanceof ForStatement) {
        final ForStatement fs = (ForStatement) parent;
        if (arePassive(fs.initializers()) && isPassive(fs.getExpression())) {
            return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
        }
    } else if (parent instanceof WhileStatement) {
        final WhileStatement ws = (WhileStatement) parent;
        if (isPassive(ws.getExpression()) && !Boolean.TRUE.equals(ws.getExpression().resolveConstantExpressionValue())) {
            return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
        }
    } else if (parent instanceof DoStatement) {
        final DoStatement ds = (DoStatement) parent;
        if (isPassive(ds.getExpression()) && !Boolean.TRUE.equals(ds.getExpression().resolveConstantExpressionValue())) {
            return maybeRemoveEmptyStmtBody(node, ds, ds.getBody());
        }
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) 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) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 15 with ForStatement

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

the class ConvertForLoopQuickFixTest method getForStatement.

private static ForStatement getForStatement(ICompilationUnit cu) {
    CompilationUnit ast = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, new NullProgressMonitor());
    final ForStatement[] statement = new ForStatement[1];
    ast.accept(new GenericVisitor() {

        protected boolean visitNode(ASTNode node) {
            if (node instanceof ForStatement) {
                statement[0] = (ForStatement) node;
                return false;
            } else {
                return super.visitNode(node);
            }
        }
    });
    return statement[0];
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ForStatement(org.eclipse.jdt.core.dom.ForStatement) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

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