use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class StringValueOfRatherThanConcatRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if (InfixExpression.Operator.PLUS.equals(node.getOperator()) && extendedOperands(node).isEmpty()) {
final Expression leftOperand = node.getLeftOperand();
final Expression rightOperand = node.getRightOperand();
return maybeReplaceStringConcatenation(node, leftOperand, rightOperand) && // if not replaced then try the other way round
maybeReplaceStringConcatenation(node, rightOperand, leftOperand);
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if ((hasOperator(node, CONDITIONAL_OR) || hasOperator(node, OR)) && !node.hasExtendedOperands()) {
final InfixExpression firstCondition = as(node.getLeftOperand(), InfixExpression.class);
final InfixExpression secondCondition = as(node.getRightOperand(), InfixExpression.class);
if (firstCondition != null && !firstCondition.hasExtendedOperands() && (hasOperator(firstCondition, CONDITIONAL_AND) || hasOperator(firstCondition, AND)) && secondCondition != null && !secondCondition.hasExtendedOperands() && (hasOperator(secondCondition, CONDITIONAL_AND) || hasOperator(secondCondition, AND))) {
final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
final AtomicBoolean isFourthExprPositive = new AtomicBoolean();
final Expression firstExpr = getBasisExpression(firstCondition.getLeftOperand(), isFirstExprPositive);
final Expression secondExpr = getBasisExpression(firstCondition.getRightOperand(), isSecondExprPositive);
final Expression thirdExpr = getBasisExpression(secondCondition.getLeftOperand(), isThirdExprPositive);
final Expression fourthExpr = getBasisExpression(secondCondition.getRightOperand(), isFourthExprPositive);
if (isBooleanVariable(firstExpr) && isBooleanVariable(secondExpr) && isBooleanVariable(thirdExpr) && isBooleanVariable(fourthExpr)) {
if (match(new ASTMatcher(), firstExpr, thirdExpr) && isFirstExprPositive.get() != isThirdExprPositive.get()) {
return maybeReplaceDuplicateExpr(node, firstExpr, firstCondition.getRightOperand(), secondCondition.getRightOperand(), isFirstExprPositive.get());
} else if (match(new ASTMatcher(), firstExpr, fourthExpr) && isFirstExprPositive.get() != isFourthExprPositive.get()) {
return maybeReplaceDuplicateExpr(node, firstExpr, firstCondition.getRightOperand(), secondCondition.getLeftOperand(), isFirstExprPositive.get());
} else if (match(new ASTMatcher(), secondExpr, thirdExpr) && isSecondExprPositive.get() != isThirdExprPositive.get()) {
return maybeReplaceDuplicateExpr(node, secondExpr, firstCondition.getLeftOperand(), secondCondition.getRightOperand(), isSecondExprPositive.get());
} else if (match(new ASTMatcher(), secondExpr, fourthExpr) && isSecondExprPositive.get() != isFourthExprPositive.get()) {
return maybeReplaceDuplicateExpr(node, secondExpr, firstCondition.getLeftOperand(), secondCondition.getLeftOperand(), isSecondExprPositive.get());
}
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Expression in project AutoRefactor by JnRouvignac.
the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method maybeReplaceDuplicateExpr.
private boolean maybeReplaceDuplicateExpr(final InfixExpression node, final Expression firstExpr, final Expression secondExpr, final Expression thirdExpr, final boolean isFirstExprPositive) {
if (!match(new ASTMatcher(), secondExpr, thirdExpr)) {
final ASTBuilder b = ctx.getASTBuilder();
final Expression thenExpr;
final Expression elseExpr;
if (isFirstExprPositive) {
thenExpr = secondExpr;
elseExpr = thirdExpr;
} else {
thenExpr = thirdExpr;
elseExpr = secondExpr;
}
ctx.getRefactorings().replace(node, b.conditionalExpr(b.copy(firstExpr), b.copy(thenExpr), b.copy(elseExpr)));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Expression 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.Expression in project AutoRefactor by JnRouvignac.
the class XORRatherThanDuplicateConditionsRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if ((hasOperator(node, CONDITIONAL_OR) || hasOperator(node, OR)) && !node.hasExtendedOperands()) {
final InfixExpression firstCondition = as(node.getLeftOperand(), InfixExpression.class);
final InfixExpression secondCondition = as(node.getRightOperand(), InfixExpression.class);
if (firstCondition != null && !firstCondition.hasExtendedOperands() && (hasOperator(firstCondition, CONDITIONAL_AND) || hasOperator(firstCondition, AND)) && secondCondition != null && !secondCondition.hasExtendedOperands() && (hasOperator(secondCondition, CONDITIONAL_AND) || hasOperator(secondCondition, AND))) {
final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
final AtomicBoolean isFourthExprPositive = new AtomicBoolean();
final Expression firstExpr = getBasisExpression(firstCondition.getLeftOperand(), isFirstExprPositive);
final Expression secondExpr = getBasisExpression(firstCondition.getRightOperand(), isSecondExprPositive);
final Expression thirdExpr = getBasisExpression(secondCondition.getLeftOperand(), isThirdExprPositive);
final Expression fourthExpr = getBasisExpression(secondCondition.getRightOperand(), isFourthExprPositive);
if (isPassive(firstExpr) && isPassive(secondExpr) && ((match(new ASTMatcher(), firstExpr, thirdExpr) && match(new ASTMatcher(), secondExpr, fourthExpr) && isFirstExprPositive.get() ^ isThirdExprPositive.get() && isSecondExprPositive.get() ^ isFourthExprPositive.get()) || (match(new ASTMatcher(), firstExpr, fourthExpr) && match(new ASTMatcher(), secondExpr, thirdExpr) && isFirstExprPositive.get() ^ isFourthExprPositive.get() && isSecondExprPositive.get() ^ isThirdExprPositive.get()))) {
replaceDuplicateExpr(node, firstExpr, secondExpr, isFirstExprPositive, isSecondExprPositive);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
Aggregations