Search in sources :

Example 21 with IfStatement

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

Example 22 with IfStatement

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

the class BooleanRefactoring method noThenReturnStmt.

private boolean noThenReturnStmt(final IfStatement node) {
    final Assignment thenA = asExpression(node.getThenStatement(), Assignment.class);
    if (hasOperator(thenA, ASSIGN) && asList(node.getElseStatement()).isEmpty() && (thenA.getLeftHandSide() instanceof Name || thenA.getLeftHandSide() instanceof FieldAccess)) {
        final Statement previousSibling = getPreviousSibling(node);
        if (previousSibling instanceof VariableDeclarationStatement) {
            final VariableDeclarationStatement vds = (VariableDeclarationStatement) previousSibling;
            VariableDeclarationFragment vdf = getVariableDeclarationFragment(vds, thenA.getLeftHandSide());
            if (vdf != null) {
                final VariableDefinitionsUsesVisitor variableUseVisitor = new VariableDefinitionsUsesVisitor(vdf.resolveBinding(), node.getExpression()).find();
                if (variableUseVisitor.getUses().isEmpty()) {
                    final ITypeBinding typeBinding = vds.getType().resolveBinding();
                    return maybeReplace(node, thenA, typeBinding, vdf.getInitializer());
                }
            }
        } else if (previousSibling instanceof ExpressionStatement) {
            final Assignment elseA = asExpression(previousSibling, Assignment.class);
            if (hasOperator(elseA, ASSIGN) && isSameVariable(thenA.getLeftHandSide(), elseA.getLeftHandSide())) {
                final ITypeBinding typeBinding = elseA.resolveTypeBinding();
                return maybeReplace(node, thenA, typeBinding, elseA.getRightHandSide());
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 23 with IfStatement

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

the class CommonCodeInIfElseStatementRefactoring method collectAllCases.

/**
 * Collects all cases (if/else, if/else if/else, etc.) and returns whether all are covered.
 *
 * @param allCases the output collection for all the cases
 * @param node the {@link IfStatement} to examine
 * @return true if all cases (if/else, if/else if/else, etc.) are covered,
 *         false otherwise
 */
private boolean collectAllCases(List<List<Statement>> allCases, IfStatement node) {
    final List<Statement> thenStmts = asList(node.getThenStatement());
    final List<Statement> elseStmts = asList(node.getElseStatement());
    if (thenStmts.isEmpty() || elseStmts.isEmpty()) {
        // let other refactorings take care of removing empty blocks.
        return false;
    }
    allCases.add(thenStmts);
    if (elseStmts.size() == 1) {
        final IfStatement is = as(elseStmts.get(0), IfStatement.class);
        if (is != null) {
            return collectAllCases(allCases, is);
        }
    }
    allCases.add(elseStmts);
    return true;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement)

Example 24 with IfStatement

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

the class ASTBuilder method if0.

/**
 * Builds a new {@link IfStatement} instance.
 *
 * @param condition the if condition
 * @param thenStatement the statement of the then clause
 * @param elseStatement the statement of the else clause
 * @return a new if statement
 */
public IfStatement if0(Expression condition, Statement thenStatement, Statement elseStatement) {
    final IfStatement is = ast.newIfStatement();
    is.setExpression(condition);
    is.setThenStatement(thenStatement);
    is.setElseStatement(elseStatement);
    return is;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement)

Example 25 with IfStatement

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

the class AdvancedQuickAssistProcessor method getSplitOrConditionProposals.

public static boolean getSplitOrConditionProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    Operator orOperator = InfixExpression.Operator.CONDITIONAL_OR;
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    if (infixExpression.getOperator() != orOperator) {
        return false;
    }
    int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
    if (offset == -1) {
        return false;
    }
    // check that infix expression belongs to IfStatement
    Statement statement = ASTResolving.findParentStatement(node);
    if (!(statement instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) statement;
    // check that infix expression is part of first level || condition of IfStatement
    InfixExpression topInfixExpression = infixExpression;
    while (topInfixExpression.getParent() instanceof InfixExpression && ((InfixExpression) topInfixExpression.getParent()).getOperator() == orOperator) {
        topInfixExpression = (InfixExpression) topInfixExpression.getParent();
    }
    if (ifStatement.getExpression() != topInfixExpression) {
        return false;
    }
    //
    if (resultingCollections == null) {
        return true;
    }
    AST ast = ifStatement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare left and right conditions
    Expression[] newOperands = { null, null };
    breakInfixOperationAtOperation(rewrite, topInfixExpression, orOperator, offset, true, newOperands);
    Expression leftCondition = newOperands[0];
    Expression rightCondition = newOperands[1];
    // prepare first statement
    rewrite.replace(ifStatement.getExpression(), leftCondition, null);
    IfStatement secondIf = ast.newIfStatement();
    secondIf.setExpression(rightCondition);
    secondIf.setThenStatement((Statement) rewrite.createCopyTarget(ifStatement.getThenStatement()));
    Statement elseStatement = ifStatement.getElseStatement();
    if (elseStatement == null) {
        rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, secondIf, null);
    } else {
        rewrite.replace(elseStatement, secondIf, null);
        secondIf.setElseStatement((Statement) rewrite.createMoveTarget(elseStatement));
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_splitOrCondition_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_OR_CONDITION);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) 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) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

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