Search in sources :

Example 6 with OrderedInfixExpression

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

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isObjectValid.

private boolean isObjectValid(final CollectedData data, final ConditionalExpression condition) {
    OrderedInfixExpression<Expression, NullLiteral> orderedIsFieldNull = ASTNodes.orderedInfix(condition.getExpression(), Expression.class, NullLiteral.class);
    if (orderedIsFieldNull != null && Arrays.asList(InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS).contains(orderedIsFieldNull.getOperator())) {
        SimpleName field = getField(orderedIsFieldNull.getFirstOperand());
        if (field != null) {
            Long zero;
            MethodInvocation hashOnField;
            if (InfixExpression.Operator.EQUALS.equals(orderedIsFieldNull.getOperator())) {
                zero = ASTNodes.getIntegerLiteral(condition.getThenExpression());
                hashOnField = ASTNodes.as(condition.getElseExpression(), MethodInvocation.class);
            } else {
                hashOnField = ASTNodes.as(condition.getThenExpression(), MethodInvocation.class);
                zero = ASTNodes.getIntegerLiteral(condition.getElseExpression());
            }
            if (Long.valueOf(0L).equals(zero) && hashOnField != null && hashOnField.getExpression() != null && HASH_CODE_METHOD.equals(hashOnField.getName().getIdentifier()) && Utils.isEmpty(hashOnField.arguments())) {
                SimpleName fieldToHash = getField(hashOnField.getExpression());
                if (fieldToHash != null && ASTNodes.isSameVariable(field, fieldToHash)) {
                    data.getFields().add(ASTNodes.getUnparenthesedExpression(fieldToHash));
                    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) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 7 with OrderedInfixExpression

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

the class IsEmptyRatherThanSizeCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    OrderedInfixExpression<MethodInvocation, Expression> orderedCondition = ASTNodes.orderedInfix(visited, MethodInvocation.class, Expression.class);
    if (orderedCondition != null) {
        MethodInvocation miToReplace = orderedCondition.getFirstOperand();
        Long literalSize = ASTNodes.getIntegerLiteral(orderedCondition.getSecondOperand());
        if (literalSize != null && miToReplace.getExpression() != null && !ASTNodes.is(miToReplace.getExpression(), ThisExpression.class) && (ASTNodes.usesGivenSignature(miToReplace, Collection.class.getCanonicalName(), SIZE_METHOD) || ASTNodes.usesGivenSignature(miToReplace, Map.class.getCanonicalName(), SIZE_METHOD) || ASTNodes.usesGivenSignature(miToReplace, String.class.getCanonicalName(), LENGTH_METHOD) && getJavaMinorVersion() >= 6)) {
            if (Long.valueOf(0L).equals(literalSize)) {
                if (Arrays.asList(InfixExpression.Operator.GREATER, InfixExpression.Operator.NOT_EQUALS).contains(orderedCondition.getOperator())) {
                    replaceMethod(visited, miToReplace, false);
                    return false;
                }
                if (Arrays.asList(InfixExpression.Operator.EQUALS, InfixExpression.Operator.LESS_EQUALS).contains(orderedCondition.getOperator())) {
                    replaceMethod(visited, miToReplace, true);
                    return false;
                }
            } else if (Long.valueOf(1L).equals(literalSize)) {
                if (InfixExpression.Operator.GREATER_EQUALS.equals(orderedCondition.getOperator())) {
                    replaceMethod(visited, miToReplace, false);
                    return false;
                }
                if (InfixExpression.Operator.LESS.equals(orderedCondition.getOperator())) {
                    replaceMethod(visited, miToReplace, true);
                    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) Collection(java.util.Collection) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Map(java.util.Map)

Example 8 with OrderedInfixExpression

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

the class UseStringContainsCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    OrderedInfixExpression<MethodInvocation, Expression> orderedExpression = ASTNodes.orderedInfix(visited, MethodInvocation.class, Expression.class);
    if (orderedExpression != null) {
        MethodInvocation indexOf = orderedExpression.getFirstOperand();
        Long value = ASTNodes.getIntegerLiteral(orderedExpression.getSecondOperand());
        if (value != null && (// $NON-NLS-1$
        ASTNodes.usesGivenSignature(indexOf, String.class.getCanonicalName(), "indexOf", String.class.getCanonicalName()) || ASTNodes.usesGivenSignature(indexOf, String.class.getCanonicalName(), "lastIndexOf", String.class.getCanonicalName()))) {
            if (is(orderedExpression.getOperator(), InfixExpression.Operator.GREATER_EQUALS, value, 0) || is(orderedExpression.getOperator(), InfixExpression.Operator.NOT_EQUALS, value, -1)) {
                replaceWithStringContains(visited, indexOf, true);
                return false;
            }
            if (is(orderedExpression.getOperator(), InfixExpression.Operator.LESS, value, 0) || is(orderedExpression.getOperator(), InfixExpression.Operator.EQUALS, value, -1)) {
                replaceWithStringContains(visited, indexOf, false);
                return false;
            }
        }
    }
    return true;
}
Also used : OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

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