Search in sources :

Example 36 with InfixExpression

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

the class ForLoopHelper method getIndexOnIterable.

private static ForLoopContent getIndexOnIterable(final Expression condition, Name loopVariable) {
    final InfixExpression ie = as(condition, InfixExpression.class);
    if (ie != null && !ie.hasExtendedOperands()) {
        final Expression leftOp = ie.getLeftOperand();
        final Expression rightOp = ie.getRightOperand();
        if (hasOperator(ie, LESS)) {
            return buildForLoopContent(loopVariable, rightOp);
        } else if (hasOperator(ie, GREATER)) {
            return buildForLoopContent(loopVariable, leftOp);
        }
    }
    return null;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 37 with InfixExpression

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

the class AbstractUnitTestRefactoring method maybeRefactorComparison.

private boolean maybeRefactorComparison(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression ie, final boolean isAssertEquals, final Expression failureMessage, final boolean isRewriteNeeded) {
    final Pair<Expression, Expression> actualAndExpected = getActualAndExpected(ie.getLeftOperand(), ie.getRightOperand());
    if (isComparingObjects(ie) && !isNullLiteral(ie.getLeftOperand()) && !isNullLiteral(ie.getRightOperand())) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Refactorings r = this.ctx.getRefactorings();
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Same"), b.copy(actualAndExpected.getFirst()), b.copy(actualAndExpected.getSecond()), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    } else {
        return maybeRefactorToAssertEquals(nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
    }
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 38 with InfixExpression

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

the class ORConditionRatherThanRedundantClausesRefactoring method replaceDuplicateExpr.

private void replaceDuplicateExpr(final InfixExpression node, final Operator operator, final Expression leftExpr, final Expression rightExpr, final boolean isLeftExprPositive, final boolean isRightExprPositive, final boolean forward) {
    final ASTBuilder b = ctx.getASTBuilder();
    Expression copyOfLeftExpr = b.copy(leftExpr);
    if (!isLeftExprPositive) {
        copyOfLeftExpr = b.not(copyOfLeftExpr);
    }
    Expression copyOfRightExpr = b.copy(rightExpr);
    if (!isRightExprPositive) {
        copyOfRightExpr = b.not(copyOfRightExpr);
    }
    if (forward) {
        ctx.getRefactorings().replace(node, b.infixExpr(copyOfLeftExpr, operator, copyOfRightExpr));
    } else {
        ctx.getRefactorings().replace(node, b.infixExpr(copyOfRightExpr, operator, copyOfLeftExpr));
    }
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 39 with InfixExpression

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

the class ORConditionRatherThanRedundantClausesRefactoring method maybeRefactorCondition.

private boolean maybeRefactorCondition(final InfixExpression node, final Operator operator, final Expression leftOperand, final Expression rightOperand, final boolean forward) {
    final InfixExpression complexCondition = as(leftOperand, InfixExpression.class);
    if (complexCondition != null && !complexCondition.hasExtendedOperands() && (hasOperator(complexCondition, CONDITIONAL_AND) || hasOperator(complexCondition, AND))) {
        final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
        final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
        final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
        final Expression firstExpr = getBasisExpression(complexCondition.getLeftOperand(), isFirstExprPositive);
        final Expression secondExpr = getBasisExpression(complexCondition.getRightOperand(), isSecondExprPositive);
        final Expression thirdExpr = getBasisExpression(rightOperand, isThirdExprPositive);
        if (isPrimitive(firstExpr) && isPrimitive(secondExpr) && isPrimitive(thirdExpr)) {
            if (match(new ASTMatcher(), secondExpr, thirdExpr)) {
                replaceDuplicateExpr(node, operator, firstExpr, thirdExpr, isFirstExprPositive.get(), isThirdExprPositive.get(), forward);
                return DO_NOT_VISIT_SUBTREE;
            } else if (match(new ASTMatcher(), firstExpr, thirdExpr)) {
                replaceDuplicateExpr(node, operator, secondExpr, thirdExpr, isSecondExprPositive.get(), isThirdExprPositive.get(), forward);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 40 with InfixExpression

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

the class RemoveUnnecessaryCastRefactoring method canRemoveCast.

private boolean canRemoveCast(CastExpression node) {
    final ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case RETURN_STATEMENT:
            final MethodDeclaration md = getAncestor(parent, MethodDeclaration.class);
            return isAssignmentCompatible(node.getExpression(), md.getReturnType2());
        case ASSIGNMENT:
            final Assignment as = (Assignment) parent;
            return isAssignmentCompatible(node.getExpression(), as) || isConstantExpressionAssignmentConversion(node);
        case VARIABLE_DECLARATION_FRAGMENT:
            final VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
            return isAssignmentCompatible(node.getExpression(), resolveTypeBinding(vdf)) || isConstantExpressionAssignmentConversion(node);
        case INFIX_EXPRESSION:
            final InfixExpression ie = (InfixExpression) parent;
            final Expression lo = ie.getLeftOperand();
            final Expression ro = ie.getRightOperand();
            if (node.equals(lo)) {
                return (isStringConcat(ie) || isAssignmentCompatible(node.getExpression(), ro)) && !isPrimitiveTypeNarrowing(node) && !hasOperator(ie, DIVIDE) && !hasOperator(ie, PLUS) && !hasOperator(ie, MINUS);
            } else {
                final boolean integralDivision = isIntegralDivision(ie);
                return ((isNotRefactored(lo) && isStringConcat(ie)) || (!integralDivision && isAssignmentCompatibleInInfixExpression(node, ie)) || (integralDivision && canRemoveCastInIntegralDivision(node, ie))) && !isPrimitiveTypeNarrowing(node) && !isIntegralDividedByFloatingPoint(node, ie);
            }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)95 Expression (org.eclipse.jdt.core.dom.Expression)65 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)51 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)39 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)31 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)26 AST (org.eclipse.jdt.core.dom.AST)24 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)20 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)19 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 Assignment (org.eclipse.jdt.core.dom.Assignment)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)10 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)10