Search in sources :

Example 81 with InfixExpression

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));
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AST(org.eclipse.jdt.core.dom.AST) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Test(org.junit.Test)

Example 82 with InfixExpression

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;
}
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 83 with InfixExpression

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;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression)

Example 84 with InfixExpression

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);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 85 with InfixExpression

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;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)196 Expression (org.eclipse.jdt.core.dom.Expression)137 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)85 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)67 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)55 CastExpression (org.eclipse.jdt.core.dom.CastExpression)47 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)37 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)34 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)31 AST (org.eclipse.jdt.core.dom.AST)29 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)25 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)24 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)24 TextEditGroup (org.eclipse.text.edits.TextEditGroup)22 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Assignment (org.eclipse.jdt.core.dom.Assignment)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)17