Search in sources :

Example 91 with InfixExpression

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

the class ObsoleteComparisonCleanUp method refactorComparingToZero.

private void refactorComparingToZero(final InfixExpression visited, final MethodInvocation comparisonMethod, final InfixExpression.Operator operator) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteComparisonCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, comparisonMethod));
    newInfixExpression.setOperator(operator);
    // $NON-NLS-1$
    newInfixExpression.setRightOperand(ast.newNumberLiteral("0"));
    ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 92 with InfixExpression

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

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeRefactorIfStatement.

private boolean maybeRefactorIfStatement(final IfStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    if (node.getElseStatement() != null) {
        InfixExpression condition = ASTNodes.as(node.getExpression(), InfixExpression.class);
        List<Statement> thenStatements = ASTNodes.asList(node.getThenStatement());
        List<Statement> elseStatements = ASTNodes.asList(node.getElseStatement());
        if (condition != null && !condition.hasExtendedOperands() && ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS) && thenStatements != null && thenStatements.size() == 1 && elseStatements != null && elseStatements.size() == 1) {
            OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(condition, Expression.class, NullLiteral.class);
            if (nullityOrderedCondition != null && ASTNodes.isPassive(nullityOrderedCondition.getFirstOperand())) {
                return maybeReplaceCode(node, condition, thenStatements, elseStatements, nullityOrderedCondition.getFirstOperand(), classesToUseWithImport, importsToAdd);
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 93 with InfixExpression

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

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceCode.

private boolean maybeReplaceCode(final IfStatement node, final InfixExpression condition, final List<Statement> thenStatements, final List<Statement> elseStatements, final Expression firstField, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    IfStatement checkNullityStatement;
    IfStatement checkEqualsStatement;
    if (ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS)) {
        checkNullityStatement = ASTNodes.as(thenStatements.get(0), IfStatement.class);
        checkEqualsStatement = ASTNodes.as(elseStatements.get(0), IfStatement.class);
    } else {
        checkEqualsStatement = ASTNodes.as(thenStatements.get(0), IfStatement.class);
        checkNullityStatement = ASTNodes.as(elseStatements.get(0), IfStatement.class);
    }
    if (checkNullityStatement != null && checkNullityStatement.getElseStatement() == null && checkEqualsStatement != null && checkEqualsStatement.getElseStatement() == null) {
        InfixExpression nullityCondition = ASTNodes.as(checkNullityStatement.getExpression(), InfixExpression.class);
        ReturnStatement nullityStatement = ASTNodes.as(checkNullityStatement.getThenStatement(), ReturnStatement.class);
        PrefixExpression equalsCondition = ASTNodes.as(checkEqualsStatement.getExpression(), PrefixExpression.class);
        ReturnStatement equalsStatement = ASTNodes.as(checkEqualsStatement.getThenStatement(), ReturnStatement.class);
        if (nullityCondition != null && !nullityCondition.hasExtendedOperands() && ASTNodes.hasOperator(nullityCondition, InfixExpression.Operator.NOT_EQUALS) && nullityStatement != null && equalsCondition != null && ASTNodes.hasOperator(equalsCondition, PrefixExpression.Operator.NOT) && equalsStatement != null) {
            return maybeReplaceEquals(node, firstField, nullityCondition, nullityStatement, equalsCondition, equalsStatement, classesToUseWithImport, importsToAdd);
        }
    }
    return true;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

Example 94 with InfixExpression

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

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceEquals.

private boolean maybeReplaceEquals(final IfStatement node, final Expression firstField, final InfixExpression nullityCondition, final ReturnStatement returnStatement1, final PrefixExpression equalsCondition, final ReturnStatement returnStatement2, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(nullityCondition, Expression.class, NullLiteral.class);
    MethodInvocation equalsMethod = ASTNodes.as(equalsCondition.getOperand(), MethodInvocation.class);
    if (nullityOrderedCondition != null && returnStatement1 != null && returnStatement2 != null && equalsMethod != null && equalsMethod.getExpression() != null && EQUALS_METHOD.equals(equalsMethod.getName().getIdentifier()) && equalsMethod.arguments() != null && equalsMethod.arguments().size() == 1) {
        Expression secondField = nullityOrderedCondition.getFirstOperand();
        if (secondField != null && (match(firstField, secondField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)) || match(secondField, firstField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)))) {
            BooleanLiteral returnFalse1 = ASTNodes.as(returnStatement1.getExpression(), BooleanLiteral.class);
            BooleanLiteral returnFalse2 = ASTNodes.as(returnStatement2.getExpression(), BooleanLiteral.class);
            if (returnFalse1 != null && !returnFalse1.booleanValue() && returnFalse2 != null && !returnFalse2.booleanValue()) {
                replaceEquals(node, firstField, classesToUseWithImport, importsToAdd, secondField, returnStatement1);
                return false;
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 95 with InfixExpression

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

the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method visit.

@Override
public boolean visit(final InfixExpression node) {
    if (ASTNodes.hasOperator(node, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR) && !node.hasExtendedOperands()) {
        InfixExpression firstCondition = ASTNodes.as(node.getLeftOperand(), InfixExpression.class);
        InfixExpression secondCondition = ASTNodes.as(node.getRightOperand(), InfixExpression.class);
        if (firstCondition != null && secondCondition != null && !firstCondition.hasExtendedOperands() && !secondCondition.hasExtendedOperands() && ASTNodes.hasOperator(firstCondition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND) && ASTNodes.hasOperator(secondCondition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND) && ASTNodes.isPassive(firstCondition.getLeftOperand()) && ASTNodes.isPassive(firstCondition.getRightOperand()) && ASTNodes.isPassive(secondCondition.getLeftOperand()) && ASTNodes.isPassive(secondCondition.getRightOperand())) {
            return maybeReplaceDuplicateExpression(node, firstCondition.getLeftOperand(), secondCondition.getLeftOperand(), firstCondition.getRightOperand(), secondCondition.getRightOperand()) && maybeReplaceDuplicateExpression(node, firstCondition.getLeftOperand(), secondCondition.getRightOperand(), firstCondition.getRightOperand(), secondCondition.getLeftOperand());
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)196 Expression (org.eclipse.jdt.core.dom.Expression)137 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)85 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)67 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)55 CastExpression (org.eclipse.jdt.core.dom.CastExpression)47 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)37 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)34 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)31 AST (org.eclipse.jdt.core.dom.AST)29 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)25 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)24 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)24 TextEditGroup (org.eclipse.text.edits.TextEditGroup)22 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Assignment (org.eclipse.jdt.core.dom.Assignment)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)17