Search in sources :

Example 26 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)

Example 27 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)

Example 28 with InfixExpression

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

the class CollapseIfStatementRefactoring method replaceIfNoElseStatement.

private boolean replaceIfNoElseStatement(IfStatement outerIf, IfStatement innerIf) {
    if (innerIf.getElseStatement() != null) {
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final InfixExpression ie = b.infixExpr(parenthesizeOrExpr(b, outerIf.getExpression()), CONDITIONAL_AND, parenthesizeOrExpr(b, innerIf.getExpression()));
    this.ctx.getRefactorings().replace(outerIf.getExpression(), ie);
    this.ctx.getRefactorings().replace(outerIf.getThenStatement(), b.copy(innerIf.getThenStatement()));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 29 with InfixExpression

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

the class OperatorEnumTest method simpleTestCompareExpressions.

@Test
public void simpleTestCompareExpressions() {
    final AST ast = AST.newAST(AST.JLS4);
    final Assignment op1 = ast.newAssignment();
    op1.setOperator(Assignment.Operator.ASSIGN);
    final InfixExpression op2 = ast.newInfixExpression();
    op2.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
    assertTrue(OperatorEnum.compareTo(op1, op2) < 0);
    assertEquals("Comparing unknown objects result in no decision", 0, OperatorEnum.compareTo(op1, null));
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AST(org.eclipse.jdt.core.dom.AST) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Test(org.junit.Test)

Example 30 with InfixExpression

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

the class BooleanEqualsRatherThanNullCheckRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
        final Expression leftOperand = node.getLeftOperand();
        final Expression rightOperand = node.getRightOperand();
        final InfixExpression condition = as(leftOperand, InfixExpression.class);
        final boolean isNullCheck = hasOperator(condition, EQUALS);
        final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
        if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
            Expression firstExpr = null;
            if (isNullLiteral(condition.getLeftOperand())) {
                firstExpr = condition.getRightOperand();
            } else if (isNullLiteral(condition.getRightOperand())) {
                firstExpr = condition.getLeftOperand();
            }
            Expression secondExpr = null;
            final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
            final boolean isPositiveExpr;
            if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
                secondExpr = negateSecondExpr.getOperand();
                isPositiveExpr = false;
            } else {
                secondExpr = rightOperand;
                isPositiveExpr = true;
            }
            if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
                replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
                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) 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)73 Expression (org.eclipse.jdt.core.dom.Expression)50 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)30 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)23 CastExpression (org.eclipse.jdt.core.dom.CastExpression)22 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)19 AST (org.eclipse.jdt.core.dom.AST)18 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)10 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)10 Assignment (org.eclipse.jdt.core.dom.Assignment)8 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)8 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)8 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7