Search in sources :

Example 51 with PrefixExpression

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

the class ObsoletePushNegationDownCleanUp method visit.

@Override
public boolean visit(final PrefixExpression node) {
    if (!ASTNodes.hasOperator(node, PrefixExpression.Operator.NOT)) {
        return true;
    }
    Expression replacement = getOppositeExpression(node.getOperand());
    if (replacement != null) {
        TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoletePushNegationDownCleanUp_description);
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodes.replaceButKeepComment(rewrite, node, replacement, group);
        return false;
    }
    return 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) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 52 with PrefixExpression

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

the class ObsoletePushNegationDownCleanUp method getOppositeExpression.

private Expression getOppositeExpression(final Expression negativeExpression) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    Expression operand = ASTNodes.getUnparenthesedExpression(negativeExpression);
    if (operand instanceof PrefixExpression) {
        PrefixExpression pe = (PrefixExpression) operand;
        if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.NOT)) {
            return ASTNodes.createMoveTarget(rewrite, pe.getOperand());
        }
    } else if (operand instanceof InfixExpression) {
        InfixExpression infixExpression = (InfixExpression) operand;
        InfixExpression.Operator reverseOp = (InfixExpression.Operator) OperatorEnum.getOperator(infixExpression).getReverseBooleanOperator();
        if (reverseOp != null) {
            List<Expression> allOperands = new ArrayList<>(ASTNodes.allOperands(infixExpression));
            if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
                for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
                    Expression anOperand = it.next();
                    Expression oppositeOperand = getOppositeExpression(anOperand);
                    it.set(oppositeOperand != null ? oppositeOperand : ast.negate(anOperand, true));
                }
            } else {
                allOperands = ASTNodes.createMoveTarget(rewrite, allOperands);
            }
            return ast.newParenthesizedExpression(ast.newInfixExpression(reverseOp, allOperands));
        }
    } else {
        Boolean constant = ASTNodes.getBooleanLiteral(operand);
        if (constant != null) {
            return ast.newBooleanLiteral(!constant);
        }
    }
    return null;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList) ListIterator(java.util.ListIterator)

Example 53 with PrefixExpression

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

the class ObsoleteRedundantComparatorCleanUp method isReturnedExpressionToRemove.

private boolean isReturnedExpressionToRemove(final SimpleName name1, final SimpleName name2, final Expression returnExpression, final boolean isForward) {
    PrefixExpression negativeExpression = ASTNodes.as(returnExpression, PrefixExpression.class);
    if (negativeExpression != null && ASTNodes.hasOperator(negativeExpression, PrefixExpression.Operator.MINUS)) {
        return isReturnedExpressionToRemove(name1, name2, negativeExpression.getOperand(), !isForward);
    }
    MethodInvocation compareToMethod = ASTNodes.as(returnExpression, MethodInvocation.class);
    if (compareToMethod != null && compareToMethod.getExpression() != null) {
        ITypeBinding comparisonType = compareToMethod.getExpression().resolveTypeBinding();
        if (comparisonType != null) {
            List<Expression> arguments = compareToMethod.arguments();
            if (compareToMethod.getExpression() != null && // $NON-NLS-1$
            ASTNodes.usesGivenSignature(// $NON-NLS-1$
            compareToMethod, // $NON-NLS-1$
            comparisonType.getQualifiedName(), // $NON-NLS-1$
            "compareTo", comparisonType.getQualifiedName())) {
                return isRefactorComparisonToRefactor(name1, name2, compareToMethod.getExpression(), arguments.get(0), isForward);
            }
            String primitiveType = Bindings.getUnboxedTypeName(comparisonType.getQualifiedName());
            if (primitiveType != null && // $NON-NLS-1$
            ASTNodes.usesGivenSignature(// $NON-NLS-1$
            compareToMethod, // $NON-NLS-1$
            comparisonType.getQualifiedName(), // $NON-NLS-1$
            "compare", primitiveType, primitiveType)) {
                return isRefactorComparisonToRefactor(name1, name2, arguments.get(0), arguments.get(1), isForward);
            }
        }
    }
    return false;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 54 with PrefixExpression

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

the class BooleanEqualsRatherThanNullCheckCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
        Expression leftOperand = visited.getLeftOperand();
        Expression rightOperand = visited.getRightOperand();
        InfixExpression condition = ASTNodes.as(leftOperand, InfixExpression.class);
        boolean isNullCheck = ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS);
        boolean isAndExpression = ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND);
        if (!visited.hasExtendedOperands() && isNullCheck ^ isAndExpression && condition != null && ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS)) {
            Expression firstExpression = null;
            if (ASTNodes.is(condition.getLeftOperand(), NullLiteral.class)) {
                firstExpression = condition.getRightOperand();
            } else if (ASTNodes.is(condition.getRightOperand(), NullLiteral.class)) {
                firstExpression = condition.getLeftOperand();
            }
            Expression secondExpression = null;
            PrefixExpression negateSecondExpression = ASTNodes.as(rightOperand, PrefixExpression.class);
            boolean isPositiveExpression;
            if (negateSecondExpression != null && ASTNodes.hasOperator(negateSecondExpression, PrefixExpression.Operator.NOT)) {
                secondExpression = negateSecondExpression.getOperand();
                isPositiveExpression = false;
            } else {
                secondExpression = rightOperand;
                isPositiveExpression = true;
            }
            if (firstExpression != null && ASTNodes.hasType(firstExpression, Boolean.class.getCanonicalName()) && ASTNodes.isPassive(firstExpression) && ASTNodes.match(firstExpression, secondExpression)) {
                replaceNullCheck(visited, firstExpression, isNullCheck, isAndExpression, isPositiveExpression);
                return false;
            }
        }
    }
    return true;
}
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) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 55 with PrefixExpression

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

