Search in sources :

Example 66 with InfixExpression

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

the class AbstractUnitTestCleanUp method maybeRefactorStatement.

/**
 * Maybe refactor the statement.
 *
 * @param classesToUseWithImport The classes to use with import
 * @param importsToAdd           The imports to add
 * @param nodeToReplace          The node
 * @param originalMethod         The method invocation
 * @param isAssertTrue           True if assertTrue is used, False if assertFalse is
 *                               used.
 * @param condition              The condition on which the assert is based.
 * @param failureMessage         The failure message or null.
 * @param isRewriteNeeded        True if is the rewriting is needed.
 * @return True if refactored
 */
protected boolean maybeRefactorStatement(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertTrue, final Expression condition, final Expression failureMessage, final boolean isRewriteNeeded) {
    Expression localCondition = condition;
    boolean localIsAssertTrue = isAssertTrue;
    boolean localIsRewriteNeeded = isRewriteNeeded;
    PrefixExpression localConditionPe = ASTNodes.as(localCondition, PrefixExpression.class);
    while (ASTNodes.hasOperator(localConditionPe, PrefixExpression.Operator.NOT)) {
        localIsRewriteNeeded = true;
        localIsAssertTrue = !localIsAssertTrue;
        localCondition = ASTNodes.as(localConditionPe.getOperand(), Expression.class);
        localConditionPe = ASTNodes.as(localCondition, PrefixExpression.class);
    }
    InfixExpression conditionIe = ASTNodes.as(localCondition, InfixExpression.class);
    MethodInvocation conditionMi = ASTNodes.as(localCondition, MethodInvocation.class);
    Object constantValue = localCondition.resolveConstantExpressionValue();
    return maybeRefactorAssertTrueOrFalse(classesToUseWithImport, importsToAdd, nodeToReplace, originalMethod, localIsAssertTrue, localCondition, conditionIe, conditionMi, constantValue, failureMessage, localIsRewriteNeeded);
}
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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 67 with InfixExpression

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

the class AbstractUnitTestCleanUp method maybeRefactorComparison.

private boolean maybeRefactorComparison(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression infixExpression, final boolean isAssertEquals, final Expression failureMessage) {
    Pair<Expression, Expression> actualAndExpected = getActualAndExpected(infixExpression.getLeftOperand(), infixExpression.getRightOperand());
    if (isComparingObjects(infixExpression) && !ASTNodes.is(infixExpression.getLeftOperand(), NullLiteral.class) && !ASTNodes.is(infixExpression.getRightOperand(), NullLiteral.class)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        // $NON-NLS-1$
        TextEditGroup group = new TextEditGroup("");
        MethodInvocation newAssert = invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, // $NON-NLS-1$
        getAssertName(isAssertEquals, "Same"), // $NON-NLS-1$
        ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getFirst())), // $NON-NLS-1$
        ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actualAndExpected.getSecond())), null, failureMessage);
        ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, newAssert), group);
        return false;
    }
    return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 68 with InfixExpression

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

the class ControlWorkflowMatcher method expandActualNode.

private void expandActualNode(final ControlWorkflowNode actualNode) {
    if (actualNode.getCondition() == null) {
        return;
    }
    PrefixExpression prefixExpression = ASTNodes.as(actualNode.getCondition(), PrefixExpression.class);
    ConditionalExpression ternaryExpression = ASTNodes.as(actualNode.getCondition(), ConditionalExpression.class);
    InfixExpression infixExpression = ASTNodes.as(actualNode.getCondition(), InfixExpression.class);
    if (prefixExpression != null) {
        if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.NOT)) {
            ControlWorkflowNode oppositeNode = actualNode.getThenNode();
            actualNode.setThenNode(actualNode.getElseNode());
            actualNode.setElseNode(oppositeNode);
            actualNode.setCondition(prefixExpression.getOperand());
            expandActualNode(actualNode);
            return;
        }
    } else if (ternaryExpression != null) {
        ControlWorkflowNode node1 = new ControlWorkflowNode();
        node1.setCondition(ternaryExpression.getThenExpression());
        node1.setThenNode(cloneNode(actualNode.getThenNode()));
        node1.setElseNode(cloneNode(actualNode.getElseNode()));
        ControlWorkflowNode node2 = new ControlWorkflowNode();
        node2.setCondition(ternaryExpression.getElseExpression());
        node2.setThenNode(cloneNode(actualNode.getThenNode()));
        node2.setElseNode(cloneNode(actualNode.getElseNode()));
        actualNode.setCondition(ternaryExpression.getExpression());
        actualNode.setThenNode(node1);
        actualNode.setElseNode(node2);
        expandActualNode(actualNode);
        return;
    } else if (infixExpression != null && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
        List<Expression> allOperands = ASTNodes.allOperands(infixExpression);
        Expression firstOperand = allOperands.remove(0);
        ControlWorkflowNode currentNode = actualNode;
        for (Expression operand : allOperands) {
            ControlWorkflowNode subNode = new ControlWorkflowNode();
            subNode.setCondition(operand);
            subNode.setThenNode(cloneNode(currentNode.getThenNode()));
            subNode.setElseNode(cloneNode(currentNode.getElseNode()));
            currentNode.setCondition(firstOperand);
            if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
                currentNode.setThenNode(subNode);
            } else {
                currentNode.setElseNode(subNode);
            }
            currentNode = subNode;
        }
        expandActualNode(actualNode);
        return;
    } else if (infixExpression != null && !infixExpression.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.XOR)) {
        ControlWorkflowNode subNode1 = new ControlWorkflowNode();
        subNode1.setCondition(infixExpression.getRightOperand());
        subNode1.setThenNode(cloneNode(actualNode.getElseNode()));
        subNode1.setElseNode(cloneNode(actualNode.getThenNode()));
        ControlWorkflowNode subNode2 = new ControlWorkflowNode();
        subNode2.setCondition(infixExpression.getRightOperand());
        subNode2.setThenNode(cloneNode(actualNode.getThenNode()));
        subNode2.setElseNode(cloneNode(actualNode.getElseNode()));
        actualNode.setCondition(infixExpression.getLeftOperand());
        actualNode.setThenNode(subNode1);
        actualNode.setElseNode(subNode2);
        expandActualNode(actualNode);
        return;
    }
    expandActualNode(actualNode.getThenNode());
    expandActualNode(actualNode.getElseNode());
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 69 with InfixExpression

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

