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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations