use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method getBasisExpression.
private Expression getBasisExpression(final Expression originalExpr, final AtomicBoolean isExprPositive) {
Expression basisExpr = null;
final PrefixExpression negateExpr = as(originalExpr, PrefixExpression.class);
if (hasOperator(negateExpr, NOT)) {
basisExpr = negateExpr.getOperand();
isExprPositive.set(false);
} else {
basisExpr = originalExpr;
isExprPositive.set(true);
}
return basisExpr;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method getExpressionWithoutParentheses.
private Expression getExpressionWithoutParentheses(ParenthesizedExpression node) {
final ASTNode parent = node.getParent();
final Expression innerExpr = node.getExpression();
if (innerExpr instanceof ParenthesizedExpression) {
return innerExpr;
}
if (parent instanceof InfixExpression) {
final InfixExpression parentInfixExpr = (InfixExpression) parent;
if (innerExpr instanceof InfixExpression) {
final Operator innerOp = ((InfixExpression) innerExpr).getOperator();
if (innerOp == parentInfixExpr.getOperator() && OperatorEnum.isAssociative(innerOp) && // to other if statements in this method.
equalNotNull(innerExpr.resolveTypeBinding(), parentInfixExpr.resolveTypeBinding())) {
return innerExpr;
}
}
}
// Infix, prefix or postfix without parenthesis is not readable
if ((parent instanceof InfixExpression && (InfixExpression.Operator.PLUS.equals(((InfixExpression) parent).getOperator()) || InfixExpression.Operator.MINUS.equals(((InfixExpression) parent).getOperator()))) || (parent instanceof PrefixExpression && (PLUS.equals(((PrefixExpression) parent).getOperator()) || MINUS.equals(((PrefixExpression) parent).getOperator())))) {
if (innerExpr instanceof PrefixExpression && (DECREMENT.equals(((PrefixExpression) innerExpr).getOperator()) || INCREMENT.equals(((PrefixExpression) innerExpr).getOperator()) || PLUS.equals(((PrefixExpression) innerExpr).getOperator()) || MINUS.equals(((PrefixExpression) innerExpr).getOperator()))) {
return node;
}
if (innerExpr instanceof PostfixExpression && (PostfixExpression.Operator.DECREMENT.equals(((PostfixExpression) innerExpr).getOperator()) || PostfixExpression.Operator.INCREMENT.equals(((PostfixExpression) innerExpr).getOperator()))) {
return node;
}
}
if (isInnerExprHardToRead(innerExpr, parent)) {
// return (bla != null) ? bla.getSomething() : null;
return node;
}
if (isUselessParenthesesInStatement(parent, node)) {
return innerExpr;
}
final int compareTo = OperatorEnum.compareTo(innerExpr, parent);
if (compareTo < 0) {
return node;
} else if (compareTo > 0) {
return innerExpr;
}
if (// some like it like that
innerExpr instanceof InfixExpression || // or if it can be removed.
innerExpr instanceof CastExpression || // infix and prefix or postfix without parenthesis is not readable
((parent instanceof InfixExpression || parent instanceof PrefixExpression || parent instanceof PostfixExpression) && (innerExpr instanceof PrefixExpression || innerExpr instanceof PostfixExpression))) {
return node;
}
return innerExpr;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class SimplifyExpressionRefactoring method maybeReduceBooleanExpression.
private boolean maybeReduceBooleanExpression(final InfixExpression node, final Expression leftExpr, final Expression rightExpr) {
final Boolean leftBoolean = getBooleanLiteral(leftExpr);
final Boolean rightBoolean = getBooleanLiteral(rightExpr);
if (leftBoolean != null) {
return replace(node, leftBoolean.booleanValue(), rightExpr);
} else if (rightBoolean != null) {
return replace(node, rightBoolean.booleanValue(), leftExpr);
}
Expression leftOppositeExpr = null;
final PrefixExpression leftPrefix = as(leftExpr, PrefixExpression.class);
if (leftPrefix != null && hasOperator(leftPrefix, NOT)) {
leftOppositeExpr = leftPrefix.getOperand();
}
Expression rightOppositeExpr = null;
final PrefixExpression rightPrefix = as(rightExpr, PrefixExpression.class);
if (rightPrefix != null && hasOperator(rightPrefix, NOT)) {
rightOppositeExpr = rightPrefix.getOperand();
}
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
if (leftOppositeExpr != null && rightOppositeExpr != null) {
r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), getAppropriateOperator(node), b.copy(rightOppositeExpr)));
return DO_NOT_VISIT_SUBTREE;
} else if (leftOppositeExpr != null) {
final Operator reverseOp = getReverseOperator(node);
r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), reverseOp, b.copy(rightExpr)));
return DO_NOT_VISIT_SUBTREE;
} else if (rightOppositeExpr != null) {
final Operator reverseOp = getReverseOperator(node);
r.replace(node, b.infixExpr(b.copy(leftExpr), reverseOp, b.copy(rightOppositeExpr)));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class AbstractUnitTestRefactoring method maybeRefactorStatement.
/**
* Maybe refactor the statement.
*
* @param nodeToReplace
* The node
* @param originalMethod
* The method invocation
* @param isAssertTrue
* True if assertTrue is used, False if assertFalse is used.
* @param condition
* The condition on which the assert is based.
* @param failureMessage
* The failure message or null.
* @param isRewriteNeeded
* True if is the rewriting is needed.
* @return True if refactored
*/
protected boolean maybeRefactorStatement(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertTrue, final Expression condition, final Expression failureMessage, final boolean isRewriteNeeded) {
Expression localCondition = condition;
boolean localIsAssertTrue = isAssertTrue;
boolean localIsRewriteNeeded = isRewriteNeeded;
PrefixExpression localConditionPe = as(localCondition, PrefixExpression.class);
while (hasOperator(localConditionPe, NOT)) {
localIsRewriteNeeded = true;
localIsAssertTrue = !localIsAssertTrue;
localCondition = as(localConditionPe.getOperand(), Expression.class);
localConditionPe = as(localCondition, PrefixExpression.class);
}
final InfixExpression conditionIe = as(localCondition, InfixExpression.class);
final MethodInvocation conditionMi = as(localCondition, MethodInvocation.class);
final Object constantValue = localCondition.resolveConstantExpressionValue();
return maybeRefactorAssertTrueOrFalse(nodeToReplace, originalMethod, localIsAssertTrue, localCondition, conditionIe, conditionMi, constantValue, failureMessage, localIsRewriteNeeded);
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ASTBuilder method prefixExpr.
private Expression prefixExpr(PrefixExpression.Operator operator, Expression operand) {
final PrefixExpression pe = ast.newPrefixExpression();
pe.setOperator(operator);
pe.setOperand(operand);
return pe;
}
Aggregations