Search in sources :

Example 21 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")) {
        r.replace(node, 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) 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 22 with ASTBuilder

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

the class StringRefactoring method replaceStringValueOfByArg0.

private void replaceStringValueOfByArg0(final Expression toReplace, final MethodInvocation mi) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final ITypeBinding expectedType = mi.resolveMethodBinding().getParameterTypes()[0];
    final ITypeBinding actualType = arg0(mi).resolveTypeBinding();
    if (!expectedType.equals(actualType) && !getBoxedTypeBinding(expectedType, mi.getAST()).equals(actualType)) {
        ctx.getRefactorings().replace(toReplace, b.cast(b.type(expectedType.getQualifiedName()), b.move(arg0(mi))));
    } else {
        ctx.getRefactorings().replace(toReplace, b.parenthesizeIfNeeded(b.move(arg0(mi))));
    }
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 23 with ASTBuilder

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

the class TryWithResourceRefactoring method collapseTryStatements.

private boolean collapseTryStatements(TryStatement outerTryStmt, TryStatement innerTryStmt) {
    final Refactorings r = ctx.getRefactorings();
    final ASTBuilder b = ctx.getASTBuilder();
    r.insertLast(outerTryStmt, TryStatement.RESOURCES_PROPERTY, b.copyRange(resources(innerTryStmt)));
    r.replace(innerTryStmt, b.move(innerTryStmt.getBody()));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 24 with ASTBuilder

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

the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method maybeReplaceDuplicateExpr.

private boolean maybeReplaceDuplicateExpr(final InfixExpression node, final Expression firstExpr, final Expression secondExpr, final Expression thirdExpr, final boolean isFirstExprPositive) {
    if (!match(new ASTMatcher(), secondExpr, thirdExpr)) {
        final ASTBuilder b = ctx.getASTBuilder();
        final Expression thenExpr;
        final Expression elseExpr;
        if (isFirstExprPositive) {
            thenExpr = secondExpr;
            elseExpr = thirdExpr;
        } else {
            thenExpr = thirdExpr;
            elseExpr = secondExpr;
        }
        ctx.getRefactorings().replace(node, b.conditionalExpr(b.copy(firstExpr), b.copy(thenExpr), b.copy(elseExpr)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
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) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 25 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")) {
            final ASTBuilder b = ctx.getASTBuilder();
            ctx.getRefactorings().replace(node, b.parenthesizeIfNeeded(b.copy(arg0)));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) 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