Search in sources :

Example 1 with OrderedInfixExpression

use of org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression in project AutoRefactor by JnRouvignac.

the class MatchingStreamRatherThanCountCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    OrderedInfixExpression<MethodInvocation, Expression> orderedCondition = ASTNodes.orderedInfix(visited, MethodInvocation.class, Expression.class);
    if (orderedCondition != null) {
        MethodInvocation countMethod = orderedCondition.getFirstOperand();
        Long literalCount = ASTNodes.getIntegerLiteral(orderedCondition.getSecondOperand());
        MethodInvocation filterMethod = ASTNodes.as(countMethod.getExpression(), MethodInvocation.class);
        if (literalCount != null && filterMethod != null && filterMethod.getExpression() != null && !ASTNodes.is(filterMethod.getExpression(), ThisExpression.class) && (ASTNodes.usesGivenSignature(countMethod, Stream.class.getCanonicalName(), COUNT_METHOD) || ASTNodes.usesGivenSignature(countMethod, IntStream.class.getCanonicalName(), COUNT_METHOD) || ASTNodes.usesGivenSignature(countMethod, LongStream.class.getCanonicalName(), COUNT_METHOD) || ASTNodes.usesGivenSignature(countMethod, DoubleStream.class.getCanonicalName(), COUNT_METHOD)) && (ASTNodes.usesGivenSignature(filterMethod, Stream.class.getCanonicalName(), FILTER_METHOD, Predicate.class.getCanonicalName()) || ASTNodes.usesGivenSignature(filterMethod, IntStream.class.getCanonicalName(), FILTER_METHOD, IntPredicate.class.getCanonicalName()) || ASTNodes.usesGivenSignature(filterMethod, LongStream.class.getCanonicalName(), FILTER_METHOD, LongPredicate.class.getCanonicalName()) || ASTNodes.usesGivenSignature(filterMethod, DoubleStream.class.getCanonicalName(), FILTER_METHOD, DoublePredicate.class.getCanonicalName()))) {
            LambdaExpression predicate = ASTNodes.as((Expression) filterMethod.arguments().get(0), LambdaExpression.class);
            if (predicate != null && ASTNodes.isPassiveWithoutFallingThrough(predicate.getBody())) {
                if (Long.valueOf(0L).equals(literalCount)) {
                    if (Arrays.asList(InfixExpression.Operator.GREATER, InfixExpression.Operator.NOT_EQUALS).contains(orderedCondition.getOperator())) {
                        replaceMethod(visited, filterMethod, true);
                        return false;
                    }
                    if (Arrays.asList(InfixExpression.Operator.EQUALS, InfixExpression.Operator.LESS_EQUALS).contains(orderedCondition.getOperator())) {
                        replaceMethod(visited, filterMethod, false);
                        return false;
                    }
                } else if (Long.valueOf(1L).equals(literalCount)) {
                    if (InfixExpression.Operator.GREATER_EQUALS.equals(orderedCondition.getOperator())) {
                        replaceMethod(visited, filterMethod, true);
                        return false;
                    }
                    if (InfixExpression.Operator.LESS.equals(orderedCondition.getOperator())) {
                        replaceMethod(visited, filterMethod, false);
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IntStream(java.util.stream.IntStream) LongStream(java.util.stream.LongStream) DoubleStream(java.util.stream.DoubleStream) Stream(java.util.stream.Stream) DoubleStream(java.util.stream.DoubleStream) LongStream(java.util.stream.LongStream) IntStream(java.util.stream.IntStream) LongPredicate(java.util.function.LongPredicate) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 2 with OrderedInfixExpression

use of org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression in project AutoRefactor by JnRouvignac.

the class ObsoleteComparisonCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    OrderedInfixExpression<MethodInvocation, Expression> orderedCondition = ASTNodes.orderedInfix(visited, MethodInvocation.class, Expression.class);
    if (orderedCondition != null && Arrays.asList(InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS).contains(orderedCondition.getOperator())) {
        MethodInvocation comparisonMI = orderedCondition.getFirstOperand();
        Long literalValue = ASTNodes.getIntegerLiteral(orderedCondition.getSecondOperand());
        if (literalValue != null && literalValue.compareTo(0L) != 0 && comparisonMI.getExpression() != null && !ASTNodes.is(comparisonMI.getExpression(), ThisExpression.class) && (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(comparisonMI, Comparable.class.getCanonicalName(), "compareTo", Object.class.getCanonicalName()) || // $NON-NLS-1$
        ASTNodes.usesGivenSignature(comparisonMI, Comparator.class.getCanonicalName(), "compare", Object.class.getCanonicalName(), Object.class.getCanonicalName()) || getJavaMinorVersion() >= 2 && ASTNodes.usesGivenSignature(comparisonMI, String.class.getCanonicalName(), "compareToIgnoreCase", String.class.getCanonicalName()))) {
            // $NON-NLS-1$
            if (literalValue.compareTo(0L) < 0) {
                if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) {
                    refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.LESS);
                } else {
                    refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.GREATER_EQUALS);
                }
            } else if (InfixExpression.Operator.EQUALS.equals(orderedCondition.getOperator())) {
                refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.GREATER);
            } else {
                refactorComparingToZero(visited, comparisonMI, InfixExpression.Operator.LESS_EQUALS);
            }
            return false;
        }
    }
    return true;
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Comparator(java.util.Comparator)

Example 3 with OrderedInfixExpression

use of org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression in project AutoRefactor by JnRouvignac.

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeRefactorIfStatement.

private boolean maybeRefactorIfStatement(final IfStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    if (node.getElseStatement() != null) {
        InfixExpression condition = ASTNodes.as(node.getExpression(), InfixExpression.class);
        List<Statement> thenStatements = ASTNodes.asList(node.getThenStatement());
        List<Statement> elseStatements = ASTNodes.asList(node.getElseStatement());
        if (condition != null && !condition.hasExtendedOperands() && ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS) && thenStatements != null && thenStatements.size() == 1 && elseStatements != null && elseStatements.size() == 1) {
            OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(condition, Expression.class, NullLiteral.class);
            if (nullityOrderedCondition != null && ASTNodes.isPassive(nullityOrderedCondition.getFirstOperand())) {
                return maybeReplaceCode(node, condition, thenStatements, elseStatements, nullityOrderedCondition.getFirstOperand(), classesToUseWithImport, importsToAdd);
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 4 with OrderedInfixExpression

use of org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression in project AutoRefactor by JnRouvignac.

the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceEquals.

private boolean maybeReplaceEquals(final IfStatement node, final Expression firstField, final InfixExpression nullityCondition, final ReturnStatement returnStatement1, final PrefixExpression equalsCondition, final ReturnStatement returnStatement2, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(nullityCondition, Expression.class, NullLiteral.class);
    MethodInvocation equalsMethod = ASTNodes.as(equalsCondition.getOperand(), MethodInvocation.class);
    if (nullityOrderedCondition != null && returnStatement1 != null && returnStatement2 != null && equalsMethod != null && equalsMethod.getExpression() != null && EQUALS_METHOD.equals(equalsMethod.getName().getIdentifier()) && equalsMethod.arguments() != null && equalsMethod.arguments().size() == 1) {
        Expression secondField = nullityOrderedCondition.getFirstOperand();
        if (secondField != null && (match(firstField, secondField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)) || match(secondField, firstField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)))) {
            BooleanLiteral returnFalse1 = ASTNodes.as(returnStatement1.getExpression(), BooleanLiteral.class);
            BooleanLiteral returnFalse2 = ASTNodes.as(returnStatement2.getExpression(), BooleanLiteral.class);
            if (returnFalse1 != null && !returnFalse1.booleanValue() && returnFalse2 != null && !returnFalse2.booleanValue()) {
                replaceEquals(node, firstField, classesToUseWithImport, importsToAdd, secondField, returnStatement1);
                return false;
            }
        }
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 5 with OrderedInfixExpression

use of org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression in project AutoRefactor by JnRouvignac.

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isGreatNumberValid.

private boolean isGreatNumberValid(final CollectedData data, final CastExpression newHash) {
    OrderedInfixExpression<Expression, InfixExpression> orderedBitwise = ASTNodes.orderedInfix(newHash.getExpression(), Expression.class, InfixExpression.class);
    if (ASTNodes.hasType(newHash, int.class.getSimpleName()) && orderedBitwise != null && ASTNodes.hasType(newHash.getExpression(), long.class.getSimpleName(), double.class.getSimpleName()) && InfixExpression.Operator.XOR.equals(orderedBitwise.getOperator())) {
        SimpleName field = getField(orderedBitwise.getFirstOperand());
        InfixExpression moveExpression = orderedBitwise.getSecondOperand();
        if (field != null && moveExpression != null && !ASTNodes.isSameVariable(field, data.getPrimeId()) && !ASTNodes.isSameVariable(field, data.getResultId()) && ASTNodes.hasOperator(moveExpression, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED)) {
            SimpleName againFieldName = getField(moveExpression.getLeftOperand());
            Long hash = ASTNodes.getIntegerLiteral(moveExpression.getRightOperand());
            if (againFieldName != null && ASTNodes.isSameVariable(againFieldName, field) && Long.valueOf(32L).equals(hash)) {
                if (data.isTempValueUsed()) {
                    data.getFields().add(ASTNodes.getUnparenthesedExpression(againFieldName));
                    return true;
                }
                if (ASTNodes.isSameVariable(data.getTempVar(), field)) {
                    data.setTempValueUsed(true);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression)

Aggregations

OrderedInfixExpression (org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression)8 Expression (org.eclipse.jdt.core.dom.Expression)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)5 NullLiteral (org.eclipse.jdt.core.dom.NullLiteral)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 LongPredicate (java.util.function.LongPredicate)1 DoubleStream (java.util.stream.DoubleStream)1 IntStream (java.util.stream.IntStream)1 LongStream (java.util.stream.LongStream)1 Stream (java.util.stream.Stream)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1