use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ForLoopHelper method getIndexOnIterable.
private static ForLoopContent getIndexOnIterable(final Expression condition, Name loopVariable) {
final InfixExpression ie = as(condition, InfixExpression.class);
if (ie != null && !ie.hasExtendedOperands()) {
final Expression leftOp = ie.getLeftOperand();
final Expression rightOp = ie.getRightOperand();
if (hasOperator(ie, LESS)) {
return buildForLoopContent(loopVariable, rightOp);
} else if (hasOperator(ie, GREATER)) {
return buildForLoopContent(loopVariable, leftOp);
}
}
return null;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class AbstractUnitTestRefactoring method maybeRefactorComparison.
private boolean maybeRefactorComparison(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression ie, final boolean isAssertEquals, final Expression failureMessage, final boolean isRewriteNeeded) {
final Pair<Expression, Expression> actualAndExpected = getActualAndExpected(ie.getLeftOperand(), ie.getRightOperand());
if (isComparingObjects(ie) && !isNullLiteral(ie.getLeftOperand()) && !isNullLiteral(ie.getRightOperand())) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Same"), b.copy(actualAndExpected.getFirst()), b.copy(actualAndExpected.getSecond()), failureMessage);
r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
return DO_NOT_VISIT_SUBTREE;
} else {
return maybeRefactorToAssertEquals(nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
}
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ORConditionRatherThanRedundantClausesRefactoring method replaceDuplicateExpr.
private void replaceDuplicateExpr(final InfixExpression node, final Operator operator, final Expression leftExpr, final Expression rightExpr, final boolean isLeftExprPositive, final boolean isRightExprPositive, final boolean forward) {
final ASTBuilder b = ctx.getASTBuilder();
Expression copyOfLeftExpr = b.copy(leftExpr);
if (!isLeftExprPositive) {
copyOfLeftExpr = b.not(copyOfLeftExpr);
}
Expression copyOfRightExpr = b.copy(rightExpr);
if (!isRightExprPositive) {
copyOfRightExpr = b.not(copyOfRightExpr);
}
if (forward) {
ctx.getRefactorings().replace(node, b.infixExpr(copyOfLeftExpr, operator, copyOfRightExpr));
} else {
ctx.getRefactorings().replace(node, b.infixExpr(copyOfRightExpr, operator, copyOfLeftExpr));
}
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ORConditionRatherThanRedundantClausesRefactoring method maybeRefactorCondition.
private boolean maybeRefactorCondition(final InfixExpression node, final Operator operator, final Expression leftOperand, final Expression rightOperand, final boolean forward) {
final InfixExpression complexCondition = as(leftOperand, InfixExpression.class);
if (complexCondition != null && !complexCondition.hasExtendedOperands() && (hasOperator(complexCondition, CONDITIONAL_AND) || hasOperator(complexCondition, AND))) {
final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
final Expression firstExpr = getBasisExpression(complexCondition.getLeftOperand(), isFirstExprPositive);
final Expression secondExpr = getBasisExpression(complexCondition.getRightOperand(), isSecondExprPositive);
final Expression thirdExpr = getBasisExpression(rightOperand, isThirdExprPositive);
if (isPrimitive(firstExpr) && isPrimitive(secondExpr) && isPrimitive(thirdExpr)) {
if (match(new ASTMatcher(), secondExpr, thirdExpr)) {
replaceDuplicateExpr(node, operator, firstExpr, thirdExpr, isFirstExprPositive.get(), isThirdExprPositive.get(), forward);
return DO_NOT_VISIT_SUBTREE;
} else if (match(new ASTMatcher(), firstExpr, thirdExpr)) {
replaceDuplicateExpr(node, operator, secondExpr, thirdExpr, isSecondExprPositive.get(), isThirdExprPositive.get(), forward);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class RemoveUnnecessaryCastRefactoring method canRemoveCast.
private boolean canRemoveCast(CastExpression node) {
final ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case RETURN_STATEMENT:
final MethodDeclaration md = getAncestor(parent, MethodDeclaration.class);
return isAssignmentCompatible(node.getExpression(), md.getReturnType2());
case ASSIGNMENT:
final Assignment as = (Assignment) parent;
return isAssignmentCompatible(node.getExpression(), as) || isConstantExpressionAssignmentConversion(node);
case VARIABLE_DECLARATION_FRAGMENT:
final VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
return isAssignmentCompatible(node.getExpression(), resolveTypeBinding(vdf)) || isConstantExpressionAssignmentConversion(node);
case INFIX_EXPRESSION:
final InfixExpression ie = (InfixExpression) parent;
final Expression lo = ie.getLeftOperand();
final Expression ro = ie.getRightOperand();
if (node.equals(lo)) {
return (isStringConcat(ie) || isAssignmentCompatible(node.getExpression(), ro)) && !isPrimitiveTypeNarrowing(node) && !hasOperator(ie, DIVIDE) && !hasOperator(ie, PLUS) && !hasOperator(ie, MINUS);
} else {
final boolean integralDivision = isIntegralDivision(ie);
return ((isNotRefactored(lo) && isStringConcat(ie)) || (!integralDivision && isAssignmentCompatibleInInfixExpression(node, ie)) || (integralDivision && canRemoveCastInIntegralDivision(node, ie))) && !isPrimitiveTypeNarrowing(node) && !isIntegralDividedByFloatingPoint(node, ie);
}
}
return false;
}
Aggregations