Search in sources :

Example 31 with IfStatement

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

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

the class InputFlowAnalyzer method endVisit.

@Override
public void endVisit(IfStatement node) {
    if (skipNode(node))
        return;
    Statement thenPart = node.getThenStatement();
    Statement elsePart = node.getElseStatement();
    if ((thenPart != null && fSelection.coveredBy(thenPart)) || (elsePart != null && fSelection.coveredBy(elsePart))) {
        GenericSequentialFlowInfo info = createSequential();
        setFlowInfo(node, info);
        endVisitConditional(info, node.getExpression(), new ASTNode[] { thenPart, elsePart });
    } else {
        super.endVisit(node);
    }
}
Also used : ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) 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) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

Example 33 with IfStatement

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

the class IfElseIfRefactoring method visit.

// TODO JNR
// UseIfElseIfRefactoring
// if (b) {
// return i;
// }
// if (c) {
// return j;
// }
// if (d) {
// return k;
// }
// return l;
@Override
public boolean visit(IfStatement node) {
    final Statement elseStmt = node.getElseStatement();
    if (elseStmt instanceof Block) {
        List<Statement> elseStmts = statements((Block) elseStmt);
        if (elseStmts.size() == 1 && elseStmts.get(0) instanceof IfStatement) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            this.ctx.getRefactorings().set(node, ELSE_STATEMENT_PROPERTY, b.copy(elseStmts.get(0)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Block(org.eclipse.jdt.core.dom.Block) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 34 with IfStatement

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

the class TryWithResourceRefactoring method visit.

@Override
public boolean visit(TryStatement node) {
    final List<Statement> tryStmts = asList(node.getBody());
    if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
        final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
        if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
            return collapseTryStatements(node, innerTryStmt);
        }
    }
    final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
    if (previousDeclStmt == null) {
        return VISIT_SUBTREE;
    }
    final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
    final List<Statement> finallyStmts = asList(node.getFinally());
    if (previousDeclFragment != null && finallyStmts.size() >= 1) {
        final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
        nodesToRemove.add(previousDeclStmt);
        final Statement finallyStmt = finallyStmts.get(0);
        nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
        final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
        final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
        if (finallyEs != null) {
            final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        } else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
            final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
            final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
            final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 35 with IfStatement

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

the class UpdateSetRatherThanTestingFirstRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Statement elseStmt = node.getElseStatement();
    final Statement thenStmt = node.getThenStatement();
    final PrefixExpression pe = as(node.getExpression(), PrefixExpression.class);
    if (hasOperator(pe, NOT)) {
        return maybeReplaceSetContains(node, pe.getOperand(), thenStmt, elseStmt, false);
    } else {
        return maybeReplaceSetContains(node, node.getExpression(), elseStmt, thenStmt, true);
    }
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Aggregations

IfStatement (org.eclipse.jdt.core.dom.IfStatement)43 Statement (org.eclipse.jdt.core.dom.Statement)39 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)27 ForStatement (org.eclipse.jdt.core.dom.ForStatement)27 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)27 DoStatement (org.eclipse.jdt.core.dom.DoStatement)25 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)25 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)23 Block (org.eclipse.jdt.core.dom.Block)22 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)21 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 Expression (org.eclipse.jdt.core.dom.Expression)18 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)17 AST (org.eclipse.jdt.core.dom.AST)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)15 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)13