Search in sources :

Example 46 with Statement

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

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

use of org.eclipse.jdt.core.dom.Statement in project eclipse-pmd by acanda.

the class DefaultLabelNotLastInSwitchStmtQuickFix method apply.

/**
 * Moves the default case to the last position. The default case includes the default {@code SwitchCase} and all
 * following statements up to the next {@code SwitchCase}.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final SwitchStatement node) {
    final List<Statement> statements = node.statements();
    final List<Statement> defaultCaseStatements = new ArrayList<>(statements.size());
    boolean isDefaultCaseStatement = false;
    for (final Statement statement : statements) {
        if (statement instanceof SwitchCase) {
            if (((SwitchCase) statement).getExpression() == DEFAULT_LABEL) {
                isDefaultCaseStatement = true;
            } else {
                isDefaultCaseStatement = false;
            }
        }
        if (isDefaultCaseStatement) {
            defaultCaseStatements.add(statement);
        }
    }
    statements.removeAll(defaultCaseStatements);
    statements.addAll(defaultCaseStatements);
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) ArrayList(java.util.ArrayList)

Example 49 with Statement

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

Example 50 with Statement

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

the class QuickAssistProcessor method getChangeLambdaBodyToBlockProposal.

private static boolean getChangeLambdaBodyToBlockProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    LambdaExpression lambda;
    if (covering instanceof LambdaExpression) {
        lambda = (LambdaExpression) covering;
    } else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
        lambda = (LambdaExpression) covering.getParent();
    } else {
        return false;
    }
    if (!(lambda.getBody() instanceof Expression))
        return false;
    if (lambda.resolveMethodBinding() == null)
        return false;
    if (resultingCollections == null)
        return true;
    AST ast = lambda.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    Statement statementInBlockBody;
    Expression bodyExpr = (Expression) rewrite.createMoveTarget(lambda.getBody());
    if (ast.resolveWellKnownType("void").isEqualTo(lambda.resolveMethodBinding().getReturnType())) {
        //$NON-NLS-1$
        ExpressionStatement expressionStatement = ast.newExpressionStatement(bodyExpr);
        statementInBlockBody = expressionStatement;
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(bodyExpr);
        statementInBlockBody = returnStatement;
    }
    Block blockBody = ast.newBlock();
    blockBody.statements().add(statementInBlockBody);
    rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, blockBody, null);
    // add proposal
    String label = CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_block;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_BLOCK);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) 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) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

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