Search in sources :

Example 41 with Statement

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

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

the class StringBuilderRefactoring method maybeRefactorAppending.

private boolean maybeRefactorAppending(Expression node) {
    final LinkedList<Pair<ITypeBinding, Expression>> allAppendedStrings = new LinkedList<Pair<ITypeBinding, Expression>>();
    final AtomicBoolean isRefactoringNeeded = new AtomicBoolean(false);
    final AtomicBoolean isInstanceCreationToRewrite = new AtomicBoolean(false);
    final Expression lastExpr = readAppendMethod(node, allAppendedStrings, isRefactoringNeeded, isInstanceCreationToRewrite);
    if (lastExpr != null) {
        removeEmptyStrings(allAppendedStrings, isRefactoringNeeded);
        removeCallsToToString(allAppendedStrings, isRefactoringNeeded, isInstanceCreationToRewrite.get());
        if (isRefactoringNeeded.get()) {
            if (allAppendedStrings.isEmpty() && isVariable(lastExpr) && node.getParent() instanceof Statement) {
                ctx.getRefactorings().remove(node.getParent());
            } else {
                replaceWithNewStringAppends(node, allAppendedStrings, lastExpr, isInstanceCreationToRewrite.get());
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Statement(org.eclipse.jdt.core.dom.Statement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) LinkedList(java.util.LinkedList) Pair(org.autorefactor.util.Pair)

Example 43 with Statement

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

the class AbstractClassSubstituteRefactoring method canVarOccurrenceBeRefactored0.

private boolean canVarOccurrenceBeRefactored0(final Block node, final List<VariableDeclaration> varDecls, final List<MethodInvocation> methodCallsToRefactor, final List<VariableDeclaration> otherVarDecls) {
    for (final VariableDeclaration varDecl : varDecls) {
        final VarOccurrenceVisitor varOccurrenceVisitor = new VarOccurrenceVisitor(varDecl);
        final Statement parent = getAncestorOrNull(varDecl, Statement.class);
        Statement nextSibling = getNextSibling(parent);
        while (nextSibling != null) {
            varOccurrenceVisitor.visitNode(nextSibling);
            nextSibling = getNextSibling(nextSibling);
        }
        if (varOccurrenceVisitor.isUsedInAnnonymousClass()) {
            return false;
        }
        for (final SimpleName varOccurrence : varOccurrenceVisitor.getVarOccurrences()) {
            final List<VariableDeclaration> subVarDecls = new ArrayList<VariableDeclaration>();
            if (!canBeRefactored(node, varOccurrence, varOccurrence.resolveTypeBinding(), subVarDecls, methodCallsToRefactor)) {
                return false;
            }
            otherVarDecls.addAll(subVarDecls);
        }
    }
    return true;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Example 44 with Statement

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

the class AllInOneMethodRatherThanLoopRefactoring method visit.

@Override
public boolean visit(EnhancedForStatement node) {
    final Expression iterable = node.getExpression();
    final List<Statement> stmts = asList(node.getBody());
    if (stmts.size() != 1) {
        return VISIT_SUBTREE;
    }
    final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
    final IVariableBinding foreachVariable = node.getParameter().resolveBinding();
    // As we replace only one, there should be no more than one occurrence
    if (getVariableUseCount(foreachVariable, node.getBody()) == 1) {
        if (instanceOf(iterable, "java.util.Collection")) {
            if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
                return maybeReplaceWithCollectionMethod(node, iterable, "addAll", mi);
            } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                return maybeReplaceWithCollectionMethod(node, iterable, "removeAll", mi);
            }
        } else if (isArray(iterable) && isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), iterable) && isSameLocalVariable(foreachVariable, arg0(mi))) {
            replaceWithCollectionsAddAll(node, iterable, mi);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : 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) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 45 with Statement

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

the class BooleanRefactoring method visitIfStatement.

private boolean visitIfStatement(final IfStatement node) {
    final BooleanASTMatcher matcher = new BooleanASTMatcher();
    if (match(matcher, node.getThenStatement(), node.getElseStatement())) {
        // Then and else statement are matching, bar the boolean values
        // which are opposite
        final Statement copyStmt = b.copySubtree(node.getThenStatement());
        // identify the node that need to be replaced after the copy
        final BooleanASTMatcher matcher2 = new BooleanASTMatcher(matcher.matches);
        if (match(matcher2, copyStmt, node.getElseStatement())) {
            final Expression ifCondition = node.getExpression();
            copyStmt.accept(new BooleanReplaceVisitor(ifCondition, matcher2.matches.values(), getBooleanName(node)));
            // make sure to keep curly braces if the node is an else statement
            ctx.getRefactorings().replace(node, isElseStatementOfParent(node) ? copyStmt : toSingleStmt(copyStmt));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    final ReturnStatement thenRs = as(node.getThenStatement(), ReturnStatement.class);
    if (thenRs != null) {
        final ReturnStatement elseRs = as(node.getElseStatement() != null ? node.getElseStatement() : getNextSibling(node), ReturnStatement.class);
        if (elseRs != null) {
            return withThenReturnStmt(node, thenRs, elseRs);
        }
        return VISIT_SUBTREE;
    } else {
        return noThenReturnStmt(node);
    }
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)95 ForStatement (org.eclipse.jdt.core.dom.ForStatement)59 IfStatement (org.eclipse.jdt.core.dom.IfStatement)58 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)56 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)51 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)49 DoStatement (org.eclipse.jdt.core.dom.DoStatement)46 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)46 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)45 Block (org.eclipse.jdt.core.dom.Block)42 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)39 Expression (org.eclipse.jdt.core.dom.Expression)38 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)30 AST (org.eclipse.jdt.core.dom.AST)26 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)26 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)25 TryStatement (org.eclipse.jdt.core.dom.TryStatement)23 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)22