Search in sources :

Example 31 with InfixExpression

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

the class BooleanEqualsRatherThanNullCheckRefactoring method replaceNullCheck.

private void replaceNullCheck(final InfixExpression node, final Expression firstExpr, final boolean isNullCheck, final boolean isAndExpr, final boolean isPositiveExpr) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Name booleanConstant = b.name("Boolean", isAndExpr == isPositiveExpr ? "TRUE" : "FALSE");
    final MethodInvocation equalsMethod = b.invoke(booleanConstant, "equals", b.copy(firstExpr));
    Expression newExpr = null;
    if (!isNullCheck || isAndExpr) {
        newExpr = equalsMethod;
    } else {
        newExpr = b.not(equalsMethod);
    }
    ctx.getRefactorings().replace(node, newExpr);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) Name(org.eclipse.jdt.core.dom.Name)

Example 32 with InfixExpression

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

the class StringValueOfRatherThanConcatRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if (InfixExpression.Operator.PLUS.equals(node.getOperator()) && extendedOperands(node).isEmpty()) {
        final Expression leftOperand = node.getLeftOperand();
        final Expression rightOperand = node.getRightOperand();
        return maybeReplaceStringConcatenation(node, leftOperand, rightOperand) && // if not replaced then try the other way round
        maybeReplaceStringConcatenation(node, rightOperand, leftOperand);
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 33 with InfixExpression

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

the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if ((hasOperator(node, CONDITIONAL_OR) || hasOperator(node, OR)) && !node.hasExtendedOperands()) {
        final InfixExpression firstCondition = as(node.getLeftOperand(), InfixExpression.class);
        final InfixExpression secondCondition = as(node.getRightOperand(), InfixExpression.class);
        if (firstCondition != null && !firstCondition.hasExtendedOperands() && (hasOperator(firstCondition, CONDITIONAL_AND) || hasOperator(firstCondition, AND)) && secondCondition != null && !secondCondition.hasExtendedOperands() && (hasOperator(secondCondition, CONDITIONAL_AND) || hasOperator(secondCondition, AND))) {
            final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
            final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
            final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
            final AtomicBoolean isFourthExprPositive = new AtomicBoolean();
            final Expression firstExpr = getBasisExpression(firstCondition.getLeftOperand(), isFirstExprPositive);
            final Expression secondExpr = getBasisExpression(firstCondition.getRightOperand(), isSecondExprPositive);
            final Expression thirdExpr = getBasisExpression(secondCondition.getLeftOperand(), isThirdExprPositive);
            final Expression fourthExpr = getBasisExpression(secondCondition.getRightOperand(), isFourthExprPositive);
            if (isBooleanVariable(firstExpr) && isBooleanVariable(secondExpr) && isBooleanVariable(thirdExpr) && isBooleanVariable(fourthExpr)) {
                if (match(new ASTMatcher(), firstExpr, thirdExpr) && isFirstExprPositive.get() != isThirdExprPositive.get()) {
                    return maybeReplaceDuplicateExpr(node, firstExpr, firstCondition.getRightOperand(), secondCondition.getRightOperand(), isFirstExprPositive.get());
                } else if (match(new ASTMatcher(), firstExpr, fourthExpr) && isFirstExprPositive.get() != isFourthExprPositive.get()) {
                    return maybeReplaceDuplicateExpr(node, firstExpr, firstCondition.getRightOperand(), secondCondition.getLeftOperand(), isFirstExprPositive.get());
                } else if (match(new ASTMatcher(), secondExpr, thirdExpr) && isSecondExprPositive.get() != isThirdExprPositive.get()) {
                    return maybeReplaceDuplicateExpr(node, secondExpr, firstCondition.getLeftOperand(), secondCondition.getRightOperand(), isSecondExprPositive.get());
                } else if (match(new ASTMatcher(), secondExpr, fourthExpr) && isSecondExprPositive.get() != isFourthExprPositive.get()) {
                    return maybeReplaceDuplicateExpr(node, secondExpr, firstCondition.getLeftOperand(), secondCondition.getLeftOperand(), isSecondExprPositive.get());
                }
            }
        }
    }
    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 34 with InfixExpression

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

the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method maybeReplaceDuplicateExpr.

private boolean maybeReplaceDuplicateExpr(final InfixExpression node, final Expression firstExpr, final Expression secondExpr, final Expression thirdExpr, final boolean isFirstExprPositive) {
    if (!match(new ASTMatcher(), secondExpr, thirdExpr)) {
        final ASTBuilder b = ctx.getASTBuilder();
        final Expression thenExpr;
        final Expression elseExpr;
        if (isFirstExprPositive) {
            thenExpr = secondExpr;
            elseExpr = thirdExpr;
        } else {
            thenExpr = thirdExpr;
            elseExpr = secondExpr;
        }
        ctx.getRefactorings().replace(node, b.conditionalExpr(b.copy(firstExpr), b.copy(thenExpr), b.copy(elseExpr)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
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) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 35 with InfixExpression

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

the class XORRatherThanDuplicateConditionsRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if ((hasOperator(node, CONDITIONAL_OR) || hasOperator(node, OR)) && !node.hasExtendedOperands()) {
        final InfixExpression firstCondition = as(node.getLeftOperand(), InfixExpression.class);
        final InfixExpression secondCondition = as(node.getRightOperand(), InfixExpression.class);
        if (firstCondition != null && !firstCondition.hasExtendedOperands() && (hasOperator(firstCondition, CONDITIONAL_AND) || hasOperator(firstCondition, AND)) && secondCondition != null && !secondCondition.hasExtendedOperands() && (hasOperator(secondCondition, CONDITIONAL_AND) || hasOperator(secondCondition, AND))) {
            final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
            final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
            final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
            final AtomicBoolean isFourthExprPositive = new AtomicBoolean();
            final Expression firstExpr = getBasisExpression(firstCondition.getLeftOperand(), isFirstExprPositive);
            final Expression secondExpr = getBasisExpression(firstCondition.getRightOperand(), isSecondExprPositive);
            final Expression thirdExpr = getBasisExpression(secondCondition.getLeftOperand(), isThirdExprPositive);
            final Expression fourthExpr = getBasisExpression(secondCondition.getRightOperand(), isFourthExprPositive);
            if (isPassive(firstExpr) && isPassive(secondExpr) && ((match(new ASTMatcher(), firstExpr, thirdExpr) && match(new ASTMatcher(), secondExpr, fourthExpr) && isFirstExprPositive.get() ^ isThirdExprPositive.get() && isSecondExprPositive.get() ^ isFourthExprPositive.get()) || (match(new ASTMatcher(), firstExpr, fourthExpr) && match(new ASTMatcher(), secondExpr, thirdExpr) && isFirstExprPositive.get() ^ isFourthExprPositive.get() && isSecondExprPositive.get() ^ isThirdExprPositive.get()))) {
                replaceDuplicateExpr(node, firstExpr, secondExpr, isFirstExprPositive, isSecondExprPositive);
                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)

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