the class DoWhileRatherThanWhileCleanUp method peremptoryValue.

private Object peremptoryValue(final ASTNode visited, final Expression condition) {
    Object constantCondition = condition.resolveConstantExpressionValue();
    if (constantCondition != null) {
        return constantCondition;
    }
    Long integerLiteral = ASTNodes.getIntegerLiteral(condition);
    if (integerLiteral != null) {
        return integerLiteral;
    }
    SimpleName variable = ASTNodes.as(condition, SimpleName.class);
    if (variable != null && variable.resolveBinding() != null && variable.resolveBinding().getKind() == IBinding.VARIABLE) {
        List<ASTNode> precedingStatements = getPrecedingCode(visited);
        Collections.reverse(precedingStatements);
        for (ASTNode precedingStatement : precedingStatements) {
            if (isConditionalCode(precedingStatement)) {
                return null;
            }
            VarDefinitionsUsesVisitor visitor = new VarDefinitionsUsesVisitor((IVariableBinding) variable.resolveBinding(), precedingStatement, true);
            if (visitor.getWrites().size() > 1) {
                return null;
            }
            for (SimpleName astNode : visitor.getReads()) {
                ASTNode parent = astNode.getParent();
                while (parent instanceof ParenthesizedExpression) {
                    parent = astNode.getParent();
                }
                if (parent instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) parent, PrefixExpression.Operator.INCREMENT, PrefixExpression.Operator.DECREMENT) || parent instanceof PostfixExpression && ASTNodes.hasOperator((PostfixExpression) parent, PostfixExpression.Operator.INCREMENT, PostfixExpression.Operator.DECREMENT)) {
                    return null;
                }
            }
            if (!visitor.getWrites().isEmpty()) {
                SimpleName write = visitor.getWrites().get(0);
                ASTNode parent = write;
                while (parent != precedingStatement) {
                    if (isConditionalCode(parent)) {
                        return null;
                    }
                    parent = parent.getParent();
                }
                switch(write.getParent().getNodeType()) {
                    case ASTNode.ASSIGNMENT:
                        Assignment assignment = (Assignment) write.getParent();
                        if (ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN)) {
                            return peremptoryValue(precedingStatement, assignment.getRightHandSide());
                        }
                        break;
                    case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
                        VariableDeclarationFragment fragment = (VariableDeclarationFragment) write.getParent();
                        if (fragment.getInitializer() != null) {
                            return peremptoryValue(precedingStatement, fragment.getInitializer());
                        }
                        break;
                    case ASTNode.SINGLE_VARIABLE_DECLARATION:
                        SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) write.getParent();
                        if (singleVariableDeclaration.getInitializer() != null) {
                            return peremptoryValue(precedingStatement, singleVariableDeclaration.getInitializer());
                        }
                        break;
                    default:
                        break;
                }
                return null;
            }
        }
        return null;
    }
    InfixExpression infixExpression = ASTNodes.as(condition, InfixExpression.class);
    if (infixExpression != null) {
        if (!infixExpression.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.GREATER, InfixExpression.Operator.GREATER_EQUALS, InfixExpression.Operator.LESS, InfixExpression.Operator.LESS_EQUALS)) {
            Object leftOperand = peremptoryValue(visited, infixExpression.getLeftOperand());
            Object rightOperand = peremptoryValue(visited, infixExpression.getRightOperand());
            if (leftOperand instanceof Number && rightOperand instanceof Number) {
                Number leftNumber = (Number) leftOperand;
                Number rightNumber = (Number) rightOperand;
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.EQUALS)) {
                    return leftNumber.longValue() == rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.NOT_EQUALS)) {
                    return leftNumber.longValue() != rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.GREATER)) {
                    return leftNumber.longValue() > rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.GREATER_EQUALS)) {
                    return leftNumber.longValue() >= rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.LESS)) {
                    return leftNumber.longValue() < rightNumber.longValue();
                }
                if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.LESS_EQUALS)) {
                    return leftNumber.longValue() <= rightNumber.longValue();
                }
            }
            return null;
        }
        if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
            for (Expression operand : ASTNodes.allOperands(infixExpression)) {
                final Object hasAlwaysValue = peremptoryValue(visited, operand);
                if (!Boolean.TRUE.equals(hasAlwaysValue)) {
                    return hasAlwaysValue;
                }
            }
            return Boolean.TRUE;
        }
        if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
            for (Expression operand : ASTNodes.allOperands(infixExpression)) {
                final Object hasAlwaysValue = peremptoryValue(visited, operand);
                if (!Boolean.FALSE.equals(hasAlwaysValue)) {
                    return hasAlwaysValue;
                }
            }
            return Boolean.FALSE;
        }
    }
    return false;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Assignment(org.eclipse.jdt.core.dom.Assignment) VarDefinitionsUsesVisitor(org.autorefactor.jdt.internal.corext.dom.VarDefinitionsUsesVisitor) 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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) 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)

Aggregations

PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)64 Expression (org.eclipse.jdt.core.dom.Expression)51 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)50 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)31 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)24 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)16 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 AST (org.eclipse.jdt.core.dom.AST)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Assignment (org.eclipse.jdt.core.dom.Assignment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)7 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6