Search in sources :

Example 86 with Expression

use of org.eclipse.jdt.core.dom.Expression 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 87 with Expression

use of org.eclipse.jdt.core.dom.Expression 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 88 with Expression

use of org.eclipse.jdt.core.dom.Expression 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 89 with Expression

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

the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method getBasisExpression.

private Expression getBasisExpression(final Expression originalExpr, final AtomicBoolean isExprPositive) {
    Expression basisExpr = null;
    final PrefixExpression negateExpr = as(originalExpr, PrefixExpression.class);
    if (hasOperator(negateExpr, NOT)) {
        basisExpr = negateExpr.getOperand();
        isExprPositive.set(false);
    } else {
        basisExpr = originalExpr;
        isExprPositive.set(true);
    }
    return basisExpr;
}
Also used : 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)

Example 90 with Expression

use of org.eclipse.jdt.core.dom.Expression 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

Expression (org.eclipse.jdt.core.dom.Expression)552 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)263 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)213 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)187 CastExpression (org.eclipse.jdt.core.dom.CastExpression)185 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)135 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)126 ASTNode (org.eclipse.jdt.core.dom.ASTNode)125 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)122 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)121 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)112 AST (org.eclipse.jdt.core.dom.AST)101 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)95 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)88 SimpleName (org.eclipse.jdt.core.dom.SimpleName)83 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)76 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)73 Type (org.eclipse.jdt.core.dom.Type)70 ArrayList (java.util.ArrayList)69 Block (org.eclipse.jdt.core.dom.Block)63