Search in sources :

Example 36 with ASTBuilder

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

the class StaticConstantRatherThanInstanceConstantRefactoring method addStaticModifier.

private void addStaticModifier(final Modifier finalModifier) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    r.insertBefore(b.static0(), finalModifier);
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 37 with ASTBuilder

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

the class StringRatherThanNewStringRefactoring method visit.

@Override
public boolean visit(ClassInstanceCreation node) {
    if (hasType(node, "java.lang.String") && arguments(node).size() == 1) {
        final Expression arg0 = arguments(node).get(0);
        if (hasType(arg0, "java.lang.String") && ((arg0 instanceof StringLiteral) || (arg0 instanceof InfixExpression))) {
            final ASTBuilder b = ctx.getASTBuilder();
            ctx.getRefactorings().replace(node, b.parenthesizeIfNeeded(b.copy(arg0)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 38 with ASTBuilder

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

the class StringRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    final Expression expr = node.getExpression();
    final ASTNode parent = node.getParent();
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    final boolean isStringValueOf = isStringValueOf(node);
    if (isMethod(node, "java.lang.Object", "toString")) {
        if (hasType(expr, "java.lang.String")) {
            // if node is already a String, no need to call toString()
            r.replace(node, b.move(expr));
            return DO_NOT_VISIT_SUBTREE;
        } else if (parent.getNodeType() == INFIX_EXPRESSION) {
            // if node is in a String context, no need to call toString()
            final InfixExpression ie = (InfixExpression) node.getParent();
            final Expression leftOp = ie.getLeftOperand();
            final Expression rightOp = ie.getRightOperand();
            final boolean leftOpIsString = hasType(leftOp, "java.lang.String");
            final boolean rightOpIsString = hasType(rightOp, "java.lang.String");
            final MethodInvocation lmi = as(leftOp, MethodInvocation.class);
            final MethodInvocation rmi = as(rightOp, MethodInvocation.class);
            if (!node.equals(lmi) && !node.equals(rmi) && (leftOpIsString || rightOpIsString)) {
                // node is in the extended operands
                r.replace(node, replaceToString(node.getExpression()));
                return DO_NOT_VISIT_SUBTREE;
            } else if (leftOpIsString && isMethod(rmi, "java.lang.Object", "toString")) {
                r.replace(rmi, replaceToString(rmi.getExpression()));
                return DO_NOT_VISIT_SUBTREE;
            } else if (rightOpIsString && node.equals(lmi)) {
                r.replace(lmi, replaceToString(lmi.getExpression()));
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    } else if (isStringValueOf && hasType(arg0(node), "java.lang.String")) {
        if ((arg0(node) instanceof StringLiteral) || (arg0(node) instanceof InfixExpression)) {
            r.replace(node, b.parenthesizeIfNeeded(b.move(arg0(node))));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if ((isToStringForPrimitive(node) || isStringValueOf) && parent.getNodeType() == INFIX_EXPRESSION) {
        // if node is in a String context, no need to call toString()
        final InfixExpression ie = (InfixExpression) node.getParent();
        final Expression lo = ie.getLeftOperand();
        final Expression ro = ie.getRightOperand();
        if (node.equals(lo)) {
            if (hasType(ro, "java.lang.String")) {
                replaceStringValueOfByArg0(lo, node);
                return DO_NOT_VISIT_SUBTREE;
            }
        } else if (node.equals(ro)) {
            if (hasType(lo, "java.lang.String") && // to avoid compilation errors post refactoring
            !r.hasBeenRefactored(lo)) {
                replaceStringValueOfByArg0(ro, node);
                return DO_NOT_VISIT_SUBTREE;
            }
        } else {
            // left or right operation is necessarily a string, so just replace
            replaceStringValueOfByArg0(node, node);
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (isMethod(node, "java.lang.String", "equals", "java.lang.Object")) {
        final MethodInvocation leftInvocation = as(node.getExpression(), MethodInvocation.class);
        final MethodInvocation rightInvocation = as(arg0(node), MethodInvocation.class);
        if (leftInvocation != null && rightInvocation != null && ((isMethod(leftInvocation, "java.lang.String", "toLowerCase") && isMethod(rightInvocation, "java.lang.String", "toLowerCase")) || (isMethod(leftInvocation, "java.lang.String", "toUpperCase") && isMethod(rightInvocation, "java.lang.String", "toUpperCase")))) {
            final Expression leftExpr = leftInvocation.getExpression();
            final Expression rightExpr = rightInvocation.getExpression();
            r.replace(node, b.invoke(b.copy(leftExpr), "equalsIgnoreCase", b.copy(rightExpr)));
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (isMethod(node, "java.lang.String", "equalsIgnoreCase", "java.lang.String")) {
        final AtomicBoolean isRefactoringNeeded = new AtomicBoolean(false);
        final Expression leftExpr = getReducedStringExpression(node.getExpression(), isRefactoringNeeded);
        final Expression rightExpr = getReducedStringExpression(arg0(node), isRefactoringNeeded);
        if (isRefactoringNeeded.get()) {
            r.replace(node, b.invoke(b.copy(leftExpr), "equalsIgnoreCase", b.copy(rightExpr)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Refactorings(org.autorefactor.refactoring.Refactorings) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 39 with ASTBuilder

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

the class AbstractUnitTestRefactoring method maybeRefactorToAssertEquals.

/**
 * Maybe refactor the assert equals.
 *
 * @param nodeToReplace
 *            The node to replace
 * @param originalMethod
 *            The node
 * @param isAssertEquals
 *            The is assert equals
 * @param actualValue
 *            The actual value
 * @param expectedValue
 *            The expected value
 * @param failureMessage
 *            The failure message
 * @param isRewriteNeeded
 *            True if is the rewriting is needed.
 * @return The return
 */
protected boolean maybeRefactorToAssertEquals(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertEquals, final Expression actualValue, final Expression expectedValue, final Expression failureMessage, final boolean isRewriteNeeded) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    if (isNullLiteral(actualValue)) {
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, invokeAssertNull(originalMethod, isAssertEquals, expectedValue, failureMessage)));
        return DO_NOT_VISIT_SUBTREE;
    } else if (isNullLiteral(expectedValue)) {
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, invokeAssertNull(originalMethod, isAssertEquals, actualValue, failureMessage)));
        return DO_NOT_VISIT_SUBTREE;
    } else if ((isConstant(actualValue) || isVariableNamedExpected(actualValue)) && !isConstant(expectedValue) && !isVariableNamedExpected(expectedValue)) {
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Equals"), b.copy(expectedValue), b.copy(actualValue), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    } else if (isRewriteNeeded) {
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Equals"), b.copy(actualValue), b.copy(expectedValue), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 40 with ASTBuilder

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

the class AllInOneMethodRatherThanLoopRefactoring method replaceWithCollectionsAddAll.

private void replaceWithCollectionsAddAll(Statement node, Expression iterable, MethodInvocation mi) {
    ASTBuilder b = ctx.getASTBuilder();
    ctx.getRefactorings().replace(node, b.toStmt(b.invoke(b.name("java", "util", "Collections"), "addAll", b.copy(mi.getExpression()), b.copy(iterable))));
}
Also used : 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