the class ASTSemanticMatcher method getConsistentOperands.

private List<Expression> getConsistentOperands(final InfixExpression infixExpression) {
    List<Expression> operands = ASTNodes.allOperands(infixExpression);
    for (Iterator<Expression> iterator = operands.iterator(); iterator.hasNext() && operands.size() > 1; ) {
        Expression operand = iterator.next();
        Long numberLiteral = ASTNodes.getIntegerLiteral(operand);
        Boolean booleanValue = ASTNodes.getBooleanLiteral(operand);
        if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND)) {
            if (Boolean.TRUE.equals(booleanValue)) {
                iterator.remove();
            }
        } else if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_OR)) {
            if (Boolean.FALSE.equals(booleanValue)) {
                iterator.remove();
            }
        } else if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.PLUS)) {
            if (Long.valueOf(0L).equals(numberLiteral)) {
                iterator.remove();
            }
        } else if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.TIMES) && Long.valueOf(1L).equals(numberLiteral)) {
            iterator.remove();
        }
    }
    return operands;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 70 with InfixExpression

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

the class RemoveParenthesisCleanUp method getExpressionWithoutParentheses.

private Expression getExpressionWithoutParentheses(final ParenthesizedExpression parenthesis) {
    ASTNode parent = parenthesis.getParent();
    Expression child = parenthesis.getExpression();
    if (isParenthesesUselessForParent(parent, parenthesis) || isParenthesesUselessForChild(child)) {
        return child;
    }
    if (parent instanceof InfixExpression) {
        InfixExpression parentInfixExpression = (InfixExpression) parent;
        if (child instanceof InfixExpression) {
            InfixExpression.Operator innerOp = ((InfixExpression) child).getOperator();
            if (innerOp == parentInfixExpression.getOperator() && OperatorEnum.isAssociative(innerOp) && // to other if statements in this method.
            Utils.equalNotNull(child.resolveTypeBinding(), parentInfixExpression.resolveTypeBinding())) {
                return child;
            }
        }
    }
    // Infix, prefix or postfix without parenthesis is not readable
    if (isInnerExprHardToRead(child, parent)) {
        return null;
    }
    if (parent instanceof InfixExpression && ASTNodes.hasOperator((InfixExpression) parent, InfixExpression.Operator.PLUS, InfixExpression.Operator.MINUS) || parent instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) parent, PrefixExpression.Operator.PLUS, PrefixExpression.Operator.MINUS)) {
        if (child instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) child, PrefixExpression.Operator.DECREMENT, PrefixExpression.Operator.INCREMENT, PrefixExpression.Operator.PLUS, PrefixExpression.Operator.MINUS) || child instanceof PostfixExpression && ASTNodes.hasOperator((PostfixExpression) child, PostfixExpression.Operator.DECREMENT, PostfixExpression.Operator.INCREMENT)) {
            return null;
        }
        if (child instanceof NumberLiteral && (((NumberLiteral) child).getToken().startsWith("+") || ((NumberLiteral) child).getToken().startsWith("-"))) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return null;
        }
    }
    int compareTo = OperatorEnum.compareTo(child, parent);
    if (compareTo < 0) {
        return null;
    }
    if (compareTo > 0) {
        return child;
    }
    if (// some like it like that
    child instanceof InfixExpression || child instanceof CastExpression || // Infix and prefix or postfix without parenthesis is not readable
    (parent instanceof InfixExpression || parent instanceof PrefixExpression || parent instanceof PostfixExpression) && (child instanceof PrefixExpression || child instanceof PostfixExpression)) {
        return null;
    }
    return child;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

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