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);
}
Aggregations