Search in sources :

Example 86 with InfixExpression

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

the class ObsoleteComparisonRatherThanEqualsCleanUp method getCompareToNode.

private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    MethodInvocation compareToMethod = ast.newMethodInvocation();
    compareToMethod.setExpression(ASTNodes.createMoveTarget(rewrite, visited.getExpression()));
    // $NON-NLS-1$
    compareToMethod.setName(ast.newSimpleName("compareTo"));
    compareToMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, (Expression) visited.arguments().get(0)));
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(compareToMethod);
    newInfixExpression.setOperator(isPositive ? InfixExpression.Operator.EQUALS : InfixExpression.Operator.NOT_EQUALS);
    newInfixExpression.setRightOperand(ast.newNumberLiteral(0));
    return newInfixExpression;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 87 with InfixExpression

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

the class ObsoleteDoubleNegationCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (!visited.hasExtendedOperands() && ASTNodes.hasOperator(visited, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR)) {
        Expression leftExpression = visited.getLeftOperand();
        Expression rightExpression = visited.getRightOperand();
        Expression leftNegatedExpression = null;
        PrefixExpression leftPrefix = ASTNodes.as(leftExpression, PrefixExpression.class);
        if (leftPrefix != null && ASTNodes.hasOperator(leftPrefix, PrefixExpression.Operator.NOT)) {
            leftNegatedExpression = leftPrefix.getOperand();
        }
        Expression rightNegatedExpression = null;
        PrefixExpression rightPrefix = ASTNodes.as(rightExpression, PrefixExpression.class);
        if (rightPrefix != null && ASTNodes.hasOperator(rightPrefix, PrefixExpression.Operator.NOT)) {
            rightNegatedExpression = rightPrefix.getOperand();
        }
        if (leftNegatedExpression != null || rightNegatedExpression != null) {
            removeDoubleNegation(visited, leftExpression, rightExpression, leftNegatedExpression, rightNegatedExpression);
            return false;
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 88 with InfixExpression

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

the class ObsoleteDoubleNegationCleanUp method removeDoubleNegation.

private void removeDoubleNegation(final InfixExpression visited, final Expression leftExpression, final Expression rightExpression, final Expression leftNegatedExpression, final Expression rightNegatedExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDoubleNegationCleanUp_description);
    InfixExpression newInfixExpression = ast.newInfixExpression();
    if (leftNegatedExpression != null) {
        newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, leftNegatedExpression));
        if (rightNegatedExpression != null) {
            newInfixExpression.setOperator(getAppropriateOperator(visited));
            newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightNegatedExpression));
        } else {
            newInfixExpression.setOperator(getNegatedOperator(visited));
            newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightExpression));
        }
    } else {
        newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, leftExpression));
        newInfixExpression.setOperator(getNegatedOperator(visited));
        newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, rightNegatedExpression));
    }
    rewrite.replace(visited, newInfixExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 89 with InfixExpression

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

the class ObsoleteDuplicateAlternativeCleanUp method replaceIfNoElseStatement.

private void replaceIfNoElseStatement(final IfStatement visited, final IfStatement innerIf, final Statement innerMainStatement, final boolean isInnerMainFirst) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDuplicateAlternativeCleanUp_description);
    if (visited.getThenStatement() instanceof Block || innerMainStatement instanceof Block) {
        InfixExpression newInfixExpression = ast.newInfixExpression();
        Expression outerCondition;
        if (isInnerMainFirst) {
            outerCondition = ASTNodes.createMoveTarget(rewrite, visited.getExpression());
        } else {
            outerCondition = ast.negate(visited.getExpression(), true);
        }
        newInfixExpression.setLeftOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, outerCondition));
        newInfixExpression.setOperator(isInnerMainFirst ? InfixExpression.Operator.CONDITIONAL_AND : InfixExpression.Operator.CONDITIONAL_OR);
        newInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, innerIf.getExpression())));
        ASTNodes.replaceButKeepComment(rewrite, innerIf.getExpression(), newInfixExpression, group);
        ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, innerIf), group);
    } else {
        // Workaround: Do not do the cleanup
        // Prepare the code for the next pass
        Block newBlock = ast.newBlock();
        newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, innerIf));
        ASTNodes.replaceButKeepComment(rewrite, innerIf, newBlock, group);
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 90 with InfixExpression

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

the class ObsoleteComparisonCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    OrderedInfixExpression<MethodInvocation, Expression> orderedCondition = ASTNodes.orderedInfix(visited, MethodInvocation.class, Expression.class);
    if (orderedCondition != null && Arrays.asList(InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS).contains(orderedCondition.getOperator())) {
        MethodInvocation comparisonMI = orderedCondition.getFirstOperand();
        Long literalValue = ASTNodes.getIntegerLiteral(orderedCondition.getSecondOperand());
        if (literalValue != null && literalValue.compareTo(0L) != 0 && comparisonMI.getExpression() != null && !ASTNodes.is(comparisonMI.getExpression(), ThisExpression.class) && (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(comparisonMI, Comparable.class.getCanonicalName(), "compareTo", Object.class.getCanonicalName()) || // $NON-NLS-1$
        ASTNodes.usesGivenSignature(comparisonMI, Comparator.class.getCanonicalName(), "compare", Object.class.getCanonicalName(), Object.class.getCanonicalName()) || getJavaMinorVersion() >= 2 && ASTNodes.usesGivenSignature(comparisonMI, String.class.getCanonicalName(), "compareToIgnoreCase", String.class.getCanonicalName()))) {
            // $NON-NLS-1$
            if (literalValue.compareTo(0L) < 0) {
                if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) {
                    refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.LESS);
                } else {
                    refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.GREATER_EQUALS);
                }
            } else if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) {
                refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.GREATER);
            } else {
                refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.LESS_EQUALS);
            }
            return false;
        }
    }
    return true;
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Comparator(java.util.Comparator)

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