Search in sources :

Example 6 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder 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;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 7 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder 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;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 8 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class AbstractUnitTestRefactoring method refactorToAssertTrueOrFalse.

private boolean refactorToAssertTrueOrFalse(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final Expression failureMessage, final Expression condition, final boolean isAssertTrue) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final String methodName = isAssertTrue ? "assertTrue" : "assertFalse";
    r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, invokeMethod(b, originalMethod, methodName, b.copy(condition), null, failureMessage)));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 9 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class VectorOldToNewAPIRefactoring method replaceWith.

private void replaceWith(final MethodInvocation node, final String newMethodName) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    ctx.getRefactorings().set(node, NAME_PROPERTY, b.simpleName(newMethodName));
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 10 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.

the class VectorOldToNewAPIRefactoring method replaceWithAndSwapArguments.

private void replaceWithAndSwapArguments(final MethodInvocation node, final String newMethodName) {
    final List<Expression> args = arguments(node);
    assertSize(args, 2);
    final Expression arg1 = args.get(1);
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    r.set(node, NAME_PROPERTY, b.simpleName(newMethodName));
    r.moveToIndex(arg1, 0, b.move(arg1));
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

ASTBuilder (org.autorefactor.refactoring.ASTBuilder)44 Expression (org.eclipse.jdt.core.dom.Expression)16 Refactorings (org.autorefactor.refactoring.Refactorings)14 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)4 Statement (org.eclipse.jdt.core.dom.Statement)4 List (java.util.List)3 Block (org.eclipse.jdt.core.dom.Block)3 Type (org.eclipse.jdt.core.dom.Type)3 ArrayList (java.util.ArrayList)2 ASTHelper.hasType (org.autorefactor.refactoring.ASTHelper.hasType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Name (org.eclipse.jdt.core.dom.Name)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1