use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class OperatorEnumTest method simpleTestCompareExpressions.
@Test
public void simpleTestCompareExpressions() {
@SuppressWarnings("deprecation") final AST ast = AST.newAST(AST.JLS8);
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);
// $NON-NLS-1$
assertEquals("Comparing unknown objects result in no decision", 0, OperatorEnum.compareTo(op1, null));
}
use of org.eclipse.jdt.core.dom.InfixExpression 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.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class NoLoopIterationRatherThanEmptyCheckCleanUp method isConditionValid.
private boolean isConditionValid(final InfixExpression condition, final Expression container, final Expression arrayOperand, final Expression literalOperand, final boolean isArrayOnLeft) {
Expression array = getArray(container, arrayOperand);
Long literal = ASTNodes.getIntegerLiteral(literalOperand);
if (array != null && literal != null) {
long value = literal;
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.NOT_EQUALS)) {
return value == 0;
}
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.GREATER)) {
return isArrayOnLeft && value == 0;
}
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.GREATER_EQUALS)) {
return isArrayOnLeft && value == 1;
}
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.LESS)) {
return !isArrayOnLeft && value == 0;
}
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.LESS_EQUALS)) {
return !isArrayOnLeft && value == 1;
}
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class NoLoopIterationRatherThanEmptyCheckCleanUp method removeCondition.
private void removeCondition(final InfixExpression condition, final List<Expression> operands) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.NoLoopIterationRatherThanEmptyCheckCleanUp_description);
if (operands.size() == 2) {
rewrite.replace(condition, ASTNodes.createMoveTarget(rewrite, operands.get(0)), group);
} else {
operands.remove(operands.size() - 1);
InfixExpression newCondition = ast.newInfixExpression(condition.getOperator(), ASTNodes.createMoveTarget(rewrite, operands));
rewrite.replace(condition, newCondition, group);
}
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class NoLoopIterationRatherThanEmptyCheckCleanUp method visit.
@Override
public boolean visit(final IfStatement visited) {
if (visited.getElseStatement() == null) {
List<Statement> statements = ASTNodes.asList(visited.getThenStatement());
if (statements != null && statements.size() == 1) {
Expression container = getContainer(statements);
if (ASTNodes.isArray(container) && ASTNodes.isPassive(container)) {
InfixExpression condition = ASTNodes.as(visited.getExpression(), InfixExpression.class);
if (isConditionValid(condition, container)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.NoLoopIterationRatherThanEmptyCheckCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, statements.get(0)), group);
return false;
}
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
List<Expression> operands = ASTNodes.allOperands(condition);
Expression operand = ASTNodes.as(operands.get(operands.size() - 1), InfixExpression.class);
if (isConditionValid(operand, container)) {
removeCondition(condition, operands);
return false;
}
}
}
}
}
return true;
}
Aggregations