use of org.eclipse.jdt.core.dom.ASTMatcher in project AutoRefactor by JnRouvignac.
the class UpdateSetRatherThanTestingFirstRefactoring method maybeReplaceSetContains.
private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpr, final Statement stmt, final Statement oppositeStmt, final boolean negate, final String methodName) {
final List<Statement> stmts = asList(stmt);
final MethodInvocation miContains = as(ifExpr, MethodInvocation.class);
if (!stmts.isEmpty() && isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) {
final Statement firstStmt = getFirst(stmts);
final MethodInvocation miAddOrRemove = asExpression(firstStmt, MethodInvocation.class);
final ASTMatcher astMatcher = new ASTMatcher();
if (isMethod(miAddOrRemove, "java.util.Set", methodName, "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAddOrRemove.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAddOrRemove))) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
if (stmts.size() == 1 && asList(oppositeStmt).isEmpty()) {
// Only one statement: replace if statement with col.add() (or col.remove())
r.replace(ifStmtToReplace, b.move(firstStmt));
return DO_NOT_VISIT_SUBTREE;
} else {
// There are other statements, replace the if condition with col.add() (or col.remove())
r.replace(ifStmtToReplace.getExpression(), negate ? b.negate(miAddOrRemove, ASTBuilder.Copy.MOVE) : b.move(miAddOrRemove));
r.remove(firstStmt);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.ASTMatcher in project AutoRefactor by JnRouvignac.
the class CommonIfInIfElseRefactoring method visit.
@Override
public boolean visit(IfStatement node) {
final IfStatement thenInnerIfStmt = as(node.getThenStatement(), IfStatement.class);
final IfStatement elseInnerIfStmt = as(node.getElseStatement(), IfStatement.class);
if (isPassive(node.getExpression()) && thenInnerIfStmt != null && elseInnerIfStmt != null && thenInnerIfStmt.getElseStatement() == null && elseInnerIfStmt.getElseStatement() == null && isPassive(thenInnerIfStmt.getExpression()) && match(new ASTMatcher(), thenInnerIfStmt.getExpression(), elseInnerIfStmt.getExpression())) {
final ASTBuilder b = this.ctx.getASTBuilder();
this.ctx.getRefactorings().replace(node, b.if0(b.move(thenInnerIfStmt.getExpression()), b.block(b.if0(b.move(node.getExpression()), b.move(thenInnerIfStmt.getThenStatement()), b.move(elseInnerIfStmt.getThenStatement())))));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.ASTMatcher 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;
}
use of org.eclipse.jdt.core.dom.ASTMatcher in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
final Expression leftOperand = node.getLeftOperand();
final Expression rightOperand = node.getRightOperand();
final InfixExpression condition = as(leftOperand, InfixExpression.class);
final boolean isNullCheck = hasOperator(condition, EQUALS);
final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
Expression firstExpr = null;
if (isNullLiteral(condition.getLeftOperand())) {
firstExpr = condition.getRightOperand();
} else if (isNullLiteral(condition.getRightOperand())) {
firstExpr = condition.getLeftOperand();
}
Expression secondExpr = null;
final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
final boolean isPositiveExpr;
if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
secondExpr = negateSecondExpr.getOperand();
isPositiveExpr = false;
} else {
secondExpr = rightOperand;
isPositiveExpr = true;
}
if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.ASTMatcher 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;
}
Aggregations