Search in sources :

Example 11 with Refactorings

use of org.autorefactor.refactoring.Refactorings 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 12 with Refactorings

use of org.autorefactor.refactoring.Refactorings 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 13 with Refactorings

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

the class DoWhileRatherThanDuplicateCodeRefactoring method replaceWithDoWhile.

private void replaceWithDoWhile(final WhileStatement node, final List<Statement> previousStmts) {
    final Refactorings r = this.ctx.getRefactorings();
    r.remove(previousStmts);
    final ASTBuilder b = this.ctx.getASTBuilder();
    r.replace(node, b.doWhile(b.copy(node.getExpression()), b.copy(node.getBody())));
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 14 with Refactorings

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

the class AndroidWakeLockRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    if (isMethod(node, "android.os.PowerManager.WakeLock", "release")) {
        // check whether it is being called in onDestroy()
        MethodDeclaration enclosingMethod = getAncestor(node, MethodDeclaration.class);
        if (isMethod(enclosingMethod, "android.app.Activity", "onDestroy")) {
            final Refactorings r = ctx.getRefactorings();
            TypeDeclaration typeDeclaration = getAncestor(enclosingMethod, TypeDeclaration.class);
            MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                r.remove(node.getParent());
                r.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStmt(node));
            } else {
                // Add the missing onPause() method to the class.
                r.insertAfter(createOnPauseMethodDeclaration(), enclosingMethod);
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (isMethod(node, "android.os.PowerManager.WakeLock", "acquire")) {
        final Refactorings r = ctx.getRefactorings();
        TypeDeclaration typeDeclaration = getAncestor(node, TypeDeclaration.class);
        ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker();
        if (!releasePresenceChecker.findOrDefault(typeDeclaration, false)) {
            MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                r.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStmt(node));
            } else {
                r.insertLast(typeDeclaration, typeDeclaration.getBodyDeclarationsProperty(), createOnPauseMethodDeclaration());
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Refactorings(org.autorefactor.refactoring.Refactorings) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 15 with Refactorings

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

the class AnnotationRefactoring method visit.

@Override
public boolean visit(NormalAnnotation node) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    final List<MemberValuePair> values = values(node);
    if (values.isEmpty()) {
        r.replace(node, b.markerAnnotation(b.move(node.getTypeName())));
        return DO_NOT_VISIT_SUBTREE;
    } else if (values.size() == 1) {
        MemberValuePair pair = values.get(0);
        if ("value".equals(pair.getName().getIdentifier())) {
            r.replace(node, b.singleValueAnnotation(b.move(node.getTypeName()), b.move(pair.getValue())));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    boolean result = VISIT_SUBTREE;
    Map<String, IMethodBinding> elements = toElementsMap(node.resolveAnnotationBinding());
    for (MemberValuePair pair : values) {
        IMethodBinding elementBinding = elements.get(pair.getName().getIdentifier());
        if (equal(elementBinding.getReturnType(), pair.getValue(), elementBinding.getDefaultValue())) {
            r.remove(pair);
            result = DO_NOT_VISIT_SUBTREE;
        } else if (pair.getValue().getNodeType() == ARRAY_INITIALIZER) {
            ArrayInitializer arrayInit = (ArrayInitializer) pair.getValue();
            List<Expression> exprs = expressions(arrayInit);
            if (exprs.size() == 1) {
                r.replace(arrayInit, b.move(exprs.get(0)));
                result = DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return result;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Refactorings(org.autorefactor.refactoring.Refactorings) List(java.util.List) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

Refactorings (org.autorefactor.refactoring.Refactorings)27 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)23 Expression (org.eclipse.jdt.core.dom.Expression)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 Statement (org.eclipse.jdt.core.dom.Statement)5 List (java.util.List)3 Block (org.eclipse.jdt.core.dom.Block)3 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)3 ArrayList (java.util.ArrayList)2 TypeNameDecider (org.autorefactor.refactoring.TypeNameDecider)2 Variable (org.autorefactor.refactoring.Variable)2 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 DoStatement (org.eclipse.jdt.core.dom.DoStatement)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2