Search in sources :

Example 71 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.

the class SeparateAssertionsRatherThanBooleanExpressionCleanUp method maybeRefactorMethod.

private boolean maybeRefactorMethod(final ExpressionStatement visited, final MethodInvocation originalMethod, final InfixExpression.Operator operator, final int parameterIndex) {
    InfixExpression booleanExpression = ASTNodes.as((Expression) originalMethod.arguments().get(parameterIndex), InfixExpression.class);
    if (booleanExpression != null && ASTNodes.hasOperator(booleanExpression, operator)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.SeparateAssertionsRatherThanBooleanExpressionCleanUp_description);
        List<Expression> allOperands = ASTNodes.allOperands(booleanExpression);
        rewrite.replace(booleanExpression, ASTNodes.createMoveTarget(rewrite, allOperands.remove(0)), group);
        List<Statement> expressionStatements = new ArrayList<>(allOperands.size());
        for (Expression operand : allOperands) {
            List<Expression> newArguments = new ArrayList<>(originalMethod.arguments().size());
            for (Object argument : originalMethod.arguments()) {
                newArguments.add(rewrite.createCopyTarget(ASTNodes.getUnparenthesedExpression((Expression) argument)));
            }
            newArguments.set(parameterIndex, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(operand)));
            MethodInvocation newMethod;
            if (originalMethod.getExpression() != null) {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(rewrite.createCopyTarget(originalMethod.getExpression()));
                newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
                newMethodInvocation.arguments().addAll(newArguments);
                newMethod = newMethodInvocation;
            } else {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(null);
                newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
                newMethodInvocation.arguments().addAll(newArguments);
                newMethod = newMethodInvocation;
            }
            ExpressionStatement newStatement = ast.newExpressionStatement(newMethod);
            expressionStatements.add(newStatement);
        }
        if (ASTNodes.canHaveSiblings(visited)) {
            Collections.reverse(expressionStatements);
            for (Statement expressionStatement : expressionStatements) {
                rewrite.insertAfter(expressionStatement, visited, group);
            }
        } else {
            expressionStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
            Block newBlock = ast.newBlock();
            newBlock.statements().addAll(expressionStatements);
            ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
        }
        return false;
    }
    return true;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 72 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.

the class StringCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (ASTNodes.hasOperator(visited, InfixExpression.Operator.PLUS)) {
        List<Expression> allOperands = ASTNodes.allOperands(visited);
        for (int i = 0; i < allOperands.size(); i++) {
            Expression operand = allOperands.get(i);
            MethodInvocation valueOfMethod = ASTNodes.as(operand, MethodInvocation.class);
            if (valueOfMethod != null && (isStringValueOf(valueOfMethod) || isToStringForPrimitive(valueOfMethod))) {
                // If node is in a String context, no need to call toString()
                if (i == 0) {
                    if (ASTNodes.hasType(visited.getRightOperand(), String.class.getCanonicalName()) && !maybeReplaceStringValueOfByArg0(visited.getLeftOperand(), valueOfMethod)) {
                        return false;
                    }
                } else if (i == 1) {
                    if (ASTNodes.hasType(visited.getLeftOperand(), String.class.getCanonicalName()) && !maybeReplaceStringValueOfByArg0(visited.getRightOperand(), valueOfMethod)) {
                        return false;
                    }
                } else if (!maybeReplaceStringValueOfByArg0(valueOfMethod, valueOfMethod)) {
                    // Left or right operation is necessarily a string, so just replace
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 73 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.

the class ASTNodes method allOperands.

/**
 * Returns a copy of all the operands from the provided infix expressions.
 * It takes a bug into account.
 * In some cases, ASTConverter.java creates several infix expressions instead of one extended infix expression.
 * It occurs for an expression with a sub-infix-expression in the middle without parenthesis.
 *
 * @param node the infix expression
 * @return a List of expressions
 */
public static List<Expression> allOperands(final InfixExpression node) {
    List<Expression> extOps = node.extendedOperands();
    List<Expression> operands = new ArrayList<>(2 + extOps.size());
    operands.add(node.getLeftOperand());
    operands.add(node.getRightOperand());
    operands.addAll(extOps);
    List<Expression> optimizedOperands = new ArrayList<>();
    for (Expression expression : operands) {
        if (expression instanceof InfixExpression && hasOperator((InfixExpression) expression, node.getOperator())) {
            optimizedOperands.addAll(allOperands((InfixExpression) expression));
        } else {
            optimizedOperands.add(expression);
        }
    }
    return optimizedOperands;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayList(java.util.ArrayList) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 74 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.

the class ASTNodes method orderedInfix.

/**
 * Return the items of an infix expression in the order it is specified. It reverses the operator if needed.
 *
 * @param <F>        the required expression type
 * @param <S>        the required expression type
 * @param node       the supposed infix expression
 * @param firstClass  the class representing the required expression type
 * @param secondClass  the class representing the required expression type
 * @return the items of an infix expression in the order it is specified. It reverses the operator if needed.
 */
public static <F extends Expression, S extends Expression> OrderedInfixExpression<F, S> orderedInfix(final Expression node, final Class<F> firstClass, final Class<S> secondClass) {
    InfixExpression expression = as(node, InfixExpression.class);
    if (expression == null || expression.hasExtendedOperands()) {
        return null;
    }
    if (Utils.equalNotNull(firstClass, secondClass)) {
        F first = as(expression.getLeftOperand(), firstClass);
        S second = as(expression.getRightOperand(), secondClass);
        if (first != null && second != null) {
            return new OrderedInfixExpression<>(first, expression.getOperator(), second);
        }
    } else {
        F leftFirst = as(expression.getLeftOperand(), firstClass);
        S rightSecond = as(expression.getRightOperand(), secondClass);
        if (leftFirst != null && rightSecond != null) {
            return new OrderedInfixExpression<>(leftFirst, expression.getOperator(), rightSecond);
        }
        InfixExpression.Operator mirroredOperator = mirrorOperator(expression.getOperator());
        if (mirroredOperator != null) {
            F rightFirst = as(expression.getRightOperand(), firstClass);
            S leftSecond = as(expression.getLeftOperand(), secondClass);
            if (rightFirst != null && leftSecond != null) {
                return new OrderedInfixExpression<>(rightFirst, mirroredOperator, leftSecond);
            }
        }
    }
    return null;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 75 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.

the class ASTNodes method getNbOperands.

/**
 * Returns the number of logical operands in the expression.
 *
 * @param node The expression
 * @return the number of logical operands in the expression
 */
public static int getNbOperands(final Expression node) {
    InfixExpression infixExpression = as(node, InfixExpression.class);
    if (infixExpression == null || !hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR) && (!hasOperator(infixExpression, InfixExpression.Operator.AND, InfixExpression.Operator.OR, InfixExpression.Operator.XOR) || !hasType(infixExpression.getLeftOperand(), boolean.class.getCanonicalName(), Boolean.class.getCanonicalName()))) {
        return 1;
    }
    int nbOperands = 0;
    for (Expression operand : allOperands(infixExpression)) {
        nbOperands += getNbOperands(operand);
    }
    return nbOperands;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

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