Search in sources :

Example 16 with ASTBuilder

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

the class AbstractUnitTestRefactoring method maybeRefactorComparison.

private boolean maybeRefactorComparison(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression ie, final boolean isAssertEquals, final Expression failureMessage, final boolean isRewriteNeeded) {
    final Pair<Expression, Expression> actualAndExpected = getActualAndExpected(ie.getLeftOperand(), ie.getRightOperand());
    if (isComparingObjects(ie) && !isNullLiteral(ie.getLeftOperand()) && !isNullLiteral(ie.getRightOperand())) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Refactorings r = this.ctx.getRefactorings();
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Same"), b.copy(actualAndExpected.getFirst()), b.copy(actualAndExpected.getSecond()), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    } else {
        return maybeRefactorToAssertEquals(nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
    }
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 17 with ASTBuilder

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

the class InvertEqualsRefactoring method invertEqualsInvocation.

private void invertEqualsInvocation(final MethodInvocation node, final boolean isEquals, final Expression expr, final Expression arg0) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final String methodName = isEquals ? "equals" : "equalsIgnoreCase";
    this.ctx.getRefactorings().replace(node, b.invoke(b.parenthesizeIfNeeded(b.copy(arg0)), methodName, b.copy(expr)));
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 18 with ASTBuilder

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

the class LazyLogicalRatherThanEagerRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if (!node.hasExtendedOperands() && (hasType(node.getLeftOperand(), "boolean") || hasType(node.getLeftOperand(), "java.lang.Boolean")) && (hasType(node.getRightOperand(), "boolean") || hasType(node.getRightOperand(), "java.lang.Boolean")) && isPassive(node.getRightOperand()) && (Operator.AND.equals(node.getOperator()) || Operator.OR.equals(node.getOperator()))) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.infixExpr(b.copy(node.getLeftOperand()), getLazyOperator(node.getOperator()), b.copy(node.getRightOperand())));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 19 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 20 with ASTBuilder

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

the class ORConditionRatherThanRedundantClausesRefactoring method replaceDuplicateExpr.

private void replaceDuplicateExpr(final InfixExpression node, final Operator operator, final Expression leftExpr, final Expression rightExpr, final boolean isLeftExprPositive, final boolean isRightExprPositive, final boolean forward) {
    final ASTBuilder b = ctx.getASTBuilder();
    Expression copyOfLeftExpr = b.copy(leftExpr);
    if (!isLeftExprPositive) {
        copyOfLeftExpr = b.not(copyOfLeftExpr);
    }
    Expression copyOfRightExpr = b.copy(rightExpr);
    if (!isRightExprPositive) {
        copyOfRightExpr = b.not(copyOfRightExpr);
    }
    if (forward) {
        ctx.getRefactorings().replace(node, b.infixExpr(copyOfLeftExpr, operator, copyOfRightExpr));
    } else {
        ctx.getRefactorings().replace(node, b.infixExpr(copyOfRightExpr, operator, copyOfLeftExpr));
    }
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

ASTBuilder (org.autorefactor.refactoring.ASTBuilder)79 Expression (org.eclipse.jdt.core.dom.Expression)25 Refactorings (org.autorefactor.refactoring.Refactorings)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 Statement (org.eclipse.jdt.core.dom.Statement)9 Type (org.eclipse.jdt.core.dom.Type)9 Block (org.eclipse.jdt.core.dom.Block)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 ArrayList (java.util.ArrayList)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ASTHelper.hasType (org.autorefactor.refactoring.ASTHelper.hasType)4 NotImplementedException (org.autorefactor.util.NotImplementedException)4 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)4 ForStatement (org.eclipse.jdt.core.dom.ForStatement)4 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)4