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