Search in sources :

Example 71 with ASTBuilder

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

the class SimplifyExpressionRefactoring method replaceWithNewInfixExpr.

private boolean replaceWithNewInfixExpr(InfixExpression node, final List<Expression> remainingOperands) {
    if (remainingOperands.size() == 1) {
        replaceBy(node, remainingOperands.get(0));
    } else {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.infixExpr(node.getOperator(), b.move(remainingOperands)));
    }
    return DO_NOT_VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 72 with ASTBuilder

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

the class StringBuilderMethodRatherThanReassignationRefactoring method visit.

@Override
public boolean visit(Assignment node) {
    final Expression targetVar = node.getLeftHandSide();
    Expression var = node.getRightHandSide();
    if (ASSIGN.equals(node.getOperator()) && hasType(targetVar, "java.lang.StringBuffer", "java.lang.StringBuilder") && var instanceof MethodInvocation) {
        var = getVar(var);
        if (isSameVariable(targetVar, var)) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            ctx.getRefactorings().replace(node, b.copy(node.getRightHandSide()));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 73 with ASTBuilder

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

the class StringBuilderRefactoring method createStringConcats.

private Expression createStringConcats(final List<Pair<ITypeBinding, Expression>> appendedStrings) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    switch(appendedStrings.size()) {
        case 0:
            return b.string("");
        case 1:
            final Pair<ITypeBinding, Expression> expr = appendedStrings.get(0);
            if (hasType(expr.getSecond(), "java.lang.String")) {
                return b.copy(expr.getSecond());
            }
            return b.invoke("String", "valueOf", getTypedExpression(b, expr));
        default:
            // >= 2
            boolean isFirstAndNotAString = isFirstAndNotAString(appendedStrings);
            List<Expression> concatenateStrings = new ArrayList<Expression>(appendedStrings.size());
            for (final Pair<ITypeBinding, Expression> typeAndValue : appendedStrings) {
                if (isFirstAndNotAString) {
                    concatenateStrings.add(b.invoke("String", "valueOf", getTypedExpression(b, typeAndValue)));
                    isFirstAndNotAString = false;
                } else {
                    concatenateStrings.add(b.parenthesizeIfNeeded(getTypedExpression(b, typeAndValue)));
                }
            }
            return b.infixExpr(Operator.PLUS, concatenateStrings);
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 74 with ASTBuilder

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

the class StringBuilderRefactoring method replaceWithAppendSubstring.

private void replaceWithAppendSubstring(final MethodInvocation node, final MethodInvocation embeddedMI) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Expression stringVar = b.copy(embeddedMI.getExpression());
    final List<Expression> args = arguments(embeddedMI);
    final Expression arg0 = b.copy(args.get(0));
    final Expression arg1 = b.copy(args.get(1));
    final Expression lastExpr = b.copy(node.getExpression());
    MethodInvocation newAppendSubstring = null;
    if (arg1 == null) {
        newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0);
    } else {
        newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0, arg1);
    }
    this.ctx.getRefactorings().replace(node, newAppendSubstring);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 75 with ASTBuilder

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

the class StringBuilderRefactoring method replaceWithNewStringAppends.

/**
 * Rewrite the successive calls to append()
 *
 * @param node The node to replace.
 * @param allAppendedStrings All appended strings.
 * @param lastExpr The expression on which the methods are called.
 * @param isInstanceCreationToRewrite
 */
private void replaceWithNewStringAppends(final Expression node, final LinkedList<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpr, final boolean isInstanceCreationToRewrite) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    Expression result = null;
    final List<Expression> tempStringLiterals = new ArrayList<Expression>();
    final List<Expression> finalStrings = new ArrayList<Expression>();
    final AtomicBoolean isFirst = new AtomicBoolean(true);
    for (final Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
        if (appendedString.getSecond() instanceof StringLiteral) {
            tempStringLiterals.add(b.copy(appendedString.getSecond()));
        } else {
            result = handleTempStringLiterals(b, lastExpr, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
            if (isFirst.get()) {
                isFirst.set(false);
                if (!isInstanceCreationToRewrite) {
                    result = b.copy(lastExpr);
                    finalStrings.add(getTypedExpression(b, appendedString));
                } else if ((appendedString.getFirst() != null) ? hasType(appendedString.getFirst(), "java.lang.String") : hasType(appendedString.getSecond(), "java.lang.String")) {
                    result = b.new0(b.copy(((ClassInstanceCreation) lastExpr).getType()), getTypedExpression(b, appendedString));
                } else {
                    result = b.new0(b.copy(((ClassInstanceCreation) lastExpr).getType()));
                    finalStrings.add(getTypedExpression(b, appendedString));
                }
            } else {
                finalStrings.add(getTypedExpression(b, appendedString));
            }
        }
    }
    result = handleTempStringLiterals(b, lastExpr, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
    for (final Expression finalString : finalStrings) {
        if (result == null) {
            result = finalString;
        } else {
            result = b.invoke(result, "append", finalString);
        }
    }
    ctx.getRefactorings().replace(node, result);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) 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