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 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.ASTMatcher in project che by eclipse.
the class UnresolvedElementsSubProcessor method getArgumentName.
private static String getArgumentName(List<Expression> arguments, int index) {
String def = String.valueOf(index + 1);
ASTNode expr = arguments.get(index);
if (expr.getLength() > 18) {
return def;
}
ASTMatcher matcher = new ASTMatcher();
for (int i = 0; i < arguments.size(); i++) {
if (i != index && matcher.safeSubtreeMatch(expr, arguments.get(i))) {
return def;
}
}
return '\'' + BasicElementLabels.getJavaElementName(ASTNodes.asString(expr)) + '\'';
}
use of org.eclipse.jdt.core.dom.ASTMatcher in project AutoRefactor by JnRouvignac.
the class CommonCodeInIfElseStatementRefactoring method visit.
// TODO handle switch statements
// TODO also handle ternary operator, ConditionalExpression
@Override
public boolean visit(IfStatement node) {
if (node.getElseStatement() == null) {
return VISIT_SUBTREE;
}
if (!(node.getParent() instanceof Block)) {
// when not inside curly braces
return VISIT_SUBTREE;
}
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
final List<List<Statement>> allCasesStmts = new ArrayList<List<Statement>>();
final List<List<ASTNode>> removedCaseStmts = new LinkedList<List<ASTNode>>();
// Collect all the if / else if / else if / ... / else cases
if (collectAllCases(allCasesStmts, node)) {
// initialize removedCaseStmts list
for (int i = 0; i < allCasesStmts.size(); i++) {
removedCaseStmts.add(new LinkedList<ASTNode>());
}
// If all cases exist
final ASTMatcher matcher = new ASTMatcherSameVariablesAndMethods();
final int minSize = minSize(allCasesStmts);
final List<Statement> caseStmts = allCasesStmts.get(0);
boolean result = VISIT_SUBTREE;
// Identify matching statements starting from the beginning of each case
for (int stmtIndex = 0; stmtIndex < minSize; stmtIndex++) {
if (!match(matcher, allCasesStmts, true, stmtIndex, 0, allCasesStmts.size())) {
break;
}
r.insertBefore(b.copy(caseStmts.get(stmtIndex)), node);
removeStmts(allCasesStmts, true, stmtIndex, removedCaseStmts);
result = DO_NOT_VISIT_SUBTREE;
}
// Identify matching statements starting from the end of each case
for (int stmtIndex = 1; 0 <= minSize - stmtIndex; stmtIndex++) {
if (!match(matcher, allCasesStmts, false, stmtIndex, 0, allCasesStmts.size()) || anyContains(removedCaseStmts, allCasesStmts, stmtIndex)) {
break;
}
r.insertAfter(b.copy(caseStmts.get(caseStmts.size() - stmtIndex)), node);
removeStmts(allCasesStmts, false, stmtIndex, removedCaseStmts);
result = DO_NOT_VISIT_SUBTREE;
}
// Remove the nodes common to all cases
final boolean[] areCasesEmpty = new boolean[allCasesStmts.size()];
for (int i = 0; i < allCasesStmts.size(); i++) {
areCasesEmpty[i] = false;
}
removeStmtsFromCases(allCasesStmts, removedCaseStmts, areCasesEmpty);
if (allEmpty(areCasesEmpty)) {
r.removeButKeepComment(node);
return DO_NOT_VISIT_SUBTREE;
}
// Remove empty cases
if (areCasesEmpty[0]) {
if (areCasesEmpty.length == 2 && !areCasesEmpty[1]) {
// Then clause is empty and there is only one else clause
// => revert if statement
r.replace(node, b.if0(b.not(b.parenthesizeIfNeeded(b.move(node.getExpression()))), b.move(node.getElseStatement())));
} else {
r.replace(node.getThenStatement(), b.block());
}
result = DO_NOT_VISIT_SUBTREE;
}
for (int i = 1; i < areCasesEmpty.length; i++) {
if (areCasesEmpty[i]) {
final Statement firstStmt = allCasesStmts.get(i).get(0);
r.remove(findNodeToRemove(firstStmt, firstStmt.getParent()));
result = DO_NOT_VISIT_SUBTREE;
}
}
return result;
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.ASTMatcher in project AutoRefactor by JnRouvignac.
the class DoWhileRatherThanDuplicateCodeRefactoring method visit.
@Override
public boolean visit(WhileStatement node) {
final List<Statement> whileStmts = asList(node.getBody());
if (whileStmts.isEmpty()) {
return VISIT_SUBTREE;
}
final List<Statement> previousStmts = new ArrayList<Statement>(whileStmts.size());
final ASTMatcher matcher = new ASTMatcher();
Statement previousStmt = getPreviousSibling(node);
int i = whileStmts.size() - 1;
while (i >= 0) {
if (previousStmt == null || !match(matcher, previousStmt, whileStmts.get(i))) {
return VISIT_SUBTREE;
}
i--;
previousStmts.add(previousStmt);
previousStmt = getPreviousSibling(previousStmt);
}
replaceWithDoWhile(node, previousStmts);
return DO_NOT_VISIT_SUBTREE;
}
Aggregations