Search in sources :

Example 36 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation 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

ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)36 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)13 Expression (org.eclipse.jdt.core.dom.Expression)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)9 Type (org.eclipse.jdt.core.dom.Type)9 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)8 ArrayList (java.util.ArrayList)7 IType (org.eclipse.jdt.core.IType)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)6 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AST (org.eclipse.jdt.core.dom.AST)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 SimpleType (org.eclipse.jdt.core.dom.SimpleType